標籤:strip() apply() print zip open python new reduce 改名
1、輸出
python2.+ 輸出: print ""
python3.+ 輸出: print ("")
2、開啟檔案
python2.+ 開啟檔案: file()或open()
python3.+ 開啟檔案: open()
3、thread模組
python2.+ 匯入thread模組: import thread
線程運行方法: thread.start_new_thread()
python3.+ 匯入thread模組: import _thread
線程運行方法: _thread.start_new_thread()
4、range和xrange
python2.+中 range(0,3)獲得一個list,在python3.+中必須顯示調用list(range(0,3))
python2.+中 xrange(0,4),在python3.+中改名為range(0,4)
5、python2.+中 raw_input,在python3.+中改名為input
6、python2.+中 long,在python3.+中改名為int
7、python2.+中 except Exception, e 在python3.+中改為except (Exception) as e
8、python3.+中zip()、map()和filter()都返回迭代器。
而python2.+中的apply()、 callable()、coerce()、 execfile()、reduce()和reload ()函數都被去除了
python3.+可以使用hasattr()來替換callable(),hasattr()的文法如:hasattr(string, ‘__name__‘)
python3.+可以使用self.func(*self.args)來替換apply(self.func,self.args)
9、python2.+中的<>,在python3.+中全部改用!=
10、python3.+關鍵詞加入as 和with,還有True,False,None
11、python3.+整型除法返回浮點數,1/2結果為0.5,要得到整型結果,使用//
12、python3.+加入nonlocal語句。使用noclocal x可以直接指派外圍(非全域)變數
13、輸入函數raw_input
python2.+輸入函數raw_input,python3.+中用input代替
14、python3.+中bytes對象不能hash,也不支援 b.lower()、b.strip()和b.split()方法,但對於後兩者可以使用 b.strip(b’
\n\t\r \f’)和b.split(b’ ‘)來達到相同目的
python2.+進化至python3.+ 文法變動差異(不定期更新)