Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Organise the chaos
EXPERIMENT 11 / 101

Keep the shape, lock the slots.

Represent fixed groups and unpack values into meaningful names.

BY NAVAN SAMI◷ 6 MIN READPYTHON 3

01 The idea

A tuple is an ordered sequence whose slots cannot be reassigned after creation. Tuples are useful for small groups that naturally belong together, such as coordinates. Unpacking assigns each item to a corresponding name in a single statement.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

tuples.py
position = (12, 8)
x, y = position

print(f"x={x}, y={y}")
print(position[0])
print(("solo",))
↳ EXPECTED OUTPUT
x=12, y=8
12
('solo',)

03 Under the hood

  1. 1

    The tuple stores two coordinates in a known order. x, y = position unpacks both values.

  2. 2

    Indexing and slicing work much as they do with lists, but position[0] = 5 would fail.

  3. 3

    The comma creates a tuple. A one-item tuple needs a trailing comma: ("solo",). Parentheses by themselves just group an expression.

04 Your turn, builder

A SMALL MISSION

Unpack the tuple ("Orbit", 3) into project and version, then print "Orbit v3".

Reveal one possible solution +
one_possible_solution.py
project, version = ("Orbit", 3)
print(f"{project} v{version}")

05 Check your signal

QUICK SIGNAL CHECK

Which expression creates a one-item tuple?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.