Python-字典的增删改操作

Key的判断

in 指定的key在字典中存在返回True,'张三' in scores

not in 指定的key在字典中不存在返回True,'Marry' not in scores

字典元素的删除

del scores['张三']

字典元素的新增

scores['Jack'] = 90

 1 # key的判断
 2 scores = {'张三':100, '李四':98, '王五':45}
 3 print('张三' in scores)
 4 print('张三' not in scores)
 5 
 6 # 删除指定键值对
 7 del scores['张三']
 8 print(scores)
 9 
10 #清空字典的元素
11 scores.clear()
12 print(scores)
13 
14 # 增加字典元素
15 scores['陈六'] = 98
16 print(scores)
17 
18 # 修改字典元素
19 scores['陈六'] = 100
20 print(scores)

 

posted @ 2022-02-24 14:58  Xxiaoyu  阅读(51)  评论(0编辑  收藏  举报