Lesson 12 of 55
8 mins readPython Multiple Assignment & Variable Swapping
In Plain English
Lua evaluates all right-hand expressions before assigning them to the left-hand variables, enabling one-line variable swaps without temporary variables.
Deep Dive: How It Works
Parallel Assignment: `local a, b, c = 1, 2, 3`.
Atomic Swap: `x, y = y, x` swaps two variables instantly.
Adjusting Counts: Excess values on the right are discarded; missing values are set to `nil`.
Core Rules to Remember

Atomic Evaluation: Right-hand values are evaluated before any assignment takes place.

Swapping Idiom: `a, b = b, a` cleanly swaps two variables.
Live Interactive Example
Hit Run Code to see it liveMultiple Assignments and Swaps
Python 3.12
1
2
3
4
5
6
7
8
Output Console
Click "Run Code" to view the rendered output.
How it works: x and y swapped values in a single step, and r was assigned nil.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Swap Name Strings
Declare `local first, second = "Alice", "Bob"`.
Swap them: `first, second = second, first`.
Print `"First: " .. first .. " Second: " .. second`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.