1、字典增加键值
Dic = {'Age':18, 'Name':'Jie', 'Sex':'male'} #增 Dic ['High'] = 160 print(Dic)
2、字典键值修改
Dic = {'Age':18, 'Name':'Jie', 'Sex':'male'} #增 Dic ['High'] = 160 print(Dic) #改 Dic ['Age'] = 100 print(Dic)
3、字典Setdefault(有键值不做任何改变,无增加)
Dic = {'Age':18, 'Name':'Jie', 'Sex':'male'} #增 Dic ['High'] = 160 print(Dic) Dic ['Age'] = 100 print(Dic) Dic.setdefault('Weight') /**无增加**/ print(Dic)
Dic = {'Age':18, 'Name':'Jie', 'Sex':'male'} #增 Dic ['High'] = 160 print(Dic) Dic ['Age'] = 100 print(Dic) Dic.setdefault('Weight') print(Dic) Dic.setdefault('Name','SB') print(Dic)
-----------------------------------------------------
{'Age': 18, 'Name': 'Jie', 'Sex': 'male', 'High': 160}
{'Age': 100, 'Name': 'Jie', 'Sex': 'male', 'High': 160}
{'Age': 100, 'Name': 'Jie', 'Sex': 'male', 'High': 160, 'Weight': None}
{'Age': 100, 'Name': 'Jie', 'Sex': 'male', 'High': 160, 'Weight': None}
标签:Jie,Name,Python,Age,Day5,Dic,print,160,字典 From: https://www.cnblogs.com/leishu/p/16842315.html