不可變資料型別更改後位址發生變化,可變資料型別更改後位址不發生變化。
看**說話
a =
1#int
b ="string"
#string
c =(1,
2)#tuple
print
(type
(a),
id(a)
)print
(type
(b),
id(b)
)print
(type
(c),
id(c)
)a +=
1b +=
"1"c +=(3
,4)print
(a,aid(a)
)print
(b,id
(b))
print
(c,id
(c))
執行結果如下:
140721425453888
2794250914424
2794301510664
2 140721425453920
string1 2794303444504
(1, 2, 3, 4) 2794303362536
a =[1
,2]#list
b =#dict
c =#set
print
(type
(a),
id(a)
)print
(type
(b),
id(b)
)print
(type
(c),
id(c))3
)b[3]
="3"
c.add(3)
print
(a,id
(a))
print
(b,id
(b))
print
(c,id
(c))
執行結果如下:
2794303322632
2794303389200
2794302728904
[1, 2, 3] 2794303322632
2794303389200
2794302728904
3.結論
int、string、tuple 是不可變資料型別;
list、dict、set是可變資料型別。
Python 可變資料型別和不可變資料型別
python的資料儲存在記憶體裡,該資料分為可變資料型別和不可變資料型別 不可變資料型別 字串,數字,元祖 可變資料型別 列表,字典,集合 nums 1 2,3 nums bk nums nums 0 0print nums nums print nums bk nums bk print form...
python 可變資料型別 不可變資料型別
在python中,資料型別分為可變資料型別和不可變資料型別,不可變資料型別包括string,int,float,tuple,可變資料型別包括list,dict。所謂的可變與不可變,舉例如下 a test print a 0 t a 0 1 traceback most recent call las...
python 可變資料型別 不可變資料型別
在python中,資料型別分為可變資料型別和不可變資料型別,不可變資料型別包括string,int,float,tuple,可變資料型別包括list,dict。所謂的可變與不可變,舉例如下 a test print a 0 t a 0 1 traceback most recent call las...