1.python2中print是一個(gè)語(yǔ)句,可以帶括號(hào)或不帶括號(hào)輸出。而python 3中print是一個(gè)函數(shù),必須帶括號(hào)輸出。
2. 整數(shù)除法的區(qū)別。
? ?python 2中? 3/2 = 1? ?3//2 = 1? ?3/2.0=1.5? ?3//2.0 = 1.0
? ?python 3中? 3/2 = 1.5? ?3//2 = 1? ?3/2.0=1.5? ?3//2.0 = 1.0
3.python 2中有xrange()和range(). python 3中,range()的實(shí)現(xiàn)方式和2中的xrange()的實(shí)現(xiàn)方式一致。故3中只有range()沒有xrange()。另外,3中的range對(duì)象有__contains__方法。所以在3中查找整數(shù)或布爾型速度會(huì)加快很多。
x = 10000000
def val_in_range(x, val):
????return val in range(x)
%timeit val_in_range(x, x/2)? #?742 ms per loop
%timeit val_in_range(x, x//2) #?1.19 μs per loop
4.觸發(fā)異常的區(qū)別。
?python 2中觸發(fā)異??梢杂胷aise IOError, "file error"或raise IOError("file error")兩種方式。
python 3中觸發(fā)異常只能用raise IOError("file error")。
5.異常處理的區(qū)別。
python 2中異常處理用except NameError, err:
python 3中異常處理用except NameError as err:
6.next()函數(shù)和.next()方法的區(qū)別
my_generator = (letter for letter in 'abcdefg')
python 2中可以用? next(my_generator) 和?my_generator.next() 兩種方式。
python 3中只能用?next(my_generator)這種方式。
7.for循環(huán)變量與全局命名空間泄漏
在python 2中 for中的循環(huán)變量會(huì)影響到全局。如下:
i = 1
print 'before: i =', i? ? #輸出 1
print 'comprehension: ', [i for i in range(5)]? #輸出[0,1,2,3,4]
print 'after: i =', i #輸出4
在python 3中循環(huán)控制變量不會(huì)再泄漏到循環(huán)周圍的空間中了。同時(shí),列表推導(dǎo)不再支持[... for var in item1, item2, ...]這樣的語(yǔ)法,使用[... for var in (item1, item2, ...)]代替。如下:
i = 1
print 'before: i =', i? ? #輸出 1
print 'comprehension: ', [i for i in range(5)]? #輸出[0,1,2,3,4]
print 'after: i =', i #輸出1
8.input的區(qū)別
python 2 中通過(guò)input 輸入的類型是 int,只有通過(guò) raw_input()輸入的類型才是str.
python 3中通過(guò)input輸入的類型是str。如
python 2中:
my_input = input('enter a number: ')
enter a number: 123
type(my_input)? ?# 輸出<type 'int'>
my_input = raw_input('enter a number: ')
enter a number: 123
type(my_input)? ?#輸出<type 'str'>
python 3中:
aa=input('enter a number:')
enter a number:12
type(aa) #輸出<class 'str'>
9 可迭代對(duì)象與列表
python 2中某些函數(shù)和方法返回列表,但在3中返回可迭代對(duì)象。需要通過(guò)list()函數(shù)將可迭代對(duì)象轉(zhuǎn)為列表。如下:
python 2中
print range(3) #輸出[0,1,2]
print type(range(3)) #輸出 <type 'list'>
python 3中
print range(3) #輸出range(0,3)
print type(range(3)) #輸出<class 'range'>
print(list(range(3)))??#輸出[0,1,2]
10.比較操作符
Python 2 中任意兩個(gè)對(duì)象都可以比較
11 < 'test'? #True
Python 3中只有同一數(shù)據(jù)類型的對(duì)象可以比較
11 < 'test'? # TypeError: unorderable types: int() < str()
11.廢棄類差異
廢棄類差異
print語(yǔ)句被python3廢棄,統(tǒng)一使用print函數(shù)
exec語(yǔ)句被python3廢棄,統(tǒng)一使用exec函數(shù)
execfile語(yǔ)句被Python3廢棄,推薦使用exec(open("./filename").read())
不相等操作符"<>"被Python3廢棄,統(tǒng)一使用"!="
long整數(shù)類型被Python3廢棄,統(tǒng)一使用int
xrange函數(shù)被Python3廢棄,統(tǒng)一使用range,Python3中range的機(jī)制也進(jìn)行修改并提高了大數(shù)據(jù)集生成效率
Python3中這些方法不再返回list對(duì)象:dictionary關(guān)聯(lián)的keys()、values()、items(),zip(),map(),filter(),但是可以通過(guò)list強(qiáng)行轉(zhuǎn)換:
mydict={"a":1,"b":2,"c":3}
mydict.keys()? #
list(mydict.keys()) #['a', 'c', 'b']
迭代器iterator的next()函數(shù)被Python3廢棄,統(tǒng)一使用next(iterator)
raw_input函數(shù)被Python3廢棄,統(tǒng)一使用input函數(shù)
字典變量的has_key函數(shù)被Python廢棄,統(tǒng)一使用in關(guān)鍵詞
file函數(shù)被Python3廢棄,統(tǒng)一使用open來(lái)處理文件,可以通過(guò)io.IOBase檢查文件類型
apply函數(shù)被Python3廢棄
異常StandardError 被Python3廢棄,統(tǒng)一使用Exception