Python 2.x 和 Python 3.x 的区别?
编程社区一直存在关于学习哪个 Python 版本最佳的争论:Python 2.x 还是 Python 3.x。
以下是 Python 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” 将会报错。
>>> 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. 抛出异常
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
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP