python常用语法和示例
A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input.
一个程序需要与用户交互以完成所需的任务; 这是使用输入输出功能完成的。 输入是指程序用户输入的数据。 在python中,我们为Input提供了input()和raw_input()函数。
1)input()函数 (1) input() function)
Syntax:
句法:
input (expression)
If prompt is present, it is displayed on monitor, after which the user can provide data from keyboard. Input takes whatever is typed from the keyboard and evaluates it. As the input provided is evaluated, it expects valid python expression. If the input provided is not correct then either syntax error or exception is raised by python.
如果出现提示,它将显示在监视器上,之后用户可以从键盘提供数据。 输入接受从键盘输入的任何内容并对其求值。 在评估提供的输入时,它需要有效的python表达式。 如果提供的输入不正确,则python会引发语法错误或异常。
Example 1:
范例1:
# python input operations
# user input
x = input("Enter any value: ")
# printing value
print("Entered value is: ", x)
Output
输出量
RUN 1:
Enter any value: 12345
Entered value is: 12345
RUN 2:
Enter any value: IncludeHelp
Entered value is: IncludeHelp
RUN 3:
Enter any value: Python is a progamming language.
Entered value is: Python is a progamming language.
Example 2:
范例2:
# python input operations
# just provide a value and entered value prints
print(input())
# provide another value
x = input()
print("Your input is: ", x)
# prompting message for input
val1 = input("Enter a value: ")
val2 = input("Enter another value: ")
val3 = input("Enter another value: ")
# printing values
print("val1 =", val1)
print("val2 =", val2)
print("val3 =", val3)
Output
输出量
Hello
Hello
I'm Shivang!
Your input is: I'm Shivang!
Enter a value: 100
Enter another value: 23.45
Enter another value: Helllooooooo
val1 = 100
val2 = 23.45
val3 = Helllooooooo
2)raw_input()函数 (2) raw_input() function)
This input method fairly works in older versions (like 2.x).
此输入法在较旧的版本(如2.x)中相当有效。
Syntax:
句法:
raw_input (expression)
If prompt is present, it is displayed on the monitor after which user can provide the data from keyboard. The function takes exactly what is typed from keyboard, convert it to string and then return it to the variable on LHS of '='.
如果出现提示,则它会显示在监视器上,之后用户可以通过键盘提供数据。 该函数将完全采用键盘输入的内容,将其转换为字符串,然后将其返回到'='的 LHS上的变量。
Example: In interactive mode
示例:在交互模式下
>>>x=raw_input ('Enter your name: ')
Enter your name: ABC
x is a variable which will get the string (ABC), typed by user during the execution of program. Typing of data for the raw_input function is terminated by enter key.
x是一个变量,它将获取字符串(ABC),由用户在程序执行期间键入。 用enter键终止raw_input函数的数据输入 。
We can use raw_input() to enter numeric data also. In that case we typecast, i.e., change the data type using function, the string data accepted from user to appropriate Numeric type.
我们也可以使用raw_input()输入数字数据。 在那种情况下,我们进行类型转换,即使用函数将数据类型(用户接受的字符串数据更改为适当的数字类型)。
Example:
例:
>>>y=int(raw_input("Enter your roll no."))
Enter your roll no. 5
It will convert the accepted string i.e., 5 to integer before assigning it to 'y'.
它将接受的字符串(即5)转换为整数,然后将其分配给“ y”。
print()函数/声明 (print() function/statement)
print evaluates the expression before printing it on the monitor. Print statement outputs an entire (complete) line and then goes to next line for subsequent output (s). To print more than one item on a single line, comma (,) may be used.
print先计算表达式,然后再将其打印在监视器上。 Print语句输出整行(完整),然后转到下一行以进行后续输出。 要在一行上打印多个项目,可以使用逗号(,)。
Syntax:
句法:
print (expression/constant/variable)
Example 1:
范例1:
# print() example in Python
# using single quotes
print('Hello!')
print('How are you?')
# using double quotes
print("Hello!")
print("How are you?")
# using triple single quotes
# those can be used to print multiple line string
print('''Hello!''')
print('''How are you?''')
# printing multiline string
print('''Hello... how are you?
Hey! I am good, what about you?
I am good also, thanks.''')
Output
输出量
Hello!
How are you?
Hello!
How are you?
Hello!
How are you?
Hello... how are you?
Hey! I am good, what about you?
I am good also, thanks.
Example 2:
范例2:
# print() example in Python
# printing values
print("Printing direct values...")
print(10) # printing an integer
print(10.2345) # printing a float
print([10, 20, 30, 40, 50]) # printing a list
print({10, 20, 30, 40, 50}) # printing a set
# printing variables
a = 10
b = 10.2345
c = [10, 20, 30, 40, 50]
d = {10, 20, 30, 40, 50}
print("Printing variables...")
print(a)
print(b)
print(c)
print(d)
# printing message with variables
print("Printing message variables...")
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
Output
输出量
Printing direct values...
10
10.2345
[10, 20, 30, 40, 50]
{40, 10, 50, 20, 30}
Printing variables...
10
10.2345
[10, 20, 30, 40, 50]
{40, 10, 50, 20, 30}
Printing message variables...
a = 10
b = 10.2345
c = [10, 20, 30, 40, 50]
d = {40, 10, 50, 20, 30}
Recommended posts
推荐的帖子
Read input as an integer in Python
在Python中将输入读取为整数
Read input as a float in Python
在Python中以浮点形式读取输入
Parse a string to float in Python (float() function)
解析要在Python中浮动的字符串(float()函数)
How do you read from stdin in Python?
您如何从Python的stdin中读取信息?
Asking the user for integer input in Python | Limit the user to input only integer value
要求用户在Python中输入整数| 限制用户仅输入整数值
Asking the user for input until a valid response in Python
要求用户输入直到Python中的有效响应
Input a number in hexadecimal format in Python
在Python中以十六进制格式输入数字
Input a number in octal format in Python
在Python中以八进制格式输入数字
Input a number in binary format in Python
在Python中以二进制格式输入数字
How to get the hexadecimal value of a float number in python?
如何在python中获取浮点数的十六进制值?
Convert an integer value to the string using str() function in Python
使用Python中的str()函数将整数值转换为字符串
Convert a float value to the string using str() function in Python
使用Python中的str()函数将浮点值转换为字符串
Taking multiple inputs from the user using split() method in Python
使用Python中的split()方法从用户获取多个输入
Fast input / output for competitive programming in Python
快速输入/输出,可在Python中进行有竞争力的编程
Precision handling in Python
Python中的精确处理
Python print() function with end parameter
带有结束参数的Python print()函数
翻译自: https://www.includehelp.com/python/input-and-output-operations-with-examples.aspx
python常用语法和示例