天天看點

Python3.x與2.x版本差別資料類型未完待續…

Python3.x與2.x版本差別

  • 資料類型
  • 未完待續......

資料類型

  1. Py3.X去除了long類型,現在隻有一種整型——int,但它的行為就像2.X版本的long
  2. 新增了bytes類型,對應于2.X版本的八位串,定義一個bytes字面量的方法如下:
>>> b = b'china' 
>>> type(b) 
<type 'bytes'> 
           

str對象和bytes對象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法互相轉化。

>>> s = b.decode() 
>>> s 
'china' 
>>> b1 = s.encode() 
>>> b1 
b'china' 
           
  1. dict的.keys()、.items 和.values()方法傳回疊代器,而之前的iterkeys()等函數都被廢棄。同時去掉的還有dict.has_key(),用 in替代它吧 。

未完待續…

繼續閱讀