Python 3D Academyবাংলা

Multiple assignment and swapping

🌍 In real life. You have juice in one glass and milk in another, and you want to swap them. You can't just pour the juice into the milk glass: it's full, and the drinks would mix. You need a third, empty glass to hold one drink while you move the other.

Swapping two variables has exactly the same problem, and Python has a neat trick for it. First, a related shortcut: filling several boxes in one line.

The idea in one sentence: Python can fill several names in one line, matching the names on the left to the values on the right in order, and a, b = b, a uses this to swap two values safely.

Its words:

  • multiple assignment: x, y = 1, 2 fills two boxes at once
  • swap: exchange the values of two variables
  • temp: the usual name for a spare box that holds a value for a moment

Step by step, for x, y, z = 1, 2, 3:

  1. Python works out the whole right side: 1, 2, 3.
  2. It matches them to the names in order: the first value to the first name, and so on.
  3. So x gets 1, y gets 2 and z gets 3.
x, y, z = 1, 2, 3
print(x, y, z)
a = b = 0
print(a, b)

Why the obvious swap fails. Writing a = b and then b = a loses a value: the first line throws the old value of a away, so by the second line there's nothing left to copy back.

a = "apple"
b = "banana"
a = b
b = a
print(a, b)
Both are banana! After a = b, the "apple" was thrown out, so b = a just copies banana back.

How Python's swap works. In a, b = b, a, Python works out the whole right side first: it picks up the value of b and the value of a and holds both at once. Only then does it fill the boxes on the left. Because both values are held before anything is replaced, nothing gets lost. Watch it, and follow the tracker under the boxes:

a = "apple"
b = "banana"
a, b = b, a
print(a, b)

Many languages use a spare box called temp instead. It works in Python too:

a = 1
b = 2
temp = a
a = b
b = temp
print(a, b)

Mistakes and edge cases.

  • More values than names. x, y = 1, 2, 3 gives ValueError: too many values to unpack (expected 2, got 3).
  • Fewer values than names. x, y, z = 1, 2 gives ValueError: not enough values to unpack (expected 3, got 2).
  • The obvious swap. a = b then b = a leaves both boxes holding the same value.
  • The temp steps in the wrong order. With a spare box, save first: temp = a, then a = b, then b = temp. Starting with a = b loses a before it's saved.

Quiz

  1. What does this print?

    x, y = 3, 8
    print(y)
  2. What does this print?

    a = b = 5
    print(a + b)
  3. What does this print?

    a, b = 1, 2
    a, b = b, a
    print(a, b)
  4. Why doesn't a = b followed by b = a swap two variables?

    • The first line throws away a's old value
    • Python can't copy variables
    • You must print them first
  5. Which line causes an error?

    x, y = 1, 2, 3
    print(x)

Coding tasks

  1. In one line, set width to 4 and height to 6. Then print width * height.

  2. Swap left and right, so that left holds "R" and right holds "L". Don't retype the letters: swap the variables.