版本說(shuō)明
Python 3.0在設(shè)計(jì)的時(shí)候沒(méi)有考慮向較早版本相容
Python 2.6作為一個(gè)過(guò)渡版本并齐,基本使用了Python 2.x的語(yǔ)法和庫(kù)蹦误,同時(shí)考慮了向Python 3.0的遷移辰如,允許使用部分Python 3.0的語(yǔ)法與函數(shù)嘱腥。
除非為了使用舊的Python2.x項(xiàng)目代碼或只支持2.x的第三方庫(kù)殿雪,否則不推薦使用2.x進(jìn)行編程
死人的print函數(shù)
Python 2.6與Python 2.7里面钧栖,以下三種形式是等價(jià)的:
print "fish"
print ("fish") #注意print后面有個(gè)空格
print("fish") #print()不能帶有任何其它參數(shù)
但python3.x只能使用后兩者嘿悬,print語(yǔ)句被python3廢棄实柠,只能使用print函數(shù)
Unicode
Python3中字符串是Unicode (utf-8)編碼,支持中文做標(biāo)識(shí)符善涨。
python2中是ASCII編碼窒盐,需要更改字符集才能正常支持中文,所以在.py文件中會(huì)看到#-- coding: UTF-8 --钢拧。
#python3中
>>> 中國(guó) = 'china'
>>>print(中國(guó))
china
#python2中
>>> str = "我愛(ài)北京天安門(mén)"
>>> str
'\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8'
>>> str = u"我愛(ài)北京天安門(mén)"
>>> str
u'\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8'
除法運(yùn)算
單斜杠/,Python2中整數(shù)相除得整數(shù)蟹漓,浮點(diǎn)小數(shù)相除得浮點(diǎn);Python3中結(jié)果總是浮點(diǎn)數(shù)娶靡。
#python3
>>print(10/5)
2.0
雙斜杠//,Python2和3相同牧牢,都是除法結(jié)果去掉小數(shù)部分
>>print(10//3)
3
異常處理
Python2中try:...except ERR,e:...,在Python3中改為了try:...except ERR as e:...
#Python3
try:
open('a.txt','r')
except Exception as e:
print(e) #這里也不要使用e.message
python 2中觸發(fā)異匙硕В可以用raise IOError, "file error"或raise IOError("file error")兩種方式塔鳍。
python 3中觸發(fā)異常只能用raise IOError("file error")。
異常StandardError 被Python3廢棄呻此,統(tǒng)一使用Exception
xrange和range
Python3中不再使用xrange方法轮纫,只有range方法
range在Python2中返回列表,而在Python3中返回range可迭代對(duì)象
a=range(10)
print(a)
print(list(a))
輸出
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
八進(jìn)制字面量
Python3中只能使用0o...格式焚鲜,對(duì)于01000格式將拋出錯(cuò)誤,而在Python2中兩種都能使用
>>> 01000
File "<stdin>", line 1
01000
^
SyntaxError: invalid token
>>> 0o1000
512
不等運(yùn)算符
在Python2中有兩個(gè)不等運(yùn)算符!=和<>掌唾,在Python3中去掉了<>放前,只有!=符號(hào)表示不等。
repr
在Python2中雙反引號(hào)``可以替代repr函數(shù)糯彬,在Python3中去掉了雙反引號(hào)的表是方法凭语,只能用repr方法
模塊改名
StringIO模塊現(xiàn)在被合并到新的io模組內(nèi)。 new, md5, gopherlib等模塊被刪除撩扒。
httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib被合并到http包內(nèi)似扔。
取消了exec語(yǔ)句,只剩下exec()函數(shù)搓谆。
long類型
在Python2中l(wèi)ong是比int取值范圍更大的整數(shù)炒辉,Python3中取消了long類型,int的取值范圍擴(kuò)大到之前的long類型范圍
bytes類型
Python3新增了bytes類型,使用b開(kāi)頭的字符串定義:
>>> b = b'china'
>>> type(b)
<type 'bytes'>
str對(duì)象和bytes對(duì)象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互轉(zhuǎn)化泉手。
>>> s = b.decode()
>>> s
'china'
>>> b1 = s.encode()
>>> b1
b'china'
dict類型
Python3中dict的.keys()黔寇、.items 和.values()方法返回迭代器,而之前的iterkeys()等函數(shù)都被廢棄斩萌。
同時(shí)去掉的還有 dict.has_key()缝裤,可以用in來(lái)代替。
di={
'a':1,
'b':2,
'c':3
}
for item in d.items():
print(item)
print('c' in di)
輸出
('gggg', {'a1': 1})
('b', 12)
True
next()函數(shù)和.next()方法
my_generator = (letter for letter in 'abcdefg')
python 2中可以用 next(my_generator) 和 my_generator.next() 兩種方式术裸。
python 3中只能用 next(my_generator)這種方式倘是。
列表推導(dǎo)
不再支持[n for n in a,b]
語(yǔ)法,改為[n for n in (a,b)]
或[n for n in [a,b]]
a=1
b=2
c=[n for n in [a,b]]
print(c)
輸出[1,2]
input
python 2 中通過(guò)input 輸入的類型是 int袭艺,只有通過(guò) raw_input()輸入的類型才是str.
python 3中通過(guò)input輸入的類型都是是str搀崭,去掉了row_input()方法。
比較符
Python 2 中任意兩個(gè)對(duì)象都可以比較猾编,11 < 'test'
返回True
Python 3中只有同一數(shù)據(jù)類型的對(duì)象可以比較瘤睹,11 < 'test'
報(bào)錯(cuò),需要調(diào)用正則判斷答倡,改為import re;11 < int('test') if re.compile('^[0-9]+$').match('test') else 0
否則就報(bào)錯(cuò)
其他
exec語(yǔ)句被python3廢棄轰传,統(tǒng)一使用exec函數(shù)
execfile語(yǔ)句被Python3廢棄,推薦使用exec(open("./filename").read())
Python3中這些方法不再返回list對(duì)象:dictionary關(guān)聯(lián)的keys()瘪撇、values()获茬、items(),zip()倔既,map()恕曲,filter(),但是可以通過(guò)list強(qiáng)行轉(zhuǎn)換
迭代器iterator的next()函數(shù)被Python3廢棄渤涌,統(tǒng)一使用next(iterator)
file函數(shù)被Python3廢棄佩谣,統(tǒng)一使用open來(lái)處理文件,可以通過(guò)io.IOBase檢查文件類型
apply函數(shù)被Python3廢棄
更多內(nèi)容請(qǐng)你在實(shí)踐中慢慢體會(huì)实蓬。