Python2 和Python3 的区别

时间:2023-02-04 15:29:24

print

Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。

在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。

整除

Python3 中的 / 是浮点运算,计算出的是精确的值

// 是整除,只去计算的整数部分,余数用% 进行计算

Python2 中的 / 中想要做精确的除法,需要有一个值是浮点数

>>> 10 / 3
3
>>> 10.0 / 3
3.3333333333333335

Python2 和Python3 的整除

C:\Users\Administrator>python2
Python 2.7.13 (v2.7.13:a06454b1afa1, D
AMD64)] on win32
Type "help", "copyright", "credits" or
>>> 3/2
1
>>> 3//2
1
>>> 3/2.0 # 有一个是浮点数的时候是精确的运算
1.5
>>> 3//2.0 # 此时是整除后的浮点数
1.0
>>> ^Z


C:\Users\Administrator>python3
Python 3.6.1 |Anaconda 4.4.0 (64-bit)|
900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or
>>> 3/2 # 精确的运算
1.5
>>> 3//2 # 整除
1
>>> 3/2.0
1.5
>>> 3//2.0
1.0
>>> ^Z

Unicode

Python3 把字节和字符串分开了

参考:
http://python.jobbole.com/80006/#future
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431658624177ea4f8fcb06bc4d0e8aab2fd7aa65dd95000