Concepts / Variable Assignment and Binding

Variable Assignment and Binding

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

  • Programming

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.

python

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?

  • a is 5 and b is 10
  • a is 10 and b is 5
  • a is 10 and b is 10
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.

readreadposition 0position 1b10a5(10, 5)evaluated valuesa10b5
What values are read and stored before either variable is changed in the statement a, b = b, a?

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

assignassign10tuple position 0areceives position 05tuple position 1breceives position 1
How are the two right-side values grouped and then matched positionally with the two left-side variables?

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.

value movesvalue movesa5a10b10b5
How do the original values move to their new variables without one assignment destroying a value needed by the other?

The Complete State Change

executeproducea = 5, b = 10initial bindingsa, b = b, aevaluate then assigna = 10, b = 5final bindings
How does a, b = b, a change the bindings of a and b from the initial state to the final state?

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.

python

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.

ApproachMain stepsReadability in Python
Tuple assignmentEvaluate the right side, then assign by positionConcise and immediately recognizable as a swap
Temporary variableStore one value, move the other value, then restore the stored valueLonger and requires more steps to parse

Apply the Two-Phase Trace

EASY

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

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

  1. The statement a, b = b, a swaps the values bound to a and b in one line.
  2. Python evaluates every right-side expression before performing the left-side assignments.
  3. The evaluated values are matched positionally with the left-side variable names.
  4. Because the original values are preserved before assignment, one variable does not overwrite a value needed by the other.
  5. 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.