开始使用免费开始使用

访问字典

如果字典的键选得合适,访问字典中的值会非常直观。例如,要从 europe 中获取法国的首都,您可以使用:

europe['france']

这里,'france' 是键,返回的值是 'paris'

本练习是课程的一部分

Python 中级

查看课程

练习说明

  • 通过在 europe 上调用 keys() 方法来查看有哪些键。打印输出结果。
  • 打印键 'norway' 对应的值。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }

# Print out the keys in europe


# Print out value that belongs to key 'norway'
编辑并运行代码