Python2 和Python3的區別匯總

1.打印信息:
python2: print "hello world"
python3: print ("hello world")
2.除法運算:
Python 2 :print '3 / 2 =', 3 / 2 輸出:3 / 2 = 1
print '3 / 2.0 =', 3 / 2.0 輸出:3 / 2.0 = 1.5
Python 3: print('3 / 2 =', 3 / 2) 輸出: 3 / 2 = 1.5
print('3 / 2.0 =', 3 / 2.0) 輸出: 3 / 2.0 = 1.5
3.Unicode
Python 2有兩種字符串類型:str和unicode,
Python 3中的字符串默認就是Unicode,Python 3中的str相當于Python 2中的unicode。
在Python 2中,如果代碼中包含非英文字符,需要在代碼文件的最開始聲明編碼,如下 # -- coding: utf-8 --
在Python 3中,默認的字符串就是Unicode,就省去了這個麻煩,下面的代碼在Python 3可以正常地運行
a = "你好" print(a)

其他差異后續補充。。。。。。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容