Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is tuple unpacking in Python?
Tuple unpacking is a way to assign the elements of a tuple to multiple variables in a single statement. It makes code cleaner and easier to read.
Click to reveal answer
beginner
How do you unpack a tuple (1, 2, 3) into variables a, b, and c?
You write a, b, c = (1, 2, 3). This assigns 1 to a, 2 to b, and 3 to c.
Click to reveal answer
intermediate
What happens if you try to unpack a tuple with more elements than variables?
Python will raise a ValueError because the number of variables does not match the number of elements in the tuple.
Click to reveal answer
intermediate
How can you use the star * operator in tuple unpacking?
The star * operator collects multiple elements into a list. For example, a, *b = (1, 2, 3) assigns 1 to a and [2, 3] to b.
Click to reveal answer
beginner
Can tuple unpacking be used with other data types besides tuples?
Yes! Tuple unpacking works with any iterable like lists, strings, and sets, as long as the number of variables matches the number of elements.
Click to reveal answer
What will this code print?
a, b = (10, 20)
AError: too many values to unpack
Ba = (10, 20), b = None
Ca = 10, b = 20
Da = 20, b = 10
✗ Incorrect
The tuple (10, 20) is unpacked into variables a and b respectively.
What error occurs if you do a, b = (1, 2, 3)?
ATypeError
BValueError
CSyntaxError
DNo error
✗ Incorrect
ValueError occurs because there are more values than variables to unpack.
How do you capture the remaining elements in a tuple during unpacking?
AUsing the star operator *
BUsing double equals ==
CUsing parentheses ()
DUsing a for loop
✗ Incorrect
The star operator * collects remaining elements into a list.
Which of these is a valid unpacking?
a, b, c = [1, 2, 3]
AYes, because lists are iterable
BNo, only tuples can be unpacked
CNo, lists need to be converted to tuples first
DYes, but only if list has strings
✗ Incorrect
Unpacking works with any iterable, including lists.
What will this code print?
a, *b = (5, 6, 7, 8)
Aa=5, b=6
Ba=[5], b=6
CError
Da=5, b=[6, 7, 8]
✗ Incorrect
a gets the first element 5, b collects the rest as a list.
Explain how tuple unpacking works and give an example.
Think about how you can take a box with items and put each item into a separate labeled container.
You got /3 concepts.
Describe how the star operator (*) is used in tuple unpacking.
Imagine one container gets one item, and the other container gets all the rest.
You got /3 concepts.
Practice
(1/5)
1. What does tuple unpacking do in Python? a, b = (1, 2)
easy
A. Raises an error because of wrong syntax
B. Creates a tuple with elements a and b
C. Swaps the values of a and b
D. Assigns 1 to a and 2 to b in one line
Solution
Step 1: Understand tuple unpacking syntax
The code a, b = (1, 2) assigns the first value 1 to variable a and the second value 2 to variable b.
Step 2: Confirm the assignment
After unpacking, a equals 1 and b equals 2, which matches the tuple elements.
Final Answer:
Assigns 1 to a and 2 to b in one line -> Option D
Quick Check:
Tuple unpacking = Assign values [OK]
Hint: Match variables to tuple elements in order [OK]
Common Mistakes:
Thinking it creates a tuple instead of unpacking
Confusing unpacking with swapping
Expecting an error from correct syntax
2. Which of the following is the correct syntax for tuple unpacking in Python?
easy
A. a, b = [1, 2
B. a, b = (1, 2)
C. (a, b = 1, 2
D. a = (1, 2) = b
Solution
Step 1: Check each option for valid unpacking syntax
a, b = [1, 2 "a, b = [1, 2" is missing closing bracket, syntax error. (a, b = 1, 2 "(a, b = 1, 2" has mismatched parentheses, syntax error. a = (1, 2) = b "a = (1, 2) = b" is invalid syntax. Only B is correct.
Step 2: Identify the best tuple unpacking syntax
a, b = (1, 2) clearly shows tuple unpacking with a tuple on the right and variables on the left, which is the most correct and clear syntax.
Final Answer:
a, b = (1, 2) -> Option B
Quick Check:
Tuple unpacking syntax = a, b = (1, 2) [OK]
Hint: Look for variables on left, tuple on right [OK]
Common Mistakes:
Forgetting closing bracket when using lists
Mismatched parentheses on left side
Using invalid assignment chaining
3. What is the output of this code?
point = (4, 5)
x, y = point
print(x + y)
medium
A. 9
B. (4, 5)
C. 45
D. Error
Solution
Step 1: Unpack the tuple into variables
The tuple (4, 5) is unpacked into x = 4 and y = 5.
Step 2: Calculate the sum of x and y
Adding x + y gives 4 + 5 = 9.
Final Answer:
9 -> Option A
Quick Check:
4 + 5 = 9 [OK]
Hint: Add unpacked variables directly [OK]
Common Mistakes:
Printing the tuple instead of sum
Concatenating numbers as strings
Expecting a syntax error
4. Find the error in this tuple unpacking code:
data = (1, 2, 3)
a, b = data
print(a, b)
medium
A. Too many values to unpack
B. Not enough values to unpack
C. Syntax error in assignment
D. No error, code runs fine
Solution
Step 1: Count values and variables
The tuple data has 3 values but only 2 variables a and b are on the left side.
Step 2: Understand unpacking rules
Python requires the number of variables to match the number of values unless using a starred expression. Here, 3 values cannot fit into 2 variables, causing a ValueError.
Final Answer:
Too many values to unpack -> Option A
Quick Check:
Mismatch in values and variables = Error [OK]
Hint: Count variables and values before unpacking [OK]
Common Mistakes:
Ignoring value-variable count mismatch
Expecting Python to auto-fill missing variables
Using wrong error type
5. Given a list of tuples representing points, how do you unpack and sum all x and y values?
points = [(1, 2), (3, 4), (5, 6)]
total_x = 0
total_y = 0
for point in points:
# Fill in unpacking here
total_x += x
total_y += y
print(total_x, total_y)
Which line correctly unpacks the tuple inside the loop?
hard
A. (x, y) = [point]
B. point = x, y
C. x, y = point
D. x, y = [point]
Solution
Step 1: Understand the loop variable
Each point is a tuple like (1, 2). To access x and y, unpack it into variables x and y.
Step 2: Choose correct unpacking syntax
The correct syntax is x, y = point. This assigns the first element to x and second to y for each tuple in the list.
Final Answer:
x, y = point -> Option C
Quick Check:
Unpack tuple with x, y = point [OK]
Hint: Unpack tuple directly with x, y = point [OK]