Concepts / Immutability and Tuples

Immutability and Tuples

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.

  • 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.

a, b = b, aswap statementb10a5(10, 5)evaluated right sideleft-side assignment10 to a, 5 to bevaluatethen evaluatecombine valuesassign after evaluation
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 010areceives 10position 15breceives 5position 0position 1
How are the positions in the evaluated right-side tuple matched to the names on the left?
Worked example

Tracing a swap

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

  1. 1
    Evaluate b

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

  2. 2
    Evaluate a

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

  3. 3
    Form the evaluated tuple

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

  4. 4
    Assign by position

    The first value, 10, goes to a. The second value, 5, goes to b.

Result

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.

b10(10, 5)right-side valuesa10a5b5first positionsecond positionposition 0position 1
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.

Before
temp = first
first = second
second = temp

A temporary variable holds one value while the two assignments occur.

After

first, second = second, first

Tuple assignment expresses the exchange in one statement without a separately named temporary variable.

a5a10b10b5original value movesoriginal value moves
How do the original values move between two variables without an intermediate variable?
In the real world

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.

Best practice

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

  • Common mistake

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

    What it looks like

    Reading a, b = b, a as if a receives b first and only then b receives a.

    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like

    Thinking that assigning 10 to a destroys the original 5 before b can receive it.

    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like

    Writing temp = a; a = b; b = temp for a simple swap.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Ignoring positional matching.

    What it looks like

    Expecting the first right-side value to go to b because it originally came from b.

    Why it fails

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

    Do this instead

    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. 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. 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.

At a glance

  • 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.

Related concepts

More concepts starting with I