Python 2.x和Python 3.x之间的重要区别(代码示例)

  • 部门运营商
  • 打印功能
  • 统一码
  • Xrange
  • 错误处理
  • _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 # Refer below link for details '''

打印功能
这是最著名的更改。在此, 打印Python 2.x中的关键字被替换为打印()Python 3.x中的函数。但是, 如果在后面加上空格, 则括号在Python 2中有效打印关键字, 因为解释器将其视为表达式。
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 Refer below link for details '''

如我们所见, 如果我们在python 2.x中使用括号, 那么就没有问题, 但是, 如果我们在python 3.x中不使用括号, 则会出现SyntaxError。
Unicode:
在Python 2中, 隐式str类型为ASCII。但是在Python 3.x中, 隐式str类型是Unicode。
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
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 3.x中不存在Python 2.x的xrange()。在Python 2.x中, range返回一个列表, 即range(3)返回[0, 1, 2], 而xrange返回一个xrange对象i。例如, xrange(3)返回与Java迭代器类似的迭代器对象, 并在需要时生成数字。
如果我们需要多次遍历同一序列, 则我们首选range(), 因为range提供了一个静态列表。 xrange()每次都会重新构造序列。 xrange()不支持切片和其他列表方法。 xrange()的优点是, 当任务要在较大范围内进行迭代时, 它可以节省内存。
在Python 3.x中, range函数现在执行xrange在Python 2.x中的作用, 因此为了使我们的代码具有可移植性, 我们可能想坚持使用range。因此, Python 3.x的range函数is来自Python 2.x的xrange。
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 3.x中, 必须使用" as"关键字。
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 '''

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__模块:
这基本上不是两个版本之间的区别, 而是在这里需要提及的有用内容。 __future__模块的想法是帮助迁移到Python3.x。
如果我们计划在我们的2.x代码中提供Python 3.x支持, 则可以使用_未来_在我们的代码中导入。
例如, 在下面的Python 2.x代码中, 我们通过__future__模块使用Python 3.x的整数除法行为。
# 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

另一个示例, 我们使用__future__模块在Python 2.x中使用方括号:
from __future__ import print_functionprint ( 'lsbin' )

【Python 2.x和Python 3.x之间的重要区别(代码示例)】输出如下:
lsbin

参考这个有关__future__模块的更多详细信息。
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读