字典 dict 符號{} 大括號 花括號 無序
1:能夠存在空字典a={}
2:字典裏面數據存儲的方式: key:value
3:字典裏面value能夠包含任何類型的數據
4:字典裏面的元素 根據逗號來進行分隔
5:字典裏面的key必須是惟一的python
1 a={"class":"python11", 2 "student":119, 3 "teacher":"girl", 4 "t_age":20, 5 "score":[99,88.8,100.5]} 6 7 # 字典取值:字典[key] 8 print(a["t_age"]) 9 <<< 20
刪除 pop(key) 指明刪除的值的keyspa
1 a={"class":"python11", 2 "student":119, 3 "teacher":"girl", 4 "t_age":20, 5 "score":[99,88.8,100.5]} 6 res=a.pop("teacher") # 刪除值"girl" 7 print(res) 8 <<< girl 9 10 print(a) 11 <<< {'student': 119, 12 'class': 'python11', 13 'score': [99, 88.8, 100.5], 14 't_age': 20}
修改 a[已存在的key]=新value key是字典中已存在的code
1 a = { 2 'student': 119, 3 'class': 'python11', 4 'score': [99, 88.8, 100.5], 5 't_age': 20} 6 # 修改t_age對應的值,要保證t_age是已存在的key 7 a["t_age"]=18 8 print(a) 9 <<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18}
新增 a[不存在的key] = 新value key是字典中不存在的blog
1 a = { 2 'student': 119, 3 'class': 'python11', 4 'score': [99, 88.8, 100.5], 5 't_age': 20} 6 # 新增name對應的值,要保證name是不存在的key 7 a["name"]="monica" 8 print(a) 9 <<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18,'name':"monica"}