Python 2.7.x 和 Python 3.x 的主要區別是什麼?


Python 3.0 於 2008 年 12 月釋出。它的設計是為了糾正早期版本中的一些缺陷。Python 3 的指導原則是:“透過移除舊的做事方法來減少功能重複”。Python 3.0 不提供向後相容性。這意味著使用 2.x 語法編寫的 Python 程式不能在 python 3.x 直譯器下執行。2.7 是 Python 2.x 系列中的最後一個主要版本。

儘管這兩個版本在用法上存在相當多的差異,但最明顯的差異如下所示:

print 在 Python 2.7 中是關鍵字,但在 Python 3.x 中被包含為內建函式。因此,在 Python 3 程式碼中使用它時,括號是必須的。

print “Hello World” # is acceptable in Python 2 but not in Python 3
print (“Hello World”) #acceptable in Python 2 and Python 3

raw_input() —— Python 2.7 中的函式已被棄用。input() 函式只將接收到的資料視為字串。

整數除法 —— Python 3 中的功能已更改。在 Python 2.x 中,5/2 的結果為 2,但在 Python 3.x 中,5/2 為 2.5。

UNICODE —— 在 Python 3.x 中,字串預設是 Unicode。在 Python 2.x 中,字串必須透過在其前面加上 ‘u’ 來顯式定義為 Unicode(例如 u’hello’)。

長整數 —— 在 Python 3.x 中,整數物件預設是長整數。在 Python 2.x 中,整數必須字尾 L(例如 100L)。

更新於:2019-07-30

172 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告