1.2.2 Multiple Variable Assignment
Python allows assigning values to multiple variables in a single statement.
This feature makes code more concise and expressive.
Assigning Multiple Values
Multiple variables can be assigned at the same time using tuple unpacking.
This is commonly used to:
- Initialize several variables at once
- Swap variable values
- Assign values returned from functions
Value Swapping
Python supports swapping values without a temporary variable.
This improves readability and reduces the chance of errors compared to traditional approaches.
Assignment Behavior
When performing multiple assignment:
- The number of variables must match the number of values
- Values are assigned from left to right
- A mismatch raises an error
📌 Note:
Multiple assignment improves clarity when used appropriately, but avoid overusing it in complex expressions.