博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3与2的故事一
阅读量:4562 次
发布时间:2019-06-08

本文共 2040 字,大约阅读时间需要 6 分钟。

print函数:(Python3中print为一个函数,必须用括号括起来;Python2中print为class)

Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打印在小括号中的对象。

Python 2

print 'Python', python_version()
print 'Hello, World!'
print(
'Hello, World!')
print "text", ; 
print 'print more text on the same line'
 
run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line

Python 3

1
2
3
4
print(
'Python', python_version())
print(
'Hello, World!')
print(
"some text,", end="")
print(
' print more text on the same line')

run result:

Python 3.4.1
Hello, World!
some text, print more text on the same line


通过input()解析用户的输入:(Python3中input得到的为str;Python2的input的到的为int型,Python2的raw_input得到的为str类型)统一一下:Python3中用input,Python2中用row_input,都输入为str

幸运的是,在 Python 3 中已经解决了把用户的输入存储为一个 str 对象的问题。为了避免在 Python 2 中的读取非字符串类型的危险行为,我们不得不使用 raw_input() 代替。

Python 2

Python 2.7.6
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input('enter a number: ') enter a number: 123 >>> type(my_input) 
>>> my_input = raw_input('enter a number: ') enter a number: 123 >>> type(my_input)

Python 3

Python 3.4.1
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input('enter a number: ') enter a number: 123 >>> type(my_input) 

 


 

整除:(没有太大影响)(Python3中/表示真除,%表示取余,//表示地板除(结果取整);Python2中/表示根据除数被除数小数点位得到结果,//同样表示地板除)统一一下:Python3中/表示真除,%表示取余,//结果取整;Python2中带上小数点/表示真除,%表示取余,//结果取整

Python 2

1
2
3
4
5
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0

run result:

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3

1
2
3
4
5
print(
'Python', python_version())
print(
'3 / 2 =', 3 / 2)
print(
'3 // 2 =', 3 // 2)
print(
'3 / 2.0 =', 3 / 2.0)
print(
'3 // 2.0 =', 3 // 2.0)

run result:

Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

转载于:https://www.cnblogs.com/linux985/p/10208706.html

你可能感兴趣的文章
2018大都会赛 A Fruit Ninja【随机数】
查看>>
【实战HTML5与CSS3】用HTML5和CSS3制作页面(上)
查看>>
小公司的一年,一起看看小公司的前端可以怎么做
查看>>
oracle数据批处理
查看>>
Json网络解析
查看>>
[转]Google Chrome/IE/FireFox查看HTTP请求头request header响应头
查看>>
Harris角点检测
查看>>
Struts2的处理流程及为Action的属性注入值
查看>>
设计中最常用的CSS选择器
查看>>
Maven项目打包成可执行Jar文件
查看>>
nginx http proxy 正向代理
查看>>
对BFC的总结
查看>>
第十四周Java学习总结
查看>>
税率等级
查看>>
__alloc_pages
查看>>
web service 使用多态(转载)
查看>>
23醒
查看>>
Google Hack的一些整理
查看>>
[贪心] JZOJ P3757 随机生成器
查看>>
Codeforces Round #370 (Div. 2)(简单逻辑,比较水)
查看>>