首页 > 编程语言 > 详细

练习题00 - Python2和Python3的区别

时间:2020-07-11 12:15:14      阅读:58      评论:0      收藏:0      [点我收藏+]

一:输入的区别

1.在Python3中input功能会等待用户的输入,用户输入任何内容,都存成字符串类型,然后赋值给等号左边的变量名

username = input(‘请输入用户名:‘)
print(type(username))

# 输入:darker
# 输出:<class ‘str‘>

# 输入:123
# 输出:<class ‘str‘>

# 输入:[1,2,3]
# 输出:<class ‘str‘>

# 输入:(1,2,3)
# 输出:<class ‘str‘>

# 输入:{‘name‘:‘xxq‘, ‘age‘:18}
# 输出:<class ‘str‘>

2.在python2中还存在一个input功能,需要用户输入一个明确的数据类型,输入什么类型就存成什么类型

inp_data = input(‘请输入内容:‘)
print(type(inp_data))

# 输入:darker
# 输出:<class ‘str‘>

# 输入:123
# 输出:<class ‘int‘>

# 输入:[1,2,3]
# 输出:<class ‘list‘>

# 输入:(1,2,3)
# 输出:<class ‘tuple‘>

# 输入:{‘name‘:‘xxq‘, ‘age‘:18}
# 输出:<class ‘dict‘>

3.在python2中存在一个raw_input功能与python3中的input功能一模一样

Python2下:

username = raw_input(‘请输入用户名:‘)

Python3下:

username = input(‘请输入用户名:‘)

练习题00 - Python2和Python3的区别

原文:https://www.cnblogs.com/xuexianqi/p/13282903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!