Python 2之间的重要区别。x和Python 3。x举例说明

除法运算符

null

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

python

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被替换为 打印() 函数在Python3中使用。x、 然而,在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
Refer below link for details
'''


我们可以看到,如果我们在python 2中使用括号。那么没有问题,但是如果我们在Python3中不使用括号。x、 我们得到了SyntaxError。

Unicode:

在Python 2中,隐式str类型是ASCII。但是在Python3中。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

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:

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

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 3中。x、 “as”关键字是必需的。

python

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 3。十、 如果我们计划使用Python 3。x支持我们的2。x代码,我们可以使用 _未来_ 在我们的代码中导入。 例如,在Python 2中。在下面的x代码中,我们使用Python 3。x的整数除法行为使用_future__模块。

python

# 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使用未来模块:

python

from __future__ import print_function
print ( 'GeeksforGeeks' )


输出:

GeeksforGeeks 

提到 有关_未来_模块的更多详细信息。

本文由 阿比特·阿加瓦尔 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享