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

 更新时间:2018年09月10日 10:47:58   作者:追寻的鹿  
这篇文章主要给大家介绍了关于python2 与 pyhton3的输入语句写法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

什么是输入

咱们在银行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实现代码审查自动回复消息

    这篇文章主要介绍了python实现代码审查回复消息生成的示例,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2021-02-02
  • 详解TensorFlow在windows上安装与简单示例

    详解TensorFlow在windows上安装与简单示例

    这篇文章主要介绍了详解TensorFlow在windows上安装与简单示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Python编程生成随机用户名及密码的方法示例

    Python编程生成随机用户名及密码的方法示例

    这篇文章主要介绍了Python编程生成随机用户名及密码的方法,结合实例形式分析了Python随机字符串的相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • 使用python实现快速搭建简易的FTP服务器

    使用python实现快速搭建简易的FTP服务器

    本文给大家推荐的是如何使用Python实现快速搭建简易的FTP服务器的方法,非常的简单,有需要的小伙伴可以参考下
    2018-09-09
  • 教你学会通过python的matplotlib库绘图

    教你学会通过python的matplotlib库绘图

    今天教大家如何学会通过python的matplotlib库绘图,文中有非常详细的图文解说及代码示例,对正在学习python的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • django orm模块中的 is_delete用法

    django orm模块中的 is_delete用法

    这篇文章主要介绍了django orm模块中的 is_delete用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 使用pytorch读取数据集

    使用pytorch读取数据集

    这篇文章主要介绍了使用pytorch读取数据集,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • python利用lxml库剩下操作svg图片

    python利用lxml库剩下操作svg图片

    在大多数场景中,我们都用 lxml 库解析网页源码,但你是否知道,lxml 库也是可以操作 svg 图片的。本文就来和大家聊聊具体操作方法,希望对大家有所帮助
    2023-01-01
  • 精心整理总结的Python自动化测试面试题

    精心整理总结的Python自动化测试面试题

    简单来说,自动化测试框架包含了所有的测试工作所需的测试框架,下面这篇文章主要给大家介绍了关于Python自动化测试面试题的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • 利用python打印出菱形、三角形以及矩形的方法实例

    利用python打印出菱形、三角形以及矩形的方法实例

    最近在开发中遇到一个问题,需要利用python实现菱形、三角形以及矩形等形状,发现网上这方面的资料较少,所以总结分享下,这篇文章主要给大家介绍了关于利用python打印出菱形、三角形以及矩形的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08

最新评论