Skip to content

Lesson 7 · dictionaries

The menu book

  1. 1. Story
  2. 2. Guess
  3. 3. Your turn
  4. 4. Catch Sitara’s mistake

EllaA customer asked the price of coffee and Kai said “about forty?”

SitaraA dictionary is a menu book: each name points to its price.

KaiNo more “about”.

A dictionary pairs a key with a value, like a menu pairs a dish with its price.

  1. 1. { "key": value } pairs a name with a value
  2. 2. menu["coffee"] looks up the price
  3. 3. menu["cookie"] = 10 adds a new pair
  4. 4. for item in menu: visits each key, like each dish name
cafe.py

Try: Add scone at 15 to the menu, then run it.

Read · about 1 minute

The basics, in plain words

A dictionary pairs a key with a value, like a menu pairs a dish with its price.

menu = {"latte": 20, "coffee": 40}
print(menu["coffee"])
menu["cookie"] = 10
print(menu)
  • { "key": value } pairs a name with a value
  • menu["coffee"] looks up the price
  • menu["cookie"] = 10 adds a new pair
  • for item in menu: visits each key, like each dish name
{ key: value }
A dictionary pairs a key with a value, like a dish with its price.
menu["latte"]
Looks up the value for that key.

Common slip: Keys must match exactly. "Latte" and "latte" are different keys.

Read all twelve Python basics with Sitara →
Next · lesson 8 · about 12 minutesWhat is an agent?You will learn the agent loop.Next lesson →