【零基础入门Python】Python 2.x 和 Python 3.x 之间的重要区别及示例

面向读者:所有人

所属专栏:Python零基础入门教程

Python 2.x 和 Python 3.x 之间的差异

Python 除法运算符

Python 中的打印函数

Python 中的统一码

两个版本中的 xrange 与 range()

错误处理

Python 中的 __future__ 模块

在本文中,我们将通过一些示例了解 Python 2.x 和 Python 3.x 之间的一些重要区别。

Python 2.x 和 Python 3.x 之间的差异

在这里,我们将看到以下库和模块的差异:

  • Division operator
  • print function
  • Unicode
  • xrange
  • Error Handling
  • _future_ module

Python 除法运算符

如果我们要移植代码或在 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 中的打印函数

    这是最广为人知的变化。在此, Python 2.x 中的print 关键字被 Python 3.x 中的 print() 函数取代。但是,如果在 print 关键字后面添加空格,则括号在 Python 2 中起作用,因为解释器将其计算为表达式。您可以参阅打印单个多变量 Python以获取有关此主题的更多信息。
    在这个例子中,我们可以看到,如果我们在 python 2.x 中不使用括号,那么就没有问题,但如果我们在 python 3.x 中不使用括号,我们会得到 SyntaxError。 

  • 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 中的统一码

    在 Python 2 中,隐式 str 类型是 ASCII。但在 Python 3.x 中,隐式 str 类型是 Unicode。在此示例中,借助代码以及 Python 注释中的输出显示了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 

  • 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 与 range()

    Python 2.x 的 xrange() 在 Python 3.x 中不存在。在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 中的 range() 与 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 中,“as”关键字是可选的。 

try:
	trying_to_check_error
except NameError, err:
# Would not work in Python 3.x
	print err, 'Error Caused'

'''
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
'''

在此示例中,显示了未使用“as”关键字的错误处理。

try:
	trying_to_check_error
except NameError as err:
	print(err, 'Error Caused')

# code without using as keyword
try:
	trying_to_check_error
	
# 'as' is optional in Python 3.10
except NameError:
	print('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
'''

Python 中的 __future__ 模块

这基本上不是两个版本之间的区别,而是在这里提到的一个有用的东西。__future__ 模块的想法是帮助迁移到 Python 3.x。 
如果我们计划在 2.x 代码中支持 Python 3.x,则可以在代码中 使用__future__导入。例如,在下面的 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

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

from __future__ import print_function	
print('helloworld')

输出: 

helloworld

猜你喜欢

转载自blog.csdn.net/arthas777/article/details/133472382