增加:
a={"name":"唐僧","age":18,"sex":"男"} a["name"]="杀神" print(a)
a={"name":"唐僧","age":18,"sex":"男"} a["name1"]="杀神" print(a)
{'name': '唐僧', 'age': 18, 'sex': '男', 'name1': '杀神'}#没有name1这个键值,添加键和键值
删除:
a={"name":"唐僧","age":18,"sex":"男"} a.pop("name") print(a)
{'age': 18, 'sex': '男'}
a={"name":"唐僧","age":18,"sex":"男"} b=a.pop("name1",None) print(b," " ,type(b))
a={"name":"唐僧","age":18,"sex":"男"} b=a.pop("name1","没有这个键值") print(b," " ,type(b))
a.clear()#清空字典
del a #删除整个字典
改:
a={"name":"唐僧","age":18,"sex":"男"}c={"name":"孙悟空","age":500,"sex":"男","wuqi":"嘴"}a.update(c)print(a)print(c)
{'name': '孙悟空', 'age': 500, 'sex': '男', 'wuqi': '嘴'}
{'name': '孙悟空', 'age': 500, 'sex': '男', 'wuqi': '嘴'}
a={"name":"唐僧","age":18,"sex":"男","职业":"和尚"} c={"name":"孙悟空","age":500,"sex":"男","wuqi":"嘴"} a.update(c) print(a) print(c)
{'name': '孙悟空', 'age': 500, 'sex': '男', '职业': '和尚', 'wuqi': '嘴'}
{'name': '孙悟空', 'age': 500, 'sex': '男', 'wuqi': '嘴'}
#a有的键值对,c没有,则保存。