aDict = ('age': 37, 'score': [98, 97], 'name': 'Dong', 'sex': 'male')
时间: 2023-10-03 18:09:27 浏览: 25
这个语法是错误的,应该使用花括号 {} 来创建字典,而不是圆括号。正确的语法应该是:
aDict = {'age': 37, 'score': [98, 97], 'name': 'Dong', 'sex': 'male'}
这样就可以创建一个名为 aDict 的字典,其中包含了四个键值对。需要注意的是,字典中的键必须是不可变的,而值可以是任意类型的对象。
相关问题
aDict = {'age': 39, 'score': [98, 97], 'name': 'Dong', 'sex': 'male'} print(aDict['age']) aDict['address'] = 'SDIBT' print(aDict)
这段代码的输出结果分别是39和{'age': 39, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'address': 'SDIBT'}。
代码的实际意义是:
- 输出字典aDict中键为'age'的值,即39。
- 向字典aDict中添加一个键为'address',值为'SDIBT'的键值对。
利用for循环分别打印aDict = {'name':'Dong', 'sex':'male', 'age':37}字典对象的每一个键和每一个值。
可以使用字典对象的 `keys()` 和 `values()` 方法获取所有的键和值,然后利用 for 循环分别打印出来,示例如下:
```
aDict = {'name': 'Dong', 'sex': 'male', 'age': 37}
# 打印所有的键
for key in aDict.keys():
print(key)
# 打印所有的值
for value in aDict.values():
print(value)
```
输出结果:
```
name
sex
age
Dong
male
37
```