字典 dict 符号{} 大括号 花括号 无序
1:可以存在空字典a={}
2:字典里面数据存储的方式: key:value
3:字典里面value可以包含任何类型的数据
4:字典里面的元素 根据逗号来进行分隔
5:字典里面的key必须是唯一的
a={"class":"python11",
"student":119,
"teacher":"girl",
"t_age":20,
"score":[99,88.8,100.5]}
# 字典取值:字典[key]
print(a["t_age"])
<<<<< 20
删除 pop(key) 指明删除的值的key
a={"class":"python11",
"student":119,
"teacher":"girl",
"t_age":20,
"score":[99,88.8,100.5]}
res=a.pop("teacher") # 删除值"girl"
print(res)
<<<<< girl
print(a)
<<<<< {'student': 119,
'class': 'python11',
'score': [99, 88.8, 100.5],
't_age': 20}
修改 a[已存在的key]=新value key是字典中已存在的
a = {
'student': 119,
'class': 'python11',
'score': [99, 88.8, 100.5],
't_age': 20}
#修改t_age对应的值,要保证t_age是已存在的key
a["t_age"]=18
print(a)
<<<<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18}
新增 a[不存在的key] = 新value key是字典中不存在的
a = {
'student': 119,
'class': 'python11',
'score': [99, 88.8, 100.5],
't_age': 20}
# 新增name对应的值,要保证name是不存在的key
a["name"]="monica"
print(a)
<<<<< {'student': 119, 'class': 'python11', 'score': [99, 88.8, 100.5], 't_age': 18,'name':"monica"}