Python2 和Python3 区别



以下是我使用的python版本。

python2:
>>> import sys
>>> print sys.version
2.7.12+ (default, Sep 17 2016, 12:08:02) 
[GCC 6.2.0 20160914]

python3:
>>> import sys
>>> print(sys.version)
3.5.2+ (default, Sep 22 2016, 12:18:14) 
[GCC 6.2.0 20160927]

  • print变为了函数
Python2:
print 'Hello, World!'

Hello, World!

Python3:
print 'Hello, World!'
  File "<stdin>", line 1
    print 'Hello, World!'
                        ^
SyntaxError: Missing parentheses in call to 'print'

print('Hello, World!')

Hello, World!

注意:给python2的print加括号并不和python3等效,以下是反例。
python2:
print(1,2)

(1, 2)

python3:
print(1,2)

1 2

因为在python2里print不是函数,括号内如果没有逗号会被认为是数学表达式的括号,忽略;如果有逗号,则会被认为是tuple。

  • raw_input重命名为input,覆盖了原来的input函数,且raw_input这个函数名不再使用。
python2:
>>> print type(input('Enter a number: '))
Enter a number: 3
<type 'int'>

>>> print type(raw_input('Enter a number: '))
Enter a number: 3
<type 'str'>

python3:
>>> print(type(input('Enter a number: ')))
Enter a number: 3
<class 'str'>

>>> print(type(raw_input('Enter a number: ')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

  • 整形除法自动转为float
python2:
1/2
2/2
1//2

0
1
0

python3:
1/2
2/2
1//2

0.5
1.0
0

此外,long类型更名为int,覆盖了int类型,long类型这个名称不再使用。此外sys.maxint不存在了,因为现在int没有最大值了。

  • 针对结尾是.5的小数,取整由四舍五入变为取最接近的偶数(银行家舍入法)
python2:
round(1.5)
round(2.5)

2
3

python3:
round(1.5)
round(2.5)

2
2

  • 字符串统一用Unicode,并且增加了专门储存字节数据的类
python2:
print('\u03BCnico\u0394é!')

\u03BCnico\u0394é!

python3:
print('\u03BCnico\u0394é!')

μnicoΔé!

增加的两个类是byte和bytearray,用来和字符串区分。

  • range跟xrange差不多了,因此xrange不存在了
python2:
type(range(5))
type(xrange(5))

<type 'list'>
<type 'xrange'>

python3:
type(range(5))

<class 'range'>

python3的range和原来的xrange还是有一点点区别的。
python2:
xrange(2**1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

python3:
range(2**1000)

range(0, 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376)

  • 跟print一样,抛出异常也必须用括号了
python2:
raise Exception, 'for test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: for test

python3:
raise Exception, 'for test'
File "<stdin>", line 1
    raise Exception, 'for test'
                   ^
SyntaxError: invalid syntax

raise Exception('for test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: for test

  • 捕获异常必须使用except...as...的格式
python2:
>>> try:
...     a = b
... except NameError, err:
...     print "Something's wrong!"
...     print err
... 
Something's wrong!
name 'b' is not defined

>>> try:
...     a = b
... except NameError as err:
...     print "Something's wrong!"
...     print err
... 
Something's wrong!
name 'b' is not defined

python3:
>>> try:
...     a = b
... except NameError, err:
  File "<stdin>", line 3
    except NameError, err:
                    ^
SyntaxError: invalid syntax

>>> try:
...     a = b
... except NameError as err:
...     print("Something's wrong!")
...     print(err)
... 
Something's wrong!
name 'b' is not defined

编写过程中主要参考了这篇文章:
The key differences between Python 2.7.x and Python 3.x with examples

另请参阅官方文档中的比较:
docs.python.org/3/whats
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值