1. Concept library
  2. Python Foundations
  3. Module 4: Lists and tuples

Concept

Tuples and unpacking

A tuple is a fixed sequence that cannot be changed after it is built, which makes it the natural way to return several values. Unpacking assigns each item to its own name in one line.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what makes a tuple different from a list and when to use one
  2. Write a function that returns several values and unpack them in the caller
  3. Spot why (5) is not a tuple and write the one-item tuple correctly

In practice

A few lines that show Tuples and unpacking doing its job.

Python
def min_max(values):
    return min(values), max(values)

low, high = min_max([4, 9, 1])
print(low, high)             # 1 9

point = (3, 5)
x, y = point
first, *rest = [1, 2, 3, 4]  # 1 [2, 3, 4]

The common mistake

Watch for

Writing (5) when you want a one-item tuple. The parentheses are just grouping; it is the comma that makes a tuple, so write (5,).

What to know first

Your roadmap puts these before Tuples and unpacking, unless you already know them.

How it stays learned

Once a session shows you understand Tuples and unpacking, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← List methods