Concept
Tuple Unpacking & Variable Swapping
Assign multiple variables at once by unpacking a tuple: `x, y = 10, 20`. Swap variables instantly with `a, b = b, a`.
python
a, b = 1, 2
a, b = b, a # Swapped! No temporary variable needed!
print(a, b) # 2 1
# 76. Tuple Unpacking & Variable Swapping Assign multiple variables simultaneously by unpacking a tuple: `x, y = 10, 20`. Swap variables cleanly in one line with `a, b = b, a`: ```python a, b = 1, 2 a, b = b, a # Swapped! No temporary variable needed! print(a, b) # 2 1 ``` In other languages, swapping two variables requires introducing an annoying third variable like `temp = a; a = b; b = temp`. Python does it in a single breath.
Checkpoint
How do you swap variables `a` and `b` in modern Python without a temp variable?
Practice
Unpack the tuple `point = (100, 200)` into variables `x` and `y`. Print `x + y`.
01
Tap code blocks below to assemble
Available Blocks