...

/

Teach Your Code to Remember Lots of Things!

Teach Your Code to Remember Lots of Things!

Learn to store and retrieve labeled data using dictionaries.

So far, you’ve used variables to store single pieces of data and lists to store collections. But what if you want to label your data with keywords? That’s where dictionaries come in—Python’s way of storing key–value pairs!

A dictionary in Python

Think of a dictionary like a real one:

  • We look up a word (the key).

  • We get a definition (the value).

Let’s look at the syntax first:

person = {
"name": "Ava", # Key is "name", value is "Ava"
"age": 25, # Key is "age", value is 25 (notice: no quotes around numbers)
"city": "Seattle" # Strings must be in quotes, and each key-value pair ends with a comma
}
...