Python 3.12 ·
✓ verified by execution on 2026-07-22
A tuple is a collection of items similar to a list. However, while lists are designed to be changed (mutable), tuples are permanently fixed (immutable).
You create a tuple by separating values with commas. Parentheses are usually added to group the items clearly.
Without the comma, Python treats (5) as an integer wrapped in mathematical parentheses!
Tuple Unpacking
Tuples allow you to extract all their elements directly into variables in a single step. This is known as unpacking.
python
user = ('Alice', 25, 'Admin')name, age, role = userprint(name)print(age)print(role)
Output
Alice
25
Admin
Your output
When unpacking, you must provide exactly one variable for every item in the tuple. If the counts don’t match, Python throws a ValueError.
python
try: user = ('Alice', 25, 'Admin') name, age = userexcept ValueError as e: print(repr(e))
Output
ValueError('too many values to unpack (expected 2, got 3)')
Your output
The Swapping Idiom
Because Python evaluates the entire right side of an assignment before assigning it to the left side, you can use tuple packing and unpacking to swap two variables on a single line!
python
a = 'apple'b = 'banana'a, b = b, aprint(a, b)
Output
banana apple
Your output
Behind the scenes, Python packs b and a into a temporary tuple, and immediately unpacks it into the a and b variables on the left.
Returning Multiple Values
A function in Python can technically only return exactly one item. However, if you return multiple values separated by commas, Python automatically packs them into a single tuple!
This pattern makes Python functions incredibly flexible, allowing you to return and immediately unpack multiple pieces of data.
Check yourself
Parentheses are required to create a tuple.
Reveal answer
False, commas create the tuple. Parentheses are just often used for grouping. — Commas create the tuple, not parentheses (except for the empty tuple). Parentheses are just often used for grouping.
Does (5) create a tuple with one element?
Reveal answer
No, it creates a standard integer. You must use (5,) — Without a trailing comma, Python evaluates (5) as a standard mathematical grouping, resulting in an integer. You must use (5,).
Can you change a tuple's contents if you use list methods?
Reveal answer
No, tuples have no append(), insert(), or pop() methods because they are completely immutable. — Tuples have no append(), insert(), or pop() methods precisely because they are completely immutable.
Challenges
Challenge 1 +15 XP
You are given two variables, 'first' and 'second'. Use Python's tuple packing and unpacking to swap their values in a single line of code. Do not use a temporary variable.
python
Test 1 — expects "200\n100\n"
Need a hint? (−25% XP)
Put both variables on the left side of the equals sign separated by a comma, and their reversed order on the right side.
Show solution (0 XP)
first = 100second = 200first, second = second, firstprint(first)print(second)
Challenge 2 +20 XP
Write a function 'get_dimensions' that takes a list of numbers and returns both the first number (width) and the second number (height). Then call the function and unpack the result into two variables: w and h.
python
Test 1 — expects "1920 x 1080\n"
Need a hint? (−25% XP)
Use 'return nums[0], nums[1]' in the function. When calling it, use 'w, h = get_dimensions([1920, 1080])'.