带有示例的Python字典update()方法
词典update()方法
update()方法用于通过将新项目插入字典来更新字典。
语法:
dictionary_name.setdefault(iterable)
Parameter(s):
可迭代–表示要在字典中插入的Iterbale对象或字典。
返回值:
它什么也没返回。
示例
#带有示例的Python字典update()方法
#字典声明
student = {
"roll_no": 101,
"name": "Shivang",
"course": "B.Tech",
"perc" : 98.5
}
#印刷词典
print("data of student dictionary...")
print(student)
#插入项目
student.update({'city' : 'Indore'})
#印刷词典 after update()
print("data of student dictionary after update()...")
print(student)
#插入多个项目
student.update({'city' : 'Indore', 'address' : 'Tech park'})
#印刷词典 after update()
print("data of student dictionary after update()...")
print(student)输出结果
data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5}
data of student dictionary after update()...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5, 'city': 'Indore'}
data of student dictionary after update()...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5, 'city': 'Indore', 'address': 'Tech park'}热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法