Python2.7與3.6的一些區別
阿新 • • 發佈:2018-10-18
3.5 網絡 可能 del break 接收 byte lse pyc
2.7實現了一部分3的功能, 更早版本可能會稍稍涉及一點
首先是關鍵字的差別
python3.6
import keyword print(keyword.kwlist) [‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
python2.7
import keyword keyword.kwlist [‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
python2.4
[‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘]
這麽多看不過來了把, 哈哈
python36 = {‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘} python27 = {‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘,‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,‘with‘, ‘yield‘} python24={‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘} print(python36-python27) # {‘nonlocal‘, ‘False‘, ‘True‘, ‘None‘} print(python36-python24) # {‘False‘, ‘nonlocal‘, ‘None‘, ‘as‘, ‘with‘, ‘True‘}
最主要的就是這個nonlocal了, python2中存在False, True, None但是不屬於關鍵字
語法差異
異常:
# python3 try: raise IndexError("傻傻") except IndexError as e: print(e) # python2 try: raise IndexError("傻傻") except IndexError , e: print(e)
其他
yield from # 好像是3.4 async和await # 3.5
數據類型
在python3中str是Unicode編碼的字節, bytes是其他字節
在python2中str默認是ascii編碼的字節, Unicode是另一種類型
# 在pycharm中點擊bytes bytes = str
bytes: 就是八個二進制的字節, 我們存儲和在網絡傳輸中實際上都是字節
Unicode, GBK, ASCII, UTF8都是將字節翻譯成有用的信息, Unicode可以存儲所有文字, 可以用他來做一個中間編碼來轉換其他的編碼
python3
# 轉成字節 a = "爸爸" byte = a.encode("utf8") print(byte, type(byte)) byte = bytes(a, encoding="utf8") print(byte, type(byte)) # 轉成字符串 st = byte.decode("utf8") print(st, type(st)) st = str(byte, encoding="utf8") print(st, type(st))
python2
# _*_ coding:utf8 _*_ # 轉成Unicode, 這裏用utf8轉是因為上面寫的 nui = "我是你爸爸".decode("utf8") print nui, type(nui) # 轉成字符串 st = nui.encode("utf8") print st, type(st)
其他
輸入
# python3 input() -> str # python2 raw_input() -> str python2中也有input不過他只能接收數字
輸出
# python3 print() # python2 print
關於數字
# <> 運算符在三中被棄用, 比較大多用於數字, 所以就放在這裏 # python3中range生成一個數字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器 # 3中棄用了long長整型 # 2中/是整除, 3中則不是, 3中的整除是//
Python2.7與3.6的一些區別