Python2與Python3的區別

Python2與Python3的區別

1) 核心類差別

  1. Python3 對 Unicode 字符的原生支持。
    Python2 中使用 ASCII 碼做爲默認編碼方式致使 string 有兩種類型 str 和 unicode,Python3 只
    支持 unicode 的 string。Python2 和 Python3 字節和字符對應關係爲:python

    python2 python3 表現 轉換 做用
    str bytes 字節 encode 存儲
    unicode str 字符 decode 顯示
  2. Python3 採用的是絕對路徑的方式進行 import
  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.mysql


2) 廢棄類差別

  1. print 語句被 Python3 廢棄,統一使用 print 函數web

  2. exec 語句被 python3 廢棄,統一使用 exec 函數redis

  3. execfile 語句被 Python3 廢棄,推薦使用 exec(open("./filename").read())sql

  4. 不相等操做符"<>"被 Python3 廢棄,統一使用"!="mongodb

  5. long 整數類型被 Python3 廢棄,統一使用 int數據庫

  6. xrange 函數被 Python3 廢棄,統一使用 range,Python3 中 range 的機制也進行修改並提升了大數據集生成效率windows

  7. Python3 中這些方法再再也不返回 list 對象:dictionary 關聯的 keys()、values()、items(),zip(),map(),filter(),可是能夠經過 list 強行轉換:centos

    mydict = dict()
    mydict = {"a": 1, "b": 2, "c": 3}
    mydict.keys()  # <built-in method keys of dict object at 0x000000000040B4C8>
    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


3)修改類差別

  1. 浮點數除法操做符「/」和「//」的區別
    「 / 」:
    Python2:若爲兩個整形數進行運算,結果爲整形,但若兩個數中有一個爲浮點數,則結果爲
    浮點數;
    Python3:爲真除法,運算結果再也不根據參加運算的數的類型。
    「//」:
    Python2:返回小於除法運算結果的最大整數;從類型上講,與"/"運算符返回類型邏輯一致。
    Python3:和 Python2 運算結果同樣。
  2. 異常拋出和捕捉機制區別

python2

raise IOError, "file error"  # 拋出異常
  except NameError, err:  # 捕捉異常

python3

raise IOError("file error")  #拋出異常
  except NameError as err: #捕捉異常
  1. for 循環中變量值區別

    Python2,for 循環會修改外部相同名稱變量的值

    i = 1
    print('comprehension: ', [i for i in range(5)])
    print('after:i = ', i)  # i = 4

    Python3,for 循環不會修改外部相同名稱變量的值

    i = 1
    print("comprehension: ", [i for i in range(5)])
    print('after: i = ', i)  # i = 1
  2. round 函數返回值區別

    Python2,round 函數返回 float 類型值

    isinstance(round(15.5),int)  #True

    Python3,round 函數返回 int 類型值

    isinstance(round(15.5),float) #True
  3. 比較操做符區別

    Python2 中任意兩個對象均可以比較

    11 < 'test'  # True

    Python3 中只有同一數據類型的對象能夠比較

    11 < 'test' # TypeError: unorderable types: int() < str()


4)第三方工具包差別

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

分類 工具名 用途
數據收集 scrapy 網頁採集,爬蟲
數據收集 scrapy-redis 分佈式爬蟲
數據收集 selenium web 測試,仿真瀏覽器
數據處理 beautifulsoup 網頁解釋庫,提供 lxml 的支持
數據處理 lxml xml 解釋庫
數據處理 xlrd excel 文件讀取
數據處理 xlwt excel 文件寫入
數據處理 xlutils excel 文件簡單格式修改
數據處理 pywin32 excel 文件的讀取寫入及複雜格式定製
數據處理 Python-docx Word 文件的讀取寫入
數據分析 numpy 基於矩陣的數學計算庫
數據分析 pandas 基於表格的統計分析庫
數據分析 scipy 科學計算庫,支持高階抽象和複雜模型
數據分析 statsmodels 統計建模和計量經濟學工具包
數據分析 scikit-learn 機器學習工具庫
數據分析 gensim 天然語言處理工具庫
數據分析 jieba 中文分詞工具庫
數據存儲 MySQL-python mysql 的讀寫接口庫
數據存儲 mysqlclient mysql 的讀寫接口庫
數據存儲 SQLAlchemy 數據庫的 ORM 封裝
數據存儲 pymsql sql server 讀寫接口庫
數據存儲 redis redis 的讀寫接口
數據存儲 PyMongo mongodb 的讀寫接口
數據呈現 matplotlib 流行的數據可視化庫
數據呈現 seaborn 美觀的數據但是湖庫,基於 matplotlib
工具輔助 chardet 字符檢查工具
工具輔助 ConfigParser 配置文件讀寫支持
工具輔助 requests HTTP 庫,用於網絡訪問


5)工具安裝問題

windows 環境
Python2 沒法安裝 mysqlclient。Python3 沒法安裝 MySQL-python、 flup、functools3二、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 環境下安裝

相關文章
相關標籤/搜索