python2.+進化至python3.+ 文法變動差異(不定期更新)

來源:互聯網
上載者:User

標籤: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.+ 文法變動差異(不定期更新)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.