Python Program to Swap Two Numbers Without Temp Variable
You can swap two numbers without a temp variable in Python using arithmetic:
a = a + b; b = a - b; a = a - b or tuple unpacking: a, b = b, a.Examples
Inputa=3, b=5
Outputa=5, b=3
Inputa=0, b=0
Outputa=0, b=0
Inputa=-10, b=20
Outputa=20, b=-10
How to Think About It
To swap two numbers without a temporary variable, think about how you can use math operations or Python's ability to assign multiple variables at once. Using addition and subtraction, you combine the values and then separate them again to swap. Alternatively, Python allows swapping directly with tuple unpacking, which is simple and clean.
Algorithm
1
Get the two numbers a and b2
Add b to a and store in a3
Subtract new a by b to get original a and assign to b4
Subtract new a by new b to get original b and assign to a5
Now a and b are swappedCode
python
a = 3 b = 5 # Swap without temp variable using arithmetic print(f'Before swap: a={a}, b={b}') a = a + b b = a - b a = a - b print(f'After swap: a={a}, b={b}')
Output
Before swap: a=3, b=5
After swap: a=5, b=3
Dry Run
Let's trace swapping a=3 and b=5 through the arithmetic method
1
Initial values
a=3, b=5
2
Add b to a
a = 3 + 5 = 8
3
Subtract new a by b to get original a
b = 8 - 5 = 3
4
Subtract new a by new b to get original b
a = 8 - 3 = 5
5
Swapped values
a=5, b=3
| Step | a | b |
|---|---|---|
| Initial | 3 | 5 |
| a = a + b | 8 | 5 |
| b = a - b | 8 | 3 |
| a = a - b | 5 | 3 |
Why This Works
Step 1: Combine values
Adding a + b stores the total in a, holding both values combined.
Step 2: Extract original a
Subtracting a - b gives the original b value, which we assign to b.
Step 3: Extract original b
Subtracting a - b again gives the original a value, which we assign to a.
Alternative Approaches
Tuple unpacking
python
a = 3 b = 5 print(f'Before swap: a={a}, b={b}') a, b = b, a print(f'After swap: a={a}, b={b}')
This is the simplest and most Pythonic way, using Python's multiple assignment feature.
Using XOR bitwise operator
python
a = 3 b = 5 print(f'Before swap: a={a}, b={b}') a = a ^ b b = a ^ b a = a ^ b print(f'After swap: a={a}, b={b}')
This method works only with integers and uses bitwise XOR to swap without extra space.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers involves a fixed number of operations, so it runs in constant time O(1).
Space Complexity
No extra memory is used beyond the variables themselves, so space complexity is O(1).
Which Approach is Fastest?
Tuple unpacking is fastest and most readable in Python, while arithmetic and XOR methods are alternatives but less clear.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Arithmetic | O(1) | O(1) | Numeric types, no extra variable |
| Tuple unpacking | O(1) | O(1) | All types, most readable |
| XOR bitwise | O(1) | O(1) | Integers only, low-level operations |
Use tuple unpacking
a, b = b, a for the cleanest swap in Python.Forgetting that arithmetic swap can cause overflow or errors with non-numeric types.