Python2與Python3的區別詳解整理

一、核心類差異

1. Python3 對 Unicode 字符的原生支持。
Python2 中使用 ASCII 碼作爲默認編碼方式導致 string 有兩種類型 str 和 unicode,Python3 只
支持 unicode 的 string。Python2 和 Python3 字節和字符對應關係爲:
2. Python3 採用的是絕對路徑的方式進行 import
Python2 中相對路徑的 import 會導致標準庫導入變得困難(想象一下,同一目錄下有 file.py,如
何同時導入這個文件和標準庫 file)。Python3 中這一點將被修改,如果還需要導入同一目錄的文件必
須使用絕對路徑,否則只能使用相關導入的方式來進行導入。
3. Python2 中存在老式類和新式類的區別,Python3 統一採用新式類。新式類聲明要求繼承 object,
必須用新式類應用多重繼承。
4. Python3 使用更加嚴格的縮進。Python2 的縮進機制中,1 個 tab 和 8 個 space 是等價的,所
以在縮進中可以同時允許 tab 和 space 在代碼中共存。這種等價機制會導致部分 IDE 使用存在問題。
Python3 中 1 個 tab 只能找另外一個 tab 替代,因此 tab 和 space 共存會導致報錯:TabError:
inconsistent use of tabs and spaces in indentation.


二、廢棄類差異

1. print 語句被 Python3 廢棄,統一使用 print 函數
2. exec 語句被 python3 廢棄,統一使用 exec 函數
3. execfile 語句被 Python3 廢棄,推薦使用 exec(open("./filename").read())
4. 不相等操作符"<>"被 Python3 廢棄,統一使用"!="
5. long 整數類型被 Python3 廢棄,統一使用 int
6. xrange 函數被 Python3 廢棄,統一使用 range,Python3 中 range 的機制也進行修改並提高
了大數據集生成效率
7. Python3 中這些方法再不再返回 list 對象:dictionary 關聯的 keys()、values()、items(),zip(),
map(),filter(),但是可以通過 list 強行轉換:
1. mydict={"a":1,"b":2,"c":3}
2. mydict.keys() #<built-in method keys of dict object at 0x000000000040B4C8>
3. list(mydict.keys()) #['a', 'c', 'b']
8. 迭代器 iterator 的 next()函數被 Python3 廢棄,統一使用 next(iterator)
9. raw_input 函數被 Python3 廢棄,統一使用 input 函數
10. 字典變量的 has_key 函數被 Python 廢棄,統一使用 in 關鍵詞
11. file 函數被 Python3 廢棄,統一使用 open 來處理文件,可以通過 io.IOBase 檢查文件類型
12. apply 函數被 Python3 廢棄
13. 異常 StandardError 被 Python3 廢棄,統一使用 Exception


三 、修改類差異

1. 浮點數除法操作符“/”和“//”的區別
“ / ”:
Python2:若爲兩個整形數進行運算,結果爲整形,但若兩個數中有一個爲浮點數,則結果爲
浮點數;
Python3:爲真除法,運算結果不再根據參加運算的數的類型。
“//”:
Python2:返回小於除法運算結果的最大整數;從類型上講,與"/"運算符返回類型邏輯一致。
Python3:和 Python2 運算結果一樣。
2. 異常拋出和捕捉機制區別
Python2
1. raise IOError, "file error" #拋出異常
2. except NameError, err: #捕捉異常
Python3
1. raise IOError("file error") #拋出異常
2. except NameError as err: #捕捉異常
3. for 循環中變量值區別
Python2,for 循環會修改外部相同名稱變量的值
1. i = 1
2. print ('comprehension: ', [i for i in range(5)])
3. print ('after: i =', i ) #i=4
Python3,for 循環不會修改外部相同名稱變量的值
1. i = 1
2. print ('comprehension: ', [i for i in range(5)])
3. print ('after: i =', i ) #i=1
4. round 函數返回值區別
Python2,round 函數返回 float 類型值
1. isinstance(round(15.5),int) #True
Python3,round 函數返回 int 類型值
1. isinstance(round(15.5),float) #True
5. 比較操作符區別
Python2 中任意兩個對象都可以比較
1. 11 < 'test' #True
Python3 中只有同一數據類型的對象可以比較
1. 11 < 'test' # TypeError: unorderable types: int() < str()


四、第三方工具包差異

我們在 pip 官方下載源 pypi 搜索 Python2.7 和 Python3.5 的第三方工具包數可以發現,Python2.7
版本對應的第三方工具類目數量是 28523,Python3.5 版本的數量是 12457,這兩個版本在第三方工具
包支持數量差距相當大。
我們從數據分析的應用角度列舉了常見實用的第三方工具包(如下表),並分析這些工具包在
Python2.7 和 Python3.5 的支持情況

 

五、工具安裝問題

windows 環境
Python2 無法安裝 mysqlclient。Python3 無法安裝 MySQL-python、 flup、functools32、
Gooey、Pywin32、 webencodings。
matplotlib 在 python3 環境中安裝報錯:The following required packages can not be
built:freetype, png。需要手動下載安裝源碼包安裝解決。
scipy 在 Python3 環境中安裝報錯,numpy.distutils.system_info.NotFoundError,需要自己手
工下載對應的安裝包,依賴 numpy,pandas 必須嚴格根據 python 版本、操作系統、64 位與否。運行
matplotlib 後發現基礎包 numpy+mkl 安裝失敗,需要自己下載,國內暫無下載源
centos 環境下
Python2 無法安裝 mysql-python 和 mysqlclient 包,報錯:EnvironmentError: mysql_config not
found,解決方案是安裝 mysql-devel 包解決。使用 matplotlib 報錯:no module named _tkinter,
安裝 Tkinter、tk-devel、tc-devel 解決。
pywin32 也無法在 centos 環境下安裝。
 

六、切換

電腦共存python2和pyhthon3,把安裝目錄下的python.exe文件改python2.exe和python3.exe,這樣在調用的之後直接輸入對應版本的就可。

 

發佈了238 篇原創文章 · 獲贊 647 · 訪問量 1072萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章