Variable Assignment and Binding
Tuple assignment enables swapping two variables in one line: a, b = b, a
A Two-Value Exchange
Swapping two variable values is a fundamental programming operation. You may need to exchange two numbers while sorting, switch player positions, or reorder data. Python supports a concise form of tuple assignment that performs the exchange in one statement: a, b = b, a.
Before the final statement, a is 5 and b is 10. After it executes, a is 10 and b is 5. The important question is not only what the final values are, but how Python prevents the original value of a from being lost.
Reading Before Rebinding
What do you think happens?
Suppose a is 5 and b is 10. What are the values after a, b = b, a?
Reveal answer
Answer: a is 10 and b is 5
Python evaluates the right side first. It reads b as 10 and a as 5, then assigns those already-evaluated values to a and b in positional order.
The statement has two phases. First, Python evaluates every expression on the right side. For a, b = b, a, it looks up b and a using their current values, producing the ordered values 10 and 5. Only after that evaluation does Python assign the values to the names on the left.
Packing and Unpacking Positions
The right side supplies two ordered values. In the swap example, the values are grouped as (10, 5). The left side contains two variable names in the order a, b. Python matches the first right-side value with a and the second right-side value with b. This grouping and positional matching are the tuple assignment mechanism behind the swap.
The Complete State Change
Swapping Two Positions
Two variables represent two player positions. Exchange their values without using a separate temporary variable.
Initial bindings: Let left_position be 1 and right_position be 2.
Evaluate the right side: The right side reads right_position first and left_position second, producing the ordered values 2 and 1.
Assign by position: The first value is assigned to left_position, and the second value is assigned to right_position.
left_position is 2 and right_position is 1.
This pattern is useful whenever two existing values need to exchange places. It can appear in sorting algorithms, data rearrangement, and situations where two positions or roles must be exchanged.
Avoiding the Overwrite Trap
Treating the statement as two unrelated assignments that run from left to right
Python evaluates all right-side expressions before making the left-side assignments.
Fix:
Trace the right side first: read b, read a, form the ordered values, and then assign them to a and b.Assuming the first assignment destroys the original value needed by the second assignment
The original values have already been evaluated and preserved in the ordered right-side result.
Fix:
Separate the operation into its two phases: complete right-side evaluation, followed by left-side assignment.Replacing the recognized swap pattern with unnecessary temporary-variable steps
This longer form works conceptually, but it takes more steps to read than Python's tuple assignment.
Fix:
Use a, b = b, a when the goal is simply to exchange two values.
| Approach | Main steps | Readability in Python |
|---|---|---|
| Tuple assignment | Evaluate the right side, then assign by position | Concise and immediately recognizable as a swap |
| Temporary variable | Store one value, move the other value, then restore the stored value | Longer and requires more steps to parse |
Apply the Two-Phase Trace
Trace this exchange on paper. Start with first = 12 and second = 20. After first, second = second, first, what value is bound to each variable? Explain the right-side values before describing the assignments.
Hints
- Read second and first before changing either variable.
- Place the first evaluated value into first and the second evaluated value into second.
Explain why a sequential approach that overwrites a before preserving its original value can lose information, then describe how tuple assignment avoids that problem.
Hints
- Identify which original value could be destroyed first.
- Use the two phases: evaluate all right-side expressions, then assign to the left-side names.
Essential Takeaways
- The statement a, b = b, a swaps the values bound to a and b in one line.
- Python evaluates every right-side expression before performing the left-side assignments.
- The evaluated values are matched positionally with the left-side variable names.
- Because the original values are preserved before assignment, one variable does not overwrite a value needed by the other.
- Tuple assignment is a concise and readable alternative to a multi-step temporary-variable swap.
Key Takeaways
- Tuple assignment provides a one-statement way to swap two variable values.
- Right-side expressions are evaluated completely before left-side assignments occur.
- The ordered evaluated values prevent the overwriting problem associated with sequential assignment.
- The pattern is useful in sorting, data rearrangement, and exchanges of positions or roles.