How to Swap Two Variables Without Using a Temporary Variable in Python
Swapping two variables without using a temporary variable is a popular Python interview question and a common requirement in many algorithms. Python makes this operation elegant and easy with several built-in techniques. In this article, you'll learn multiple ways to swap two variables in Python without a temporary variable, complete with code examples, outputs, and clear explanations.
Table of Content
What is Variable Swapping?
- Definition: Swapping means exchanging the values of two variables so that each variable takes the value of the other.
- Use Cases: Sorting algorithms, data manipulation, temporary value storage, and more.
- Why without temp variable? Saves memory, reduces code, and is more Pythonic.
01. Tuple Unpacking (Pythonic Swap)
This is the most recommended and Pythonic way to swap two variables without using a temporary variable. It leverages tuple packing and unpacking in a single line.
a = 5
b = 7
a, b = b, a
print("a =", a, "b =", b)
Output:
a = 7 b = 5
- On the right side,
(b, a)
creates a tuple with the values swapped. - On the left side,
a, b
unpacks the tuple, assigning the swapped values back. - No extra variable is needed—memory efficient and atomic operation.
- Works with any data type (numbers, strings, objects, etc.).
- This is the canonical and most readable way to swap variables in Python[1][2][3][5][7][8][9].
02. Swap Using Arithmetic Operations
If the variables are numbers, you can swap them using arithmetic operations without a temporary variable.
a = 11
b = 7
a = a + b
b = a - b
a = a - b
print("a =", a, "b =", b)
Output:
a = 7 b = 11
- Adds both values and stores the sum in
a
. - Subtracts the new value of
b
froma
to get the originala
inb
. - Subtracts the new value of
b
froma
to get the originalb
ina
. - No extra variable is used—saves memory.
- Works only with numbers (integers, floats).
- Be careful with very large numbers (overflow is rare in Python but possible in other languages)[2][3][4][5].
03. Swap Using XOR Bitwise Operator
This method uses the XOR bitwise operator to swap two integer variables without a temporary variable.
a = 4
b = 8
a = a ^ b
b = a ^ b
a = a ^ b
print("a =", a, "b =", b)
Output:
a = 8 b = 4
- Uses bitwise XOR to swap values without extra storage.
- Works only with integers.
- Rarely used in modern Python—tuple unpacking is clearer and safer.
- Good for understanding bitwise operations and interview questions[2][4][5].
04. Comparing Swap Methods Without Temporary Variable
Method | Extra Variable | Works With | Pythonic | Notes |
---|---|---|---|---|
Tuple Unpacking | No | All types | Yes | Recommended in Python |
Arithmetic | No | Numbers only | No | Be careful with types/overflow |
XOR | No | Integers only | No | Rarely used in Python |
Conclusion
Swapping two variables without a temporary variable in Python is simple and efficient using tuple unpacking. Arithmetic and XOR methods are also available for numbers and integers, but tuple unpacking is preferred for its readability and versatility. Mastering these techniques will help you write cleaner, more Pythonic code for interviews and real-world applications.
a, b = b, a
for the most Pythonic and efficient swap—no temp variable needed!
Comments
Post a Comment