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”(例如 u'hello')來顯式地將其定義為 Unicode。
長整數 - 在 Python 3.x 中,整數物件預設為長整數。在 Python 2.x 中,必須在整數後新增 L(例如 100L)。
廣告