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中return和finally共同挖的坑
Aug 18 Python
Python基于回溯法子集树模板解决数字组合问题实例
Sep 02 Python
Django数据库表反向生成实例解析
Feb 06 Python
pandas分别写入excel的不同sheet方法
Dec 11 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
Django时区详解
Jul 24 Python
Python3 requests文件下载 期间显示文件信息和下载进度代码实例
Aug 16 Python
Python实现语音识别和语音合成功能
Sep 20 Python
python银行系统实现源码
Oct 25 Python
VSCode基础使用与VSCode调试python程序入门的图文教程
Mar 30 Python
Anaconda和ipython环境适配的实现
Apr 22 Python
PyCharm+PyQt5+QtDesigner配置详解
Aug 12 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面向对象全攻略 (一) 面向对象基础知识
2009/09/30 PHP
php写app接口并返回json数据的实例(分享)
2017/05/20 PHP
Javascript面向对象编程
2012/03/18 Javascript
js中eval详解
2012/03/30 Javascript
jquery限定文本框只能输入数字即整数和小数
2013/11/29 Javascript
Nodejs实现批量下载妹纸图
2015/05/28 NodeJs
jQuery on()绑定动态元素出现的问题小结
2016/02/19 Javascript
jQuery siblings()用法实例详解
2016/04/26 Javascript
一种Javascript解释ajax返回的json的好方法(推荐)
2016/06/02 Javascript
由简入繁实现Jquery树状结构的方法(推荐)
2016/06/10 Javascript
Nodejs下DNS缓存问题浅析
2016/11/16 NodeJs
详解springmvc 接收json对象的两种方式
2016/12/06 Javascript
深入理解node.js http模块
2018/01/24 Javascript
详解Vue中使用Axios拦截器
2019/04/22 Javascript
在Vue.js中使用TypeScript的方法
2020/03/19 Javascript
Vue.js中Line第三方登录api的实现代码
2020/06/29 Javascript
解决VUE自定义拖拽指令时 onmouseup 与 click事件冲突问题
2020/07/24 Javascript
python端口扫描系统实现方法
2014/11/19 Python
Python使用Django实现博客系统完整版
2020/09/29 Python
Python实现的直接插入排序算法示例
2018/04/29 Python
Python将json文件写入ES数据库的方法
2019/04/10 Python
python加载自定义词典实例
2019/12/06 Python
Python实现word2Vec model过程解析
2019/12/16 Python
python 在threading中如何处理主进程和子线程的关系
2020/04/25 Python
移动端rem布局的两种实现方法
2018/01/03 HTML / CSS
移动端Web页面的CSS3 flex布局快速上手指南
2016/05/31 HTML / CSS
HTML5自定义mp3播放器源码
2020/01/06 HTML / CSS
英国最大的邮寄种子和植物公司:Thompson & Morgan
2017/09/21 全球购物
Linux管理员面试经常问道的相关命令
2014/12/12 面试题
中文系师范生自荐信
2013/10/01 职场文书
《小草和大树》教学反思
2014/02/16 职场文书
质量承诺书怎么写
2014/05/24 职场文书
授权收款委托书
2014/09/23 职场文书
母亲去世追悼词
2015/06/23 职场文书
小学中队委竞选稿
2015/11/20 职场文书
安全责任协议书范本
2016/03/23 职场文书