创建字典stu_1,使属性与内容形成对应关系如下: name:Tom,age:18,number:20200876,score:486 (1)为字典stu_1添加新的元素:address:北京 (2)遍历字典stu_1的键、遍历字典stu_1的值、遍历字典stu_1的元素。
时间: 2024-03-27 12:35:40 浏览: 16
好的,按照您的要求,我来给您展示如何操作:
```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', '北京')
```
相关问题
创建字典stu_1,使属性与内容形成对应关系如下: #name:Tom,age:18,number:20200876,score:486 #(1)为字典stu_1添加新的元素:address:北京 #(2)遍历字典stu_1的键、遍历字典stu_1的值、遍历字典stu_1的元素。
下面是实现你所需求的Python代码:
```python
# 创建字典stu_1
stu_1 = {"name": "Tom", "age": 18, "number": 20200876, "score": 486}
# 添加新元素
stu_1["address"] = "北京"
# 遍历键
print("遍历字典stu_1的键:")
for key in stu_1.keys():
print(key)
# 遍历值
print("遍历字典stu_1的值:")
for value in stu_1.values():
print(value)
# 遍历元素
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', '北京')
```
在python中创建字典stu_1,使属性与内容形成对应关系如下: name:Tom,age:18,number:20200876,score:486 (1)为字典stu_1添加新的元素:address:北京 (2)遍历字典stu_1的键、遍历字典stu_1的值、遍历字典stu_1的元素。
好的,我来回答你的问题。
首先,我会按照你的要求来创建一个字典stu_1,代码如下:
```python
stu_1 = {'name': 'Tom', 'age': 18, 'number': 20200876, 'score': 486}
```
接着,我会按照你的要求来添加一个新的元素address,并赋值为北京,代码如下:
```python
stu_1['address'] = '北京'
```
现在,stu_1的元素已经变成了{'name': 'Tom', 'age': 18, 'number': 20200876, 'score': 486, 'address': '北京'}。
接下来,我会按照你的要求来遍历字典stu_1的键、值、元素,代码如下:
```python
# 遍历字典stu_1的键
for key in stu_1:
print(key)
# 遍历字典stu_1的值
for value in stu_1.values():
print(value)
# 遍历字典stu_1的元素
for item in stu_1.items():
print(item)
```
这样,就完成了你的问题的回答,希望能对你有所帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)