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多线程编程方式分析示例详解
Dec 06 Python
python图像处理之反色实现方法
May 30 Python
使用python实现省市三级菜单效果
Jan 20 Python
python3使用requests模块爬取页面内容的实战演练
Sep 25 Python
python爬取足球直播吧五大联赛积分榜
Jun 13 Python
Python爬虫常用小技巧之设置代理IP
Sep 13 Python
Python numpy中矩阵的基本用法汇总
Feb 12 Python
python实现nao机器人手臂动作控制
Apr 29 Python
Python AutoCAD 系统设置的实现方法
Apr 01 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
Apr 22 Python
python中shell执行知识点
May 06 Python
基于python调用jenkins-cli实现快速发布
Aug 14 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
实现了一个PHP5的getter/setter基类的代码
2007/02/25 PHP
优化PHP代码的53条建议
2008/03/27 PHP
php printf输出格式使用说明
2010/12/05 PHP
PHP备份数据库生成SQL文件并下载的函数代码
2012/02/05 PHP
Linux系统下使用XHProf和XHGui分析PHP运行性能
2015/12/08 PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
2016/07/02 PHP
php简单截取字符串代码示例
2016/10/19 PHP
User Scripts: Video Download by User Scripts
2007/05/14 Javascript
javascript xml为数据源的下拉框控件
2009/07/07 Javascript
js中return false(阻止)的用法
2013/08/14 Javascript
三种检测iPhone/iPad设备方向的方法
2014/04/23 Javascript
JavaScript知识点总结(十一)之js中的Object类详解
2016/05/31 Javascript
JS实现输入框提示文字点击时消失效果
2016/07/19 Javascript
bootstrap下拉列表与输入框组结合的样式调整
2016/10/08 Javascript
jquery表单插件form使用方法详解
2017/01/20 Javascript
Vue.js基础指令实例讲解(各种数据绑定、表单渲染大总结)
2017/07/03 Javascript
Vue.js单向绑定和双向绑定实例分析
2018/08/14 Javascript
ES6数组与对象的解构赋值详解
2019/06/14 Javascript
[51:53]DOTA2-DPC中国联赛 正赛 RNG vs Dragon BO3 第二场 1月24日
2021/03/11 DOTA
Python线程的两种编程方式
2015/04/14 Python
Python实现模拟登录及表单提交的方法
2015/07/25 Python
Python进阶-函数默认参数(详解)
2017/05/18 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
2018/07/11 Python
Pandas分组与排序的实现
2019/07/23 Python
浅谈django channels 路由误导
2020/05/28 Python
prAna官网:瑜伽、旅行和冒险服装
2019/03/10 全球购物
美国时尚大码女装购物网站:Avenue
2019/05/24 全球购物
Crocs波兰官方商店:女鞋、男鞋、童鞋、洞洞鞋
2019/10/08 全球购物
澳大利亚领先的男装零售连锁店:Lowes
2020/08/07 全球购物
争做文明公民倡议书
2014/08/29 职场文书
奥巴马经典演讲稿
2014/09/13 职场文书
旷课检讨书范文
2014/10/30 职场文书
遗失证明范文
2015/06/19 职场文书
预备党员入党感言
2015/08/01 职场文书
pandas中DataFrame重置索引的几种方法
2021/05/24 Python
用Python将GIF动图分解成多张静态图片
2021/06/11 Python