字典操作(1)
如果您会访问字典,也就可以为其中的键重新赋值。要向 europe 添加一个新的键-值对,您可以这样做:
europe['iceland'] = 'reykjavik'
本练习是课程的一部分
Python 中级
练习说明
- 向
europe添加键'italy',其值为'rome'。 - 为了验证
'italy'现在已是europe的一个键,打印'italy' in europe。 - 再向
europe添加一个键:值对:键为'poland',对应的值为'warsaw'。 - 打印
europe。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
# Add italy to europe
# Print out italy in europe
# Add poland to europe
# Print europe