字典示例

字典将键映射到值。

car = {}
car["wheels"] = 4
car["color"] = "Red"
car["model"] = "Corvette"

可以通过键访问字典值。

print "Little " + car["color"] + " " + car["model"] + "!"
# This would print out "Little Red Corvette!"    

字典也可以以 JSON 样式创建:

car = {"wheels": 4, "color": "Red", "model": "Corvette"}

字典值可以迭代:

for key in car:
  print key + ": " + car[key]

# wheels: 4
# color: Red
# model: Corvette