Python 2.x 和 Python 3.x 之间的区别?

pythonserver side programmingprogramming

在编码社区中,关于哪个 Python 版本最适合学习一直存在争议:Python 2.x 还是 Python 3.x。

以下是 pyton 2.x 和 python 3.x 之间的主要区别

1. print 函数

在 python 2.x 中,"print"被视为语句,而 python 3.x 明确将"print"视为函数。这意味着我们需要以标准方式将 print 中的项目传递给函数括号,否则您将收到语法错误。

#Python 2.7

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'some more text here'

输出

Python 2.7.6
Hello, World!
Hello, World!
text print some more text here
Python 3
import sys
print("Python version is %s.%s.%s" %sys.version_info[:3])
print('Hello, World!')

print("some text,", end="")
print('some more text here')

输出

Python version is 3.6.1
Hello, World!
some text,some more text here
>>> print "Hello"
Syntax Error: Missing parentheses in call to 'print'

2. 整数除法

Python 2 将您输入的在小数点后没有任何数字的数字视为整数,这可能导致除法期间出现一些意外结果。例如,如果您在 Python 2 代码中输入表达式 3 / 2,则评估结果将为 1,而不是您预期的 1.5。建议在您的 Python 3 代码中使用 float(x) 而不是仅使用 x(以防代码库移植到 Python 2)或在 Python 2 脚本中使用 from __future__ import division。

#Python 2

print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0

输出

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

#Python 3.6.1

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)

输出

Python 3.6.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

3. Unicode 字符串

默认情况下,Python 3 将字符串存储为 Unicode,而 Python 2 要求您用"u"标记字符串(如果您想将其存储为 Unicode)。Unicode 字符串比 Python 2 默认的 ASCII 字符串更通用,因为它们可以存储外语字母以及表情符号和标准罗马字母和数字。

#Python 2

>>> print type(unicode('this is like a python3 str type'))
<type 'unicode'>
>>> print type(b'byte type does not exist')
<type 'str'>
>>> print 'they are really' + b' the same'
they are really the same

#Python 3

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
print('strings are now utf-8 \u03BCnico\u0394é!')
print('Python %s.%s.%s' %sys.version_info[:3], end="")
print(' has', type(b' bytes for storing data'))
print('Python %s.%s.%s' %sys.version_info[:3], end="")
print(' also has', type(bytearray(b'bytearrays')))

输出

Python 3.6.1
strings are now utf-8 μnicoΔé!
Python 3.6.1 has <class 'bytes'>
Python 3.6.1 also has <class 'bytearray'>

“string” + b”bytes of data” will through an error.

>>> print ('they are really' + b' the same')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print ('they are really' + b' the same')
TypeError: must be str, not bytes

4. 引发异常

Python 3 需要不同的语法来引发异常。如果要向用户输出错误消息,则需要使用语法 −

raise IOError(“your error message”)

上述语法适用于 Python 2 和 Python 3。

但是,以下代码仅适用于 Python 2,不适用于 Python 3

raise IOError, “your error message”

5. 列表理解循环变量

在 Python 2 中,为在“for 循环”中迭代的变量赋予与全局变量相同的名称可能会导致全局变量的值被更改 –这通常是我们不希望发生的。此问题已在 Python 3 中得到修复,因此您可以在"for 循环"中使用已用于控制变量的变量名,而不必担心它会泄漏并弄乱其余代码中的变量值。

#Python 2
print 'Python', python_version()
i = 1
print 'before: i =', i
print 'comprehension: ', [i for i in range(5)]
print 'after: i =', i

输出

Python 2.7.6
before: i = 1
comprehension: [0, 1, 2, 3, 4]
after: i = 4

#Python 3

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
i = 1
print('before: i =', i)
print('comprehension:', [i for i in range(5)])
print('after: i =', i)

输出

Python 3.6.1
before: i = 1
comprehension: [0, 1, 2, 3, 4]
after: i = 1

相关文章