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

推荐的帖子

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541828.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于node.js和npm 和nvm_byKL

关于node.js和npm 和nvm Node 是一个服务器端 JavaScript 解释器,Node 本身运行 V8 JavaScript。V8 JavaScript 引擎是 Google 用于其 Chrome 浏览器的底层 JavaScript 引擎。 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题&am…

php 查看扩展 代码,[扩展推荐] 使用 PHP Insights 在终端查看 PHP 项目代码质量

PHP Insights 是一个由 Nuno Maduro 发布的、可在控制台进行 PHP 即时质量检查的拓展包。在项目的 readme 文件中,可以发现 PHP Insights 的主要功能包含:代码质量 与 代码风格 分析一个针对于代码 结构 和 复杂度 的漂亮的预览界面在 Laravel、Symfon…

航空机票预订c#代码_航空公司座位预订问题的C ++程序

航空机票预订c#代码Problem statement: Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows: 问题陈述:编写一个程序来分配飞机上的乘客座位。 假设小型飞机的座位编号如下: 1 A B C…

linux命令之which

which这个命令可以说并不常用,它的作用是查看可执行文件的位置,并返回第一个搜索结果。可执行文件也就是指的某个系统命令,但是这个命令的位置必须是在PATH路径里存在的。截图中 ,pwd的位置在/bin/pwd,当然,这个路径是…

线性代数向量乘法_向量的标量乘法| 使用Python的线性代数

线性代数向量乘法Prerequisite: Linear Algebra | Defining a Vector 先决条件: 线性代数| 定义向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

sonar扫描普通JAVA执行,SonarQube扫描源代码的方法

SonarQube扫描源代码的方法雷建锋一、分析源代码综述一旦成功安装了SonarQube平台,您就可以开始安装一个分析器并开始创建项目了。在第一次分析时,该平台会自动创建一个项目。如果您需要在第一个分析之前在项目上设置一些配置,那么您可以选择…

html的学习思维导图

转载于:https://www.cnblogs.com/lingdublog/p/6438088.html

php语言冒泡法,PHP实现冒泡排序算法的案例

PHP实现冒泡排序算法的案例发布时间:2020-10-23 17:39:38来源:亿速云阅读:84作者:小新这篇文章主要介绍PHP实现冒泡排序算法的案例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定…

线性代数分块矩阵求逆矩阵_单位矩阵属性(AI = A)| 使用Python的线性代数

线性代数分块矩阵求逆矩阵Prerequisites: 先决条件: Defining Matrix 定义矩阵 Identity matrix 身份矩阵 numpy.matmul( ) matrix multiplication numpy.matmul()矩阵乘法 In linear algebra, the identity matrix, of size n is the n n square matrix with one…

MySQL5.7.17的简单配置文件

#编译安装mysql5.7.17 [rootweb_1 data]# cat ../my.cnf [client]port3307socket/data/3307/mysql.sock[mysqld]user mysqlbasedir /usr/local/mysqldatadir /data/3307/dataport3307server-id 1socket/data/3307/mysql.sockcharacter-set-server utf8log-error /data/33…

cubic-bezier_带CSS中的示例的cube-bezier()函数

cubic-bezierIntroduction: 介绍: How many times have we come across the word function? Well, it would not be wrong to say a lot. The fact that functions are used in web development while developing a website or web page is very important. There…

php时间调用最简单的,PHP调用时间通过引用不可避免?

给定以下接口:interface ISoapInterface {public static function registerSoapTypes( &$wsdl );public static function registerSoapOperations( &$server );}以及以下代码:$soapProvider array( "FilePool", "UserList" );foreach( $soapProvi…

上手Caffe(一)

author:oneBite 本文记录编译使用caffe for windows 使用环境 VS2013 ultimate,win7 sp1,caffe-windows源码(从github上下载caffe的windows分支,下载解压之后,不要改变原有的目录结构,因为solution rebuild时会使用文件的相对路径…

使用JavaScript的图像识别游戏

Today we are going to develop a fully functional image recognition game using JavaScript. JavaScript is the best fit choice since it is a web-based game. The game is totally based on event handling and event objects. 今天,我们将使用JavaScript开发…

php 判断 in,tinkphp常用判断条件in、notin、between、AND、OR

越来越多的人使用thinkphp框架开发应用,容易上手开发周期短,接下来吾爱编程为大家分享一下tinkphp常用判断条件in、notin、between、AND、OR,有需要的小伙伴可以参考一下:in:{in name"Think.get.level" valu…

关于设置不同linux主机之间ssh免密登录简易方法

2019独角兽企业重金招聘Python工程师标准>>> 在linux日常中,经常会有ssh链接其他主机服务器的action,也学习过大家日常用配置ssh免密登录的方法。 小编今天在这里给大家介绍一种比较简单的配置linux主机ssh免密登录的方法。 两台主机的IP地址&#xff1a…

c语言指针++_C ++此指针| 查找输出程序| 套装1

c语言指针Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int A 10;this* ptr;ptr &A;*ptr 0;cout << *ptr << endl;return 0;}Output: 输出&#xff1a; main.cpp: In function ‘int main()’:main.cpp:7:5: e…

java自定义线程池池,线程池使用及自定义线程池

一 案例引申编写代码同时只允许五个线程并发访问(以下文的函数为例子)private static void method() {System.out.println("ThreadName" Thread.currentThread().getName() "进来了");Thread.sleep(2000);System.out.println("ThreadName" Th…

long类型20位示例_Java Long类reverseBytes()方法与示例

long类型20位示例长类reverseBytes()方法 (Long class reverseBytes() method) reverseBytes() method is available in java.lang package. reverseBytes()方法在java.lang包中可用。 reverseBytes() method is used to returns the value generated by reversing the order o…

impala和mysql语法,impala CREATE TABLE语句

CREATE TABLE语句用于在Impala中的所需数据库中创建新表。 创建基本表涉及命名表并定义其列和每列的数据类型。语法以下是CREATE TABLE语句的语法。 这里&#xff0c;IF NOT EXISTS是一个可选的子句。 如果使用此子句&#xff0c;则只有在指定数据库中没有具有相同名称的现有表…