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