Python 2.x和Python 3.x之间的重要区别和示例

2021年11月10日21:07:45 发表评论 604 次浏览

Python 2.x和Python 3.x的区别主要提现在如下几个方面:

  • 除法运算符
  • 打印功能
  • 统一码
  • 范围
  • 错误处理
  • __future__ 模块

除法运算符

如果我们在 python 2.x 中移植我们的代码或执行 python 3.x 代码,如果整数除法更改未被注意到(因为它不会引发任何错误),这可能是危险的。在移植我们的代码时,最好使用浮点值(如 7.0/5 或 7/5.0)来获得预期的结果。 
 

print 7 / 5
 
print -7 / 5    
 
   
 
'''
 
Output in Python 2.x
 
1
 
-2
 
Output in Python 3.x :
 
1.4
 
-1.4

打印函数

Python 2.x和Python 3.x的区别:这是最广为人知的变化。在此,Python 2.x 中的print关键字被Python 3.x 中的print()函数替换。但是,如果在print关键字后添加空格,则括号在 Python 2 中有效,因为解释器将其计算为表达式。 

  • Python
print 'Hello, Geeks'      # Python 3.x doesn't support
 
print('Hope You like these facts')
 
   
 
'''
 
Output in Python 2.x :
 
Hello, Geeks
 
Hope You like these facts
 
   
 
Output in Python 3.x :
 
File "a.py", line 1
 
    print 'Hello, Geeks'
 
                       ^
 
SyntaxError: invalid syntax

Python 2和Python3有什么不同?如我们所见,如果我们在 python 2.x 中使用括号,则没有问题,但如果我们在 python 3.x 中不使用括号,则会出现 SyntaxError。 

Unicode:

在 Python 2 中,隐式 str 类型是 ASCII。但是在 Python 3.x 中,隐式 str 类型是 Unicode。 

  • Python
print(type('default string '))
 
print(type(b'string with b '))
 
   
 
'''
 
Output in Python 2.x (Bytes is same as str)
 
<type 'str'>
 
<type 'str'>
 
   
 
Output in Python 3.x (Bytes and str are different)
 
<class 'str'>
 
<class 'bytes'>
 
'''

Python 2.x 也支持 Unicode 

  • Python 2和Python3差异示例
print(type('default string '))
 
print(type(u'string with b '))
 
   
 
'''
 
Output in Python 2.x (Unicode and str are different)
 
<type 'str'>
 
<type 'unicode'>
 
   
 
Output in Python 3.x (Unicode and str are same)
 
<class 'str'>
 
<class 'str'>
 
'''

xrange

Python 2和Python3有什么不同?Python 3.x 中不存在 Python 2.x 的 xrange()。在 Python 2.x 中,range 返回一个列表,即 range(3) 返回 [0, 1, 2] 而 xrange 返回一个 xrange 对象,即 xrange(3) 返回迭代器对象,它的工作方式类似于 Java 迭代器并在需要时生成数字。 
如果我们需要多次迭代同一个序列,我们更喜欢 range(),因为 range 提供了一个静态列表。xrange() 每次都重建序列。xrange() 不支持切片和其他列表方法。xrange() 的优点是,当任务是在大范围内迭代时,它可以节省内存。 
在 Python 3.x 中,range 函数现在执行 Python 2.x 中的 xrange 功能,因此为了保持我们的代码可移植性,我们可能希望坚持使用范围。所以 Python 3.x 的 range 函数Python 2.x 的 xrange 。 
 

  • Python
for x in xrange(1, 5):
 
    print(x),
 
   
 
for x in range(1, 5):
 
    print(x),
 
   
 
'''
 
Output in Python 2.x
 
1 2 3 4 1 2 3 4
 
   
 
Output in Python 3.x
 
NameError: name 'xrange' is not defined
 
'''

错误处理:

Python 2.x和Python 3.x的区别:两个版本的错误处理都有细微的变化。在 python 3.x 中,'as' 关键字是必需的。

  • Python 2和Python3差异示例
try:
 
    trying_to_check_error
 
except NameError, err:
 
    print err, 'Error Caused'   # Would not work in Python 3.x
 
   
 
'''
 
Output in Python 2.x:
 
name 'trying_to_check_error' is not defined Error Caused
 
   
 
Output in Python 3.x :
 
File "a.py", line 3
 
    except NameError, err:
 
                    ^
 
SyntaxError: invalid syntax
 
'''
  • Python
try:
 
     trying_to_check_error
 
except NameError as err: # 'as' is needed in Python 3.x
 
     print (err, 'Error Caused')
 
   
 
'''
 
Output in Python 2.x:
 
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused')
 
   
 
Output in Python 3.x :
 
name 'trying_to_check_error' is not defined Error Caused
 
'''

__future__ 模块:

Python 2.x和Python 3.x的区别是什么?这基本上不是两个版本之间的区别,而是在这里提到的一个有用的东西。__future__ 模块的想法是帮助迁移到 Python 3.x。 
如果我们计划在 2.x 代码中支持 Python 3.x,我们可以在代码中使用_future_导入。 
Python 2和Python3有什么不同?例如,在下面的 Python 2.x 代码中,我们使用 __future__ 模块使用 Python 3.x 的整数除法行为。 

  • Python 2和Python3差异示例

# In below python 2.x code, division works
 
# same as Python 3.x because we use  __future__
 
from __future__ import division
 
   
 
print 7 / 5
 
print -7 / 5

输出 : 

1.4 

-1.4 

我们在 Python 2.x 中使用 __future__ 模块使用方括号的另一个示例: 

  • Python
from __future__ import print_function    
 
   
 
print('lsbin')

输出: lsbin

请参阅此为__future__模块的更多细节。 

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: