Multiple Assignment and Parallel Assignment
Tuple assignment unpacks multiple values from a tuple into separate variables in a single statement, with the number of variables and values required to match exactly.
Try it: Assignment, Statement by Statement
How an assignment statement evaluates its right side first (using the names' current values) and only then binds the name on the left: updating x = x + 1, augmented operators like += and //=, chained a = b = 0, simultaneous a, b = b, a + b versus one-at-a-time assignment, rebinding a name to a value of another type, the NameError for a name that was never assigned, and the SyntaxError for a name that breaks the naming rules.
How it works
- Before anything runs, Python compiles the whole program; a name that breaks the rules (starts with a digit, contains a space or a dash, or is a keyword) is a SyntaxError and no line runs at all.
- For each assignment, evaluate the complete right side first, looking every name up in the variable table as it is now.
- Then bind the name(s) on the left to that value: a new name is added to the table, an existing one is replaced (even by a value of a different type).
- x += 5 reads x, applies the operator (+, -, *, //, %, **) and binds the result back to x; a = b = 0 evaluates 0 once and binds a, then b.
- a, b = b, a + b builds the whole right-hand tuple from the OLD values before binding a and b; two separate statements would use the new a in the second line.
- Reading a name that is not in the table raises NameError and the program stops at that line.
Default run (10 steps): A 4-line program. Python first compiles all of it, then runs it top to bottom. The variable table on the right starts empty. … Program finished. Final table: x = 16.
Simplified: The programs are fixed templates: you choose the template, its numbers, the operator and the variable name, never the code. The lab's own small Python tokenizer and parser (written for the statements these programs use: assignments, print calls, if / elif / else, for, while, def and return) checks the text; nothing is executed as real Python. Every program the lab can show was compiled by CPython 3.12 and the lab reproduces its exception type, message, line, caret columns and the printed report. A small interpreter written for the same statements then runs them (integers of any size, the str/float results these templates produce, and print); each program was also run in CPython 3.12 under sys.settrace, and the lab reproduces the variables after every line, the output and the error.
Loading the simulation…
One Statement, Several Values
Multiple assignment lets one statement place several values into several variables. In Python, this commonly appears as tuple assignment: values are unpacked from a tuple and assigned to variables in corresponding positions. The number of variables must match the number of values exactly.
What do you think happens?
After the assignment, what values do x and y contain?
Reveal answer
x contains 'have' and y contains 'fun'.
Python matches positions. The first tuple element goes to the first variable, and the second tuple element goes to the second variable.
Position-Based Unpacking
The tuple m contains two elements: 'have' at position 0 and 'fun' at position 1. In the statement x, y = m, Python maps the value at position 0 to x and the value at position 1 to y. The assignments occur together as one operation rather than as two unrelated assignment statements.
x contains 'have'
y contains 'fun'Swapping Without a Temporary Variable
A practical use of tuple assignment is swapping two variable values. The Pythonic idiom is a, b = b, a. Unlike a traditional temporary-variable approach, this statement does not require you to create a separate variable to hold one value while the other moves.
What do you think happens?
After a, b = b, a runs, what values do a and b contain?
Reveal answer
a is 2 and b is 1.
The right side uses the old values of b and a, in that order. Those values are then assigned to a and b.
a is 2
b is 1Right Side Before Left Side
The swap is safe because Python evaluates the entire right-hand side before binding values to the left-hand side. For a, b = b, a, Python first obtains the current value of b and the current value of a in that order. It then unpacks those values into a and b. Since both original values are obtained before either variable is changed, one value is not accidentally overwritten before it is used.
| Approach | Intermediate storage | Readability | Performance and style |
|---|---|---|---|
| Tuple assignment | Uses the right-side values before binding | Readable and idiomatic | More efficient than traditional temporary-variable swapping |
| Traditional swapping | Uses a temporary variable | More steps to follow | Less efficient than tuple assignment |
Tuple assignment expresses the swap directly, while a traditional approach uses temporary storage.
Matching Counts Exactly
Tuple assignment requires an exact count match. Every value being unpacked needs a corresponding variable. If the counts differ, Python raises a ValueError. This is one of the most common debugging points when learning unpacking.
ValueError: too many values to unpackCommon mistake Using fewer variables than values
What it looks likea, b = (1, 2, 3)Why it failsThere are three values but only two variables, so no variable is available for the third value.
Do this insteadMake the number of variables match the number of values before unpacking.
Common mistake Assuming Python silently discards an extra value
What it looks likea, b = (1, 2, 3)Why it failsTuple assignment requires an exact count match; the extra value causes a
ValueError.Do this insteadCheck both sides of the assignment and identify the count mismatch.
Choosing the Pythonic Form
Use tuple assignment when several values naturally need to be unpacked into corresponding variables, especially when swapping two values. The syntax communicates the relationship directly, avoids a temporary variable for the swap, and is described as more readable, faster, and more idiomatic than traditional temporary-variable swapping.
Practice the Trace
Suppose a tuple contains three values and you want to unpack it. Write an assignment pattern with three variables, then explain which value goes to each variable based on position. Next, describe what count-related problem you would investigate if Python raised a ValueError.
Hints
- Count the values in the tuple.
- Create the same number of variables.
- Match the first value with the first variable, the second value with the second variable, and so on.
Explain the state change caused by a, b = b, a when a starts with one value and b starts with a different value. Your explanation should mention why the right-hand side must be evaluated before the left-hand side is updated.
Hints
- List the old values of a and b.
- Read the right side in its written order: b, then a.
- Map those two values back to a, then b.
Key Takeaways
- 1Tuple assignment unpacks tuple values into variables by matching their positions.
- 2The number of variables must exactly match the number of values being unpacked.
- 3The Pythonic swap a, b = b, a evaluates the right side before assigning to the left side.
- 4Tuple assignment avoids a temporary variable and is more readable, faster, and more idiomatic for swapping values.
- 5A mismatched unpacking count raises a
ValueError, so count both sides when debugging.
At a glance
- Tuple assignment maps values to variables by position in a single statement.
- Swapping with a, b = b, a works because Python evaluates the complete right-hand side before binding the left-hand side.
- Unpacking requires an exact match between the number of variables and values.
- A
ValueErrorcaused by unpacking should be debugged by counting both sides of the assignment. - Tuple assignment is a readable, efficient, and idiomatic alternative to temporary-variable swapping.