python2與python3的區別
- 2020 年 1 月 9 日
- 筆記
說明:標註?????是暫時沒遇到且看不懂的,做個標記。常見的區別有print,range,open,模組改名,input,整除/,異常 except A as B
為了不帶入過多的累贅,Python 3.0在設計的時候沒有考慮向下相容。過渡版本Python2.6基本使用了Python 2.x的語法和庫,同時考慮了向Python 3.0的遷移,允許使用部分Python 3.0的語法與函數。
目錄
- 新增nonlocal在閉包中改變臨時變數
python2沒有nonlocal關鍵字,要修改臨時變數只能將其改成可變數據類型,如數組。b=[a]
- print加()
print()函數代替print語句
- Unicode編碼存儲字元串
Python 3加入 Unicode 字元串,用以編碼存儲字元串。比如用 utf-8可以用來輸入中文
- 數據類型新增bytes
Python 3去掉long類型,新增了bytes。可以看成是「位元組數組」對象,每個元素是 8-bit 的位元組,取值範圍 0~255。
在 python 3中字元串以 unicode 編碼存儲,當寫入二進位文件時,字元串無法直接寫入writr(或讀取),必須以某種方式的編碼(encode編碼/decode解碼)為位元組序列後,方可寫入。換句話說str類型的字元串無法write進文件,要將str字元串encode為bytes才能write
a='asdfg' print(type(a)) b=a.encode() print(type(b)) c=b'qwe' print(type(c)) d=c.decode() print(type(d))
運行結果:
<class 'str'> <class 'bytes'> <class 'bytes'> <class 'str'>
注意:type類型的數據不能當成普通的int型去修改
s="ABCD" b=s.encode("gbk") print (b[0]) # 輸出65 b[0] = 66 #TypeError: 'bytes' object does not support item assignment
- 除法/不需要轉float
Python 3整數之間做除法可以得到浮點數的結果,不需要進行數據格式轉換1/2=0.5
Python 2整數int間除法結果為把運算結果去尾的整數1/2=0,3/2.0=1.5
- 異常捕獲 加as
Python 3 中 except exc as var
Python 2中 except exc , var
?????????????

- range
Python 3 中 range()
Python 2 中 xrange()
?????????????


- 八進位表示 只能0o1000
Python 2 中 0o1000 或者01000
Python 3 中 只能0o1000
- 不等運算符 只能!=
Python 2 中 != 或者<>
Python 3 中 只能!=
- 去掉了repr表達式“
?????????????

- 模組改名
????????????????

執行緒模組:Python 2 中 thread,Python 3 中_thread
- 字典的關鍵字 用屬性代替函數
Python 3去掉iterkeys()、 dict.has_key(),用.keys()、.items 和.values()方法返回迭代器
dict1={'a':1,"b":2,"c":3} print(dict1.keys()) print(dict1.values()) print(dict1.items())
運行結果:
dict_keys(['a', 'b', 'c']) dict_values([1, 2, 3]) dict_items([('a', 1), ('b', 2), ('c', 3)])
- 從鍵盤鍵入字元串input
Python 2 中 raw_input("提示資訊")用以輸入字元串 ; input()用以輸入數字
Python 3 中input("提示資訊")將所有輸入默認為字元串
- map、filter、reduce
Python 2 中 map、filter是內置函數,輸出為列表
Python 3 中 map、filter是類,返回可迭代的對象,可用next()進行迭代
「對於比較高端的 reduce 函數,它在 Python 3.x 中已經不屬於 built-in 了,被挪到 functools 模組當中。」
- 打開文件 open
Python 2 中 file(。。。)或oen(。。。)
Python 3 中 只能open(。。。)
- chr( K ) 與 ord( c )的範圍
???????????????
python 2.4.2以前
chr( K ) 將編碼K 轉為字元,K的範圍是 0 ~ 255
ord( c ) 取單個字元的編碼, 返回值的範圍: 0 ~ 255
python 3.0
chr( K ) 將編碼K 轉為字元,K的範圍是 0 ~ 65535
ord( c ) 取單個字元的編碼, 返回值的範圍: 0 ~ 65535
- 位元組數組對象bytearry
python3新增,將多個bytes位元組類型數據組成數組。
(1) 初始化
a = bytearray( 10 )
# a 是一個由十個位元組組成的數組,其每個元素是一個位元組,類型借用 int
# 此時,每個元素初始值為 0
(2) 位元組數組 是可變的
a = bytearray( 10 )
a[0] = 25
# 可以用賦值語句更改其元素,但所賦的值必須在 0 ~ 255 之間
(3) 位元組數組的切片仍是位元組數組
(4) 字元串轉化為位元組數組
#coding=gbk
s ="你好"
b = s.encode( "gbk") # 先將字元串按某種「GBK」編碼方式轉化為 bytes
c = bytearray( b ) #再將 bytes 轉化為 位元組數組
也可以寫作
c = bytearray( "你好", "gbk")
(5) 位元組數組轉化為字元串
c = bytearray( 4 )
c[0] = 65 ; c[1]=66; c[2]= 67; c[3]= 68
s = c.decode( "gbk" )
print ( s )
# 應顯示: ABCD
(6) 位元組數組可用於寫入文本文件
#coding=gbk
f = open("c:\1234.txt", "wb") s = "張三李四abcd1234" # ——————————- # 在 python2.4 中我們可以這樣寫: # f.write( s ) # 但在 python 3.0中會引發異常 # ——————————- b = s.encode("gbk")
f.write( b ) c=bytearray( "王五","gbk") f.write( c ) f.close()
input("?")
參考網址:
http://www.runoob.com/python/python-2x-3x.html
https://www.cnblogs.com/hanggegege/p/5840005.html