🐸

The Opened Pair

The Data Tinkererpython-data-tinkerer-09-the-opened-pair
Reward: 105 XP
|

The Opened Pair

In the last lesson, Hoppy tucked a scroll name and shelf code into one fixed archive card. That solved the saving part: the two linked pieces could travel together as a pair.

But now the keeper asks Hoppy to fetch the scroll for real. Holding the whole card is not enough. Hoppy needs to open it and take out the scroll name and shelf code separately, then walk to the shelf. This step is about opening the fixed pair so the information can be used again.

Open the record card into two named variables

The key move in this lesson is simple: one fixed pair can be unpacked into two variables. Because the tuple already has a fixed meaning, the unpacked names feel natural too: first the scroll name, then the shelf code.

scroll_record = ("Fern Ledger", "A-7")

print("Record card:", scroll_record)

record_name, shelf_code = scroll_record

print("Opened name:", record_name)
print("Opened shelf:", shelf_code)
print(f"Fetch {record_name} from shelf {shelf_code}.")

Here, record_name, shelf_code = scroll_record opens the tuple in one step. The first slot of the tuple goes into record_name, and the second slot goes into shelf_code. You are not changing the card into something new. You are taking one saved pair and turning it into two named pieces you can immediately use.

Why this feels more natural than [0] and [1]

You could reach into the tuple with scroll_record[0] and scroll_record[1]. That works, but it feels like pulling one slot out of a box, then another.

Unpacking reads more like opening a record card. Since the first slot already means “name” and the second already means “shelf code,” giving them those variable names directly makes the code easier to read and closer to the real job Hoppy is doing.

1
Look at the tuple that is already prepared

The starter already gives you scroll_record. You do not need to rebuild the card. This time, the job is to open it.

2
Replace the two placeholder lines with one unpacking line

Write one line that gives the first slot to record_name and the second slot to shelf_code.

3
Run it and notice the change from stored pair to usable pieces

First you will still see the whole tuple on the Record card: line. Then you will see the opened variables and the final fetch line. That is the main idea of the lesson: one fixed record can be stored together and used separately.

A very small move, but an important one

Last lesson was about keeping two linked values together. This lesson is about the next moment: once that fixed pair already exists, you can open it into two clear names and use them right away. That is what makes the tuple feel useful instead of decorative.

Suggested Solution
Expand
Solution:
scroll_record = ("Moon Map", "B-12")

print("Record card:", scroll_record)

record_name, shelf_code = scroll_record

print("Opened name:", record_name)
print("Opened shelf:", shelf_code)
print(f"Fetch {record_name} from shelf {shelf_code}.")
Advanced Tips
Want more? Click to expand

You do not need bigger unpacking tricks yet. The important instinct here is smaller: a tuple is not only something you store. It can also be opened into named pieces when you want to do real work with that fixed structure.

Keep that picture in your head: save the pair as one card, then open the card when action starts. That connection between “stored together” and “used separately” is the whole point of this bridge lesson.

Loading...
Terminal
Terminal
Ready to run...