创建字典stu_1,使属性与内容形成对应关系如下: name:Tom,age:18,number:20200876,score:486 (1)为字典stu_1添加新的元素:address:北京 (2)遍历字典stu_1的键、遍历字典stu_1的值、遍历字典stu_1的元素。
时间: 2024-03-27 15:35:40 浏览: 73
stu-class.rar_be a stu_beautiful_https://stuclass_https://stucla
好的,按照您的要求,我来给您展示如何操作:
```python
# 创建字典stu_1
stu_1 = {'name': 'Tom', 'age': 18, 'number': 20200876, 'score': 486}
# 添加新元素
stu_1['address'] = '北京'
# 遍历字典stu_1的键
print("字典stu_1的键为:")
for key in stu_1.keys():
print(key)
# 遍历字典stu_1的值
print("字典stu_1的值为:")
for value in stu_1.values():
print(value)
# 遍历字典stu_1的元素
print("字典stu_1的元素为:")
for item in stu_1.items():
print(item)
```
输出结果如下:
```
字典stu_1的键为:
name
age
number
score
address
字典stu_1的值为:
Tom
18
20200876
486
北京
字典stu_1的元素为:
('name', 'Tom')
('age', 18)
('number', 20200876)
('score', 486)
('address', '北京')
```
阅读全文