Python 2.x和Python 3.x之间的区别?
在编码社区中始终存在着关于哪个版本的Python是最好的学习之争:Python2.x或Python3.x。
以下是pyton2.x和python3.x之间的主要区别
1.打印功能
在python2.x中,“print”被视为语句,而python3.x则将“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.整数除法
Python2将您键入的小数点后无任何数字的数字视为整数,这可能导致除法期间出现一些意外结果。例如,如果在Python2代码中键入表达式3/2,则计算结果将为1,而不是您期望的1.5。建议在您的python3代码中使用float(x)而不是仅使用x(以防python2的代码库端口),或者在python2脚本中使用__future__import分区。
#Python2
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
#Python3.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字符串
默认情况下,Python3将字符串存储为Unicode,而Python2需要将字符串标记为“u”(如果要将其存储为Unicode)。Unicode字符串比ASCII字符串更具通用性,后者是Python2的默认设置,因为它们可以存储外语字母,表情符号以及标准的罗马字母和数字。
#Python2
>>> 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#Python3
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'>
“字符串”+b“数据字节”将出现错误。
>>> 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 bytes4.引发异常
Python3需要不同的语法来引发异常。如果要向用户输出错误消息,则需要使用以下语法:
raise IOError(“your error message”)
上面的语法适用于python2和python3。
但是,以下代码仅适用于python2,不适用于python3
raise IOError, “your error message”
5.列表理解循环变量
在python2中,为在“for循环”中迭代的变量提供与全局变量相同的名称可能会导致更改全局变量的值-通常我们不希望这样做。此问题已在Python3中修复,因此您可以在“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
#Python3
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