字典 dict 符号{} 大括号 花括号 无序
1:可以存在空字典a={}
2:字典里面数据存储的方式: key:value
3:字典里面value可以包含任何类型的数据
4:字典里面的元素 根据逗号来进行分隔
5:字典里面的key必须是唯一的
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) 指明删除的值的key
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是字典中已存在的
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是字典中不存在的
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"}