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字符串替换的2种方法
Nov 30 Python
在Python中测试访问同一数据的竞争条件的方法
Apr 23 Python
python爬虫框架talonspider简单介绍
Jun 09 Python
Python复制Word内容并使用格式设字体与大小实例代码
Jan 22 Python
python单例模式实例解析
Aug 28 Python
对Python3中bytes和HexStr之间的转换详解
Dec 04 Python
对python产生随机的二维数组实例详解
Dec 13 Python
python开发准备工作之配置虚拟环境(非常重要)
Feb 11 Python
NumPy 基本切片和索引的具体使用方法
Apr 24 Python
python实现文件的分割与合并
Aug 29 Python
python爬虫之遍历单个域名
Nov 20 Python
TensorFlow保存TensorBoard图像操作
Jun 23 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+SQL 注入攻击的技术实现以及预防办法
2010/12/29 PHP
使用php判断服务器是否支持Gzip压缩功能
2013/09/24 PHP
php实现比较全的数据库操作类
2015/06/18 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
2017/09/22 PHP
js获取当前日期代码适用于网页头部
2013/06/27 Javascript
ECMA5数组的新增方法有哪些及forEach()模仿实现
2015/11/03 Javascript
详解JavaScript中localStorage使用要点
2016/01/13 Javascript
微信小程序 教程之模板
2016/10/18 Javascript
详解在express站点中使用ejs模板引擎
2017/09/21 Javascript
javascript实现数字配对游戏的实例讲解
2017/12/14 Javascript
JavaScript实现职责链模式概述
2018/01/25 Javascript
react配合antd组件实现的管理系统示例代码
2018/04/24 Javascript
函数式编程入门实践(一)
2019/04/20 Javascript
js模拟实现烟花特效
2020/03/10 Javascript
实例讲解python函数式编程
2014/06/09 Python
python简单的函数定义和用法实例
2015/05/07 Python
Python实现数据库编程方法详解
2015/06/09 Python
python实现备份目录的方法
2015/08/03 Python
Python守护进程和脚本单例运行详解
2017/01/06 Python
Python绘制并保存指定大小图像的方法
2019/01/10 Python
python使用正则筛选信用卡
2019/01/27 Python
Apache部署Django项目图文详解
2019/07/30 Python
ubuntu 18.04 安装opencv3.4.5的教程(图解)
2019/11/04 Python
通过Turtle库在Python中绘制一个鼠年福鼠
2020/02/03 Python
python 穷举指定长度的密码例子
2020/04/02 Python
Keras构建神经网络踩坑(解决model.predict预测值全为0.0的问题)
2020/07/07 Python
python中watchdog文件监控与检测上传功能
2020/10/30 Python
Django集成MongoDB实现过程解析
2020/12/01 Python
英国建筑用品在线:Building Supplies Online(BSO)
2018/04/30 全球购物
Richards网上商店:当代时尚,遍布巴西
2019/11/03 全球购物
《小石潭记》教学反思
2014/02/13 职场文书
抽奖活动主持词
2014/03/31 职场文书
村党支部公开承诺书
2014/05/29 职场文书
团队拓展活动总结
2014/08/27 职场文书
审计局2014法制宣传日活动总结
2014/11/01 职场文书
python实现的人脸识别打卡系统
2021/05/08 Python