python常用语法和示例_使用Python中的示例进行输入和输出操作

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

推荐的帖子

翻译自: https://www.includehelp.com/python/input-and-output-operations-with-examples.aspx

python常用语法和示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值