Concepts / Immutability and Tuples

Immutability and Tuples

Tuple assignment enables swapping two variables in one line: a, b = b, a

  • Programming

The One-Line Exchange

Swapping two values is a fundamental programming operation. You may need to exchange two numbers, change player positions, sort values, or rearrange data. Python provides a concise form of tuple assignment for this task: a, b = b, a. The statement makes a receive b's original value and b receive a's original value, without requiring a separately named temporary variable.

What do you think happens?

Before the statement a, b = b, a executes, a is 5 and b is 10. What are their values afterward?

  • a is 5 and b is 10
  • a is 10 and b is 5
  • Both variables are 10
  • Both variables are 5
Reveal answer

Answer: a is 10 and b is 5

Python evaluates the right side first, obtaining b's current value, 10, and a's current value, 5. It then assigns those two values to a and b by position.

Evaluation Before Assignment

The crucial mechanism has two phases. First, Python evaluates every expression on the right side of the assignment. For a, b = b, a, it looks up the current value of b and the current value of a, producing the tuple (10, 5) when b is 10 and a is 5. Only after the right side has been fully evaluated does Python assign the values to the names on the left. This order preserves both original values before either variable changes.

evaluatethen evaluatecombine valuesassign after evaluationa, b = b, aswap statementb10a5(10, 5)evaluated right sideleft-side assignment10 to a, 5 to b
What happens first when Python executes a, b = b, a, and how are both original values preserved?

Positional Matching

After the right side has been evaluated, tuple assignment matches values by position. The first value in the evaluated tuple is assigned to the first name on the left, and the second value is assigned to the second name. In the swap, the right side is (b's value, a's value), so the first position goes to a and the second position goes to b.

position 0position 1position 010areceives 10position 15breceives 5
How are the positions in the evaluated right-side tuple matched to the names on the left?

Tracing a swap

Suppose a has the value 5 and b has the value 10. Follow a, b = b, a.

Evaluate b: The current value of b is 10, so the first right-side value is 10.

Evaluate a: The current value of a is 5, so the second right-side value is 5.

Form the evaluated tuple: The two right-side values are represented as the ordered pair (10, 5).

Assign by position: The first value, 10, goes to a. The second value, 5, goes to b.

After the statement, a is 10 and b is 5. Neither original value was overwritten before it was saved for the assignment.

Preserving Both Values

The evaluated tuple acts as the preserved collection of right-side values during this assignment. In the example, (10, 5) contains b's original value followed by a's original value. Python then unpacks those positions into the left-side names. The important distinction is between creating the evaluated tuple for the assignment and changing an existing tuple: the swap relies on preserving the evaluated values while assignment takes place.

first positionsecond positionposition 0position 1b10(10, 5)right-side valuesa10a5b5
What does the tuple (b, a) contain during the swap, and how does it provide both original values before assignment?

Swapping in Practice

Tuple swapping is useful whenever two related values must exchange positions. The source identifies sorting algorithms, data rearrangement, and exchanged player positions as practical settings. In each case, the same reasoning applies: evaluate both current values first, then assign them to their opposite destinations.

original value movesoriginal value movesa5a10b10b5
How do the original values move between two variables without an intermediate variable?

Generated example: imagine two neighboring positions in an ordered collection that must change places during a rearrangement. Tuple assignment can express the exchange of the two position values directly. The same pattern is useful when a sorting step needs to move one value into the other value's position.

Prefer tuple assignment when the intent is a direct exchange. The source describes a, b = b, a as more concise and readable than a temporary-variable sequence because other programmers immediately recognize it as a swap.

Mistakes During a Swap

  • Treating the statement as two immediate assignments performed from left to right.

    The right side is evaluated completely before any left-side assignment occurs.

    Fix: Trace the right side first: read b, read a, form the ordered values, and then assign them by position.

  • Assuming that a's original value disappears when a changes.

    The original value has already been preserved among the evaluated right-side values.

    Fix: Remember that the evaluated tuple contains (10, 5) before either name is updated.

  • Using a longer temporary-variable sequence when a direct exchange is intended.

    The sequence can work, but it takes more code to express the same exchange.

    Fix: Use a, b = b, a when a concise, recognizable tuple swap fits the problem.

  • Ignoring positional matching.

    Assignment matches positions: the first right-side value goes to the first left-side name.

    Fix: Map position 0 to the first left-side name and position 1 to the second.

Check Your Trace

MEDIUM

A program needs to exchange two related values during a rearrangement. Explain, in order, what Python does when it executes a, b = b, a. Your explanation should name the two phases and state why the original value of a is not lost.

Hints
  • Start by identifying the current values read from the right side.
  • Write the evaluated tuple in the order Python obtains it.
  • Then match each tuple position to the corresponding left-side name.
  1. A correct trace says that Python first evaluates both right-side expressions, producing the ordered values b's value and a's value. It then assigns the first value to a and the second value to b. Because both original values were available before assignment, neither is overwritten prematurely.

Key Takeaways

  1. The statement a, b = b, a swaps two variable values in one line. Python evaluates all right-side expressions before changing any left-side names. The resulting tuple preserves the original values, and positional matching sends the first value to a and the second to b. This avoids the overwriting problem of poorly ordered sequential assignments and provides a concise, readable way to exchange values in sorting and data rearrangement.

Key Takeaways

  • Tuple assignment swaps two values with a, b = b, a.
  • Python evaluates every right-side expression before performing left-side assignments.
  • The evaluated values preserve both originals, preventing premature overwriting.
  • Assignment matches right-side values to left-side names by position.
  • Tuple swapping is concise and readable in sorting, rearrangement, and other exchange operations.