python2 与 pyhton3的输入语句写法小结


Posted in Python onSeptember 10, 2018

什么是输入

咱们在银行ATM机器前取钱时,肯定需要输入密码,对不?

那么怎样才能让程序知道咱们刚刚输入的是什么呢??

大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入一个数据,然后用一个变量来保存,是不是很好理解啊

1、python2的输入语句

在python2中有两种常见的输入语句,input()raw_input()

(1)input()函数

可以接收不同类型的参数,而且返回的是输入的类型。如,当你输入int类型数值,那么返回就是int型;其中字符型需要用单引号或双引号,否则,报错。

a.数值型输入

>>> a = input()
>>> type(a)
<type 'int'>
>>> a
>>> a = input()
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

b.字符类型

如果输入的字符不加引号,就会报错

>>> r = input()
hello

Traceback (most recent call last):
 File "<pyshell#50>", line 1, in <module>
 r = input()
 File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

正确的字符输入

>>> r = input()
'hello'
>>> r
'hello'
>>> r = input()
"hello"
>>> r
'hello'

当然,可以对输入的字符加以说明

>>> name = input('please input name:')
please input name:'Tom'
>>> print 'Your name : ',name
Your name : Tom

(2)raw_input()

函数raw_input()是把输入的数据全部看做字符类型。输入字符类型时,不需要加引号,否则,加的引号也会被看做字符。

>>> a = raw_input()
>>> type(a)
<type 'str'>
>>> a
'1'
>>> a = raw_input()
'hello'
>>> type(a)
<type 'str'>
>>> a
"'hello'"

如果想要int类型数值时,可以通过调用相关函数转化。

>>> a = int(raw_input())
>>> type(a)
<type 'int'>
>>> a
>>> a = float(raw_input())
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

在同一行中输入多个数值,可以有多种方式,这里给出调用map() 函数的转换方法。map使用方法请参考python-map的用法

>>> a, b = map(int, raw_input().split())
20
>>> a
>>> b
>>> l = list(map(int, raw_input().split()))
2 3 4
>>> l
[1, 2, 3, 4]

(3)input() 和raw_input()的区别

通过查看input()帮助文档,知道input函数也是通过调用raw_input函数实现的,区别在于,input函数额外调用内联函数eval()。eval使用方法参考Python eval 函数妙用 (见下面)

>>> help(input)
Help on built-in function input in module __builtin__:

input(...)
 input([prompt]) -> value
 
 Equivalent to eval(raw_input(prompt)).

>>> help(eval)
Help on built-in function eval in module __builtin__:

eval(...)
 eval(source[, globals[, locals]]) -> value
 
 Evaluate the source in the context of globals and locals.
 The source may be a string representing a Python expression
 or a code object as returned by compile().
 The globals must be a dictionary and locals can be any mapping,
 defaulting to the current globals and locals.
 If only globals is given, locals defaults to it.

Python eval 函数妙用

eval

功能:将字符串str当成有效的表达式来求值并返回计算结果。

语法: eval(source[, globals[, locals]]) -> value

参数:

source:一个Python表达式或函数compile()返回的代码对象

globals:可选。必须是dictionary

locals:可选。任意map对象

实例展示:

可以把list,tuple,dict和string相互转化。
#################################################
字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
<type 'list'>
#################################################
字符串转换成字典
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
{1: 'a', 2: 'b'}
>>> type(b)
<type 'dict'>
#################################################
字符串转换成元组
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
<type 'tuple'>

2、Python3输入语句

python3中的输入语句只有input()函数,没有raw_input();而且python3中的input()函数与python2中的raw_input()的使用方法一样。

>>> a = input()
10
>>> type(a)
<class 'str'>
>>> a
'10'

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python使用paramiko实现远程拷贝文件的方法
Apr 18 Python
Python实现类似jQuery使用中的链式调用的示例
Jun 16 Python
linecache模块加载和缓存文件内容详解
Jan 11 Python
Python入门之后再看点什么好?
Mar 05 Python
Python下简易的单例模式详解
Apr 08 Python
通过selenium抓取某东的TT购买记录并分析趋势过程解析
Aug 15 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
python输出结果刷新及进度条的实现操作
Jul 13 Python
详解anaconda离线安装pytorchGPU版
Sep 08 Python
使用OpenCV实现人脸图像卡通化的示例代码
Jan 15 Python
高考要来啦!用Python爬取历年高考数据并分析
Jun 03 Python
python 多态 协议 鸭子类型详解
Nov 27 Python
django DRF图片路径问题的解决方法
Sep 10 #Python
详解python中Numpy的属性与创建矩阵
Sep 10 #Python
tensorflow使用神经网络实现mnist分类
Sep 08 #Python
Python unittest单元测试框架总结
Sep 08 #Python
tensorflow实现加载mnist数据集
Sep 08 #Python
使用tensorflow实现线性回归
Sep 08 #Python
Python  unittest单元测试框架的使用
Sep 08 #Python
You might like
php读取30天之内的根据算法排序的代码
2008/04/06 PHP
php xml常用函数的集合(比较详细)
2013/06/06 PHP
PHP使用Alexa API获取网站的Alexa排名例子
2014/06/12 PHP
php实现微信模板消息推送
2018/03/30 PHP
thinkPHP5.1框架路由::get、post请求简单用法示例
2019/05/06 PHP
Thinkphp 框架扩展之应用模式实现方法分析
2020/04/27 PHP
PHP中isset、empty的用法与区别示例详解
2020/11/05 PHP
javascript与CSS复习(《精通javascript》)
2010/06/29 Javascript
js修改input的type属性及浏览器兼容问题探讨与解决
2013/01/23 Javascript
angularJS中$apply()方法详解
2015/01/07 Javascript
自己动手手写jQuery插件总结
2015/01/20 Javascript
Jquery+Ajax+PHP+MySQL实现分类列表管理(下)
2015/10/28 Javascript
分享我的jquery实现下拉菜单心的
2015/11/29 Javascript
jQuery判断checkbox选中状态
2016/05/12 Javascript
Node.js中npm常用命令大全
2016/06/09 Javascript
浅析Javascript ES6新增值比较函数Object.is
2016/08/24 Javascript
使用node打造自己的命令行工具方法教程
2018/03/26 Javascript
vue项目打包后怎样优雅的解决跨域
2019/05/26 Javascript
KnockoutJS数组比较算法实例详解
2019/11/25 Javascript
使用Python的Zato发送AMQP消息的教程
2015/04/16 Python
通过数据库对Django进行删除字段和删除模型的操作
2015/07/21 Python
Python 40行代码实现人脸识别功能
2017/04/02 Python
Python实现识别手写数字 Python图片读入与处理
2020/03/23 Python
python 读取dicom文件,生成info.txt和raw文件的方法
2019/01/24 Python
opencv 查找连通区域 最大面积实例
2020/06/04 Python
Django-imagekit的使用详解
2020/07/06 Python
详解Python3.8+PyQt5+pyqt5-tools+Pycharm配置详细教程
2020/11/02 Python
史泰博(Staples)中国官方网站:办公用品一站式采购
2016/09/05 全球购物
Furla官网:意大利著名的皮革品牌
2019/08/06 全球购物
Rentalcars.com中国:世界上最大的在线汽车租赁服务
2019/08/22 全球购物
No7 Beauty美国官网:英国国民护肤品牌
2019/10/31 全球购物
业务主管岗位职责
2013/11/20 职场文书
初中同学聚会邀请函
2014/02/03 职场文书
酒鬼酒广告词
2014/03/21 职场文书
2014年材料员工作总结
2014/11/19 职场文书
学习与创新自我评价
2015/03/09 职场文书