python2.7過渡到python3.6時遇到的差別總結

1.Python3中print爲一個函數,必須用括號括起來而Python2中print爲classpython

print('hello')函數

2.python3將raw_input和input進行了整合,只有input,輸入的爲str編碼

3.Python3中/表示真除,%表示取餘,//結果取整;Python2中帶上小數點/表示真除,%表示取餘,//結果取整code

4.python3中沒有了xrange,python3的range就是xrange,也就是說range()返回的再也不是一個列表而是一個class,若是要獲得列表須要list(),for i in range(3)照樣能夠這樣使用unicode

5.python3中字典類方法has_key()也再也不支持,用運算符處理,eg:'abc' in dic
字典的items()和keys()返回的再也不是一個列表而是一個class,須要用list()轉換爲list;字符串

6.字符串編碼
在Python2中,做爲兩種類型的字符序列,str與unicode須要轉換,它們是這樣轉換的.
str——decode方法——》unicode——encode方法——》strinput

u'string'          string

在python3中能夠這樣對應這轉換,配合上面的圖,也許會好理解一點。
byte——decode(解碼)方法——》str——>encode(編碼)方法——》byte 
it

b'string'    class

相關文章
相關標籤/搜索