S06 · Build a Themed Collection

What we learned in Python

The Python from session S06, one idea at a time. These notes follow lessons/sessions/s06/student/task-card.md in your course folder.

Back to the S06 slides

1 · Python concept

The idea

Today's Python
dictionaryOne object record made of key-value pairs. Each key names a property such as name, x, y, or color.
list of dictionariesAn ordered collection of object records. In this lesson, one list holds exactly three dictionaries.
key lookupSquare brackets retrieve one property by its key: object_record["x"] gets that record's x value.
for loopRepeats the same indented steps once for each item in a collection, in order.
Python to the worldThe Python records are a planning inventory. The validated package YAML supplies the values the Trail actually loads.

2 · Code we wrote

The code

objects = [
    {"name": "Sun Seed", "x": 180, "y": 180, "color": "gold"},
    {"name": "Rain Bell", "x": 380, "y": 300, "color": "blue"},
    {"name": "Wind Flower", "x": 620, "y": 420, "color": "pink"},
]

for object_record in objects:
    print(
        object_record["name"],
        object_record["x"],
        object_record["y"],
        object_record["color"],
    )

Run it from the course folder:

python lessons/sessions/s06/student/starter.py

Output:

Sun Seed 180 180 gold
Rain Bell 380 300 blue
Wind Flower 620 420 pink

3 · What the code means

Line by line

Each important line
objects = [Starts one ordered list. Its three items are the three object dictionaries below it.
{"name": "Sun Seed", "x": 180, "y": 180, "color": "gold"}One dictionary: a complete record for one object. Colons connect each property key to its value.
for object_record in objects:Starts one plain loop. On the first iteration object_record is the Sun Seed dictionary, then Rain Bell, then Wind Flower.
object_record["x"]Looks up the value stored under the x key in the current dictionary. On the first iteration it is 180.
four indented lookupsThe loop prints the current record's name, x, y, and color on one inventory line, then repeats for the next record.
objects/*.yamlThe package files receive the intended plain values. Python does not run inside the package; the validator checks the YAML before the Trail loads it.

4 · Why programmers use this

Beyond this lesson

A dictionary keeps the properties of one thing together. A list keeps several related things together and preserves their order.

A for loop applies the same work to every record, so the inventory needs one block instead of three repeated print statements.

Outside Explore Studio, programs use lists of records for contacts, products, observations, and map locations. A validated data file can then carry those planned values into another system.

5 · What we debugged

A bug worth understanding

broken_record = {
    "name": "Wind Flower",
    "x": 520,
    "color": "pink",
}

print(broken_record["y"])
KeyError: 'y'

The lookup asks for the y property, but that key is missing from the dictionary. Python raises KeyError instead of inventing a value. Add the missing property before reading it.

Fixed:

broken_record = {
    "name": "Wind Flower",
    "x": 520,
    "y": 240,
    "color": "pink",
}

print(broken_record["y"])

6 · Key Python words

Words to know

Vocabulary
dictionaryA collection of key-value pairs inside braces { }.
keyThe name of one property, such as "x" or "color".
valueThe data stored for a key, such as 180 or "gold".
lookupReading a value with square brackets and a key.
forThe keyword that starts a loop over a collection.
iterationOne pass through a loop for one item.
KeyErrorPython's error for looking up a key a dictionary does not have.
YAMLThe package data format that supplies the Trail's validated runtime values.

7 · Try it yourself

Your turn

  1. Trace only the first iteration on paper: which record is object_record, what is object_record["x"], and what exact line prints?
  2. Predict the three output lines, run the starter, and compare them in list order.
  3. Temporarily remove y from one record, predict the error, run it, then restore the property.
  4. Match each Python record to its YAML object file and explain which side plans the data and which side drives the visible Trail.