字典数据的增加:
1、dict1={'name':'tom','age':18,'gender':'男'}
dict1['id']='110'
print(dict1) #{'name':'tom','age':18,'gender':'男','id':110}
2、dict1={'name':tom,'age':18,'gender':'男'}
dict1['name']='rose'
print(dict1) #dict1={'name':'rose','age':18,'gender':'男'}
注意:如果key存在则修改这个key对应的值,如果key不存在则新增此键值对
2、字典数据的删除
del 字典名/del(字典名)----删除整个字典
dict1={'name':tom,'age':18,'gender':'男'}
del dict1
------------------------------------------------------
dict1={'name':tom,'age':18,'gender':'男'}
del dict1['name']
print (dict1) #{'age':18,'gender':'男'} 删除指定的键值对
字典序列号.clear()清空字典
dict1={'name':tom,'age':18,'gender':'男'}
dict1.clear() #{}
3、字典数据的查找
dict1={'name':tom,'age':18,'gender':'男'}
print(dict1['name']) #'tom' 如果当前的key存在则返回值,如果没有就报错
get()函数:字典序列.get(key,默认值)
注意:如果当前查找的key不存在则返回第二个参数(默认值),如果省略第二个参数,则返回none
dict1={'name':tom,'age':18,'gender':'男'}
print(dict1.get('name')) #tom
print(dict1.get('id')) #none
keys()函数:查找字典中所有的key,返回可迭代的对象
dict1={'name':tom,'age':18,'gender':'男'}
print(dict1.keys()) #dict_keys(['name','age','gender'])
values()函数:朝朝字典中所有的值,返回可迭代的对象
dict1={'name':tom,'age':18,'gender':'男'}
print(dict1.values()) #dict_vules(['tom',18,'男'])
items()函数:查找字典中所有的键值对,返回可迭代的对象,里面的数据是元组
dict1={'name':tom,'age':18,'gender':'男'}
print(dict1.vaules()) #dict_item([('name','tom'),('age',18),('gender','男')])