Swapping Two Python Variables in One Line

left, right = right, left swaps two boxes in one line — and why the obvious two-line swap actually fails.

  • Lesson 5 of 5
  • Free
  • Ages 10+
  • 3 hands-on levels
Try this lesson interactivelyReal Python, running in your browser.

The dragon explains

Swapping and Tracing

  1. The dragon: You know how to change ONE box at a time. Here's how to trade the values in TWO boxes at once.

  2. The dragon: left, right = right, left swaps them. Python builds the whole right side first — (right, left) — THEN hands it back to left and right.

    example.pyPython 3
    left = 1right = 2left, right = right, leftprint(left, right)
    Output2 1
  3. The dragon: Why not just do it the obvious way — left = right, then right = left? Watch closely.

  4. The dragon: left = right overwrites left's original 1 immediately. By the time right = left runs, left's old value is already gone.

    example.pyPython 3
    left = 1right = 2left = rightright = left
  5. The dragon: So right = left doesn't copy the OLD left — it copies the NEW left, which is already 2. Both boxes end up the same.

    example.pyPython 3
    left = 1right = 2left = rightright = leftprint(left, right)
    Output2 2
  6. The dragon: left, right = right, left never has this problem, because Python reads BOTH old values — building the pair (right, left) — before changing either box.

    example.pyPython 3
    left = 1right = 2left, right = right, leftprint(left, right)
    Output2 1
  7. The dragon: One more trick: left = right = 0 sets BOTH boxes to 0 at once. It's the same right-side-first rule, just with one value instead of two.

  8. The dragon: left = right = 0 gives them the same starting value — but they're still two separate boxes from then on.

    example.pyPython 3
    left = right = 0left = left + 1print(left, right)
    Output1 0

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    left = 1, right = 2, then left = right, then right = left. What does print(left, right) show?

    question.pyPython 3
    left = 1right = 2left = rightright = leftprint(left, right)
    • 2 2Correct
    • 2 1 — they swapped
    • 1 2 — nothing changed

    Why: left = right overwrites left's 1 with right's 2 — that original 1 is gone for good. Then right = left copies the CURRENT left, which is already 2. Both boxes end up holding 2, the value right started with. This is exactly why a real swap needs left, right = right, left (or a temporary third box) instead.

  2. Question 2 of 2

    left = 5, right = 9, then left, right = right, left. What does print(left, right) show?

    question.pyPython 3
    left = 5right = 9left, right = right, leftprint(left, right)
    • 9 5 — a real swapCorrect
    • 9 9 — same problem as the naive swap
    • 5 9 — nothing changed

    Why: left, right = right, left builds the pair (right, left) — (9, 5) — using both OLD values first, before touching left or right at all. Only then does it assign 9 to left and 5 to right. That's the whole trick: read everything first, assign everything after, so neither box's old value can be lost mid-swap the way the naive version loses it.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    left = 5 and right = 9. Print both, labelled, before anything happens. Then swap them in one line — left, right = right, left — and print both again, labelled. left should now be 9, and right should now be 5.

  2. +2

    PracticeTwo more, on your own

Try it yourself — in real Python.

This lesson is part of a free topic: every lesson in it is free, and no card is needed.

Start this lesson for real

Stuck three times? The game offers the answer — unasked.