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常用语法和示例

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

  1. python常用语法和示例_C语言切换案例教程,语法,示例和规则

    python常用语法和示例 使用默认情况下的决策 (Decision making using switch-case-default) Many times in our daily lives, ...

  2. python保存运行结果下次使用_将python运行结果保存至本地文件中的示例讲解

    一.建立文件,保存数据 1.使用python中内置的open函数 打开txt文件 #mode 模式 #w 只能操作写入 r 只能读取 a 向文件追加 #w+ 可读可写 r+可读可写 a+可读可追加 # ...

  3. python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

    python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...

  4. 在配有通道的计算机系统中,用户程序需要输出时,引起的中断是,在配有通道的计算机系统中,用户程序需要输入/输出时,引起的中断是()。A.访管中断B.I/O中断C....

    在配有通道的计算机系统中,用户程序需要输入/输出时,引起的中断是().A.访管中断B.I/O中断C. 更多相关问题 [判断题] "十不吊"是吊装作业必须遵循的原则. [单选] 关于 ...

  5. python 示例_在Python中带有示例的while关键字

    python 示例 关键字的Python (Python for keyword) while is a keyword (case-sensitive) in python, it is used ...

  6. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

  7. python 打印数组变量_使用Python将数组的元素导出到变量中(unpacking)

    下面就为大家分享一篇使用Python将数组的元素导出到变量中(unpacking),具有很好的参考价值,希望对大家有所帮助.一起过来看看吧 最近工作中遇到一个问题,需要利用Python将数组(list ...

  8. python之禅 中文_《Python之禅》中对于Python编程过程中的一些建议

    <Python之禅>中对于Python编程过程中的一些建议 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  <Python之禅>中对于Pyt ...

  9. python list存储对象_《python解释器源码剖析》第4章--python中的list对象

    4.0 序 python中的list对象,底层对应的则是PyListObject.如果你熟悉C++,那么会很容易和C++中的list联系起来.但实际上,这个C++中的list大相径庭,反而和STL中的 ...

最新文章

  1. 网站路径及文件路径问题
  2. 推荐一款 最方便的 阅读blog的软件---SharpReader!
  3. 在VC中如何找到崩溃的源头(二)
  4. 使用Angular reactive form发送HTTP请求的一个简单例子
  5. 云计算基础知识:CPU虚拟化
  6. 扒一扒中断为什么不能调printf
  7. imp-00017 oracle2298,急,imp怪異問題,請高手協助
  8. 网管,请别随意关闭默认共享
  9. go.js节点字体设置
  10. openssh漏洞升级修复
  11. android mvp_Android MVP
  12. 【Chrome小技巧】Chrome浏览器如何实现下载速度加快?
  13. selenium firebug firePath xpath问题
  14. 入侵检测——WebCrack
  15. mac切换网卡|IP
  16. 学plc还是学java_要学PLC想走PLC工程师之路的看看
  17. 或且非 java_Java的运算符号(逻辑与、或、非、移位运算)
  18. win10 无法查看工作组计算机名,win10系统无法查看工作组计算机的技巧
  19. MySQL日期差,时间差,使用DATEDIFF、TIMESTAMPDIFF函数
  20. 模块“Upgrade”启动失败。 未能启动虚拟机。虚拟机VMware不支持的硬件版本【终极解决方案】

热门文章

  1. diabetes影响因子2017_Journal of Diabetes
  2. c语言新龟兔赛跑_幽默 | 新龟兔赛跑
  3. sql plus 表的总记录数是多少_直播回顾 | 亿级并发丝毫不虚,TDSQL-SQL引擎是如何炼成的...
  4. java http 302重定向_Java 纯HTTP请求 禁止302自动重定向
  5. python中 numpy转list list 转numpy
  6. Docker使用-构建MySQL
  7. maven 国内私服
  8. 马哥linux第六周作业
  9. iOS开发中 常用枚举和常用的一些运算符(易错总结)
  10. easyUI layout 中使用tabs+iframe解决请求两次方法