Python Program to Swap Two Numbers
You can swap two numbers in Python using
a, b = b, a which exchanges their values in one line.Examples
Inputa = 5, b = 10
OutputAfter swapping: a = 10, b = 5
Inputa = -3, b = 7
OutputAfter swapping: a = 7, b = -3
Inputa = 0, b = 0
OutputAfter swapping: a = 0, b = 0
How to Think About It
To swap two numbers, think of exchanging their values so that the first number gets the second number's value and vice versa. You can do this by temporarily holding one value or using Python's ability to swap directly without extra storage.
Algorithm
1
Get the two numbers as input.2
Use Python's tuple unpacking to assign the first number to the second variable and the second number to the first variable.3
Print or return the swapped values.Code
python
a = 5 b = 10 print(f"Before swapping: a = {a}, b = {b}") a, b = b, a print(f"After swapping: a = {a}, b = {b}")
Output
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
Dry Run
Let's trace swapping a=5 and b=10 through the code
1
Initial values
a = 5, b = 10
2
Swap operation
a, b = b, a means a gets 10, b gets 5
3
Final values
a = 10, b = 5
| Step | a | b |
|---|---|---|
| Initial | 5 | 10 |
| After swap | 10 | 5 |
Why This Works
Step 1: Tuple unpacking
Python allows assigning multiple variables at once using a, b = b, a, which swaps values without needing a temporary variable.
Step 2: No temporary storage needed
This method avoids extra memory use by directly swapping values in one statement.
Step 3: Simple and readable
The code is easy to read and understand, making it ideal for beginners.
Alternative Approaches
Using a temporary variable
python
a = 5 b = 10 temp = a a = b b = temp print(f"After swapping: a = {a}, b = {b}")
This is the classic way but uses extra memory for the temporary variable.
Using arithmetic operations
python
a = 5 b = 10 a = a + b b = a - b a = a - b print(f"After swapping: a = {a}, b = {b}")
Swaps without extra memory but can cause overflow with very large numbers.
Using XOR bitwise operator
python
a = 5 b = 10 a = a ^ b b = a ^ b a = a ^ b print(f"After swapping: a = {a}, b = {b}")
Swaps without extra memory and no overflow risk but less readable.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers is a constant time operation because it involves a fixed number of assignments.
Space Complexity
The tuple unpacking method uses constant space, no extra memory beyond the variables themselves.
Which Approach is Fastest?
All methods run in constant time, but tuple unpacking is fastest and most readable, while arithmetic and XOR methods are clever but less clear.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Tuple unpacking | O(1) | O(1) | Readability and simplicity |
| Temporary variable | O(1) | O(1) | Classic method, easy to understand |
| Arithmetic operations | O(1) | O(1) | No extra memory but risk overflow |
| XOR bitwise operator | O(1) | O(1) | No extra memory, no overflow, less readable |
Use
a, b = b, a for the simplest and most Pythonic swap.Beginners often try to swap without a temporary variable using separate assignments, which overwrites values incorrectly.