Swapping two numbers means exchanging their values. If a = 10 and b = 20, then after swapping a = 20 and b = 10. It is one of the first programs in every programming course, and Python offers more than one way to do it. In this lesson you will see four methods and learn which one to use.
Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.
Method 1: tuple swap (the Python way)
a, b = 10, 20
a, b = b, a
print(a, b)Output:
20 10Python first evaluates the right side (b, a) as a pair, and then assigns the two values to a and b at the same time. No extra variable is needed. This is the shortest and clearest method, and the one you should normally use.
Method 2: using a temporary variable
This method works in almost every programming language, so it is the one most textbooks teach.
a, b = 10, 20
temp = a
a = b
b = temp
print(a, b)Output:
20 10Think of it as moving juice between two glasses with the help of an empty third glass. temp stores the value of a, so that it is not lost when a gets the value of b.
Method 3: using arithmetic (without a third variable)
a, b = 10, 20
a = a + b
b = a - b
a = a - b
print(a, b)Output:
20 10Here is what happens step by step:
Step | a | b |
|---|---|---|
Start | 10 | 20 |
a = a + b | 30 | 20 |
b = a – b | 30 | 10 |
a = a – b | 20 | 10 |
This method works for integers. With decimal numbers it can give small rounding errors:
a, b = 0.1, 0.2
a = a + b
b = a - b
a = a - b
print(a, b)Output:
0.2 0.10000000000000003The value of b is not exactly 0.1 any more. So prefer Method 1 for decimal numbers.
Method 4: using XOR (integers only)
a, b = 10, 20
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b)Output:
20 10The ^ operator is bitwise XOR. This trick is popular in interviews, but it works only for integers and is harder to read.
Swap other things
The tuple swap works for any values, such as strings and list items.
x, y = 'x', 'y'
x, y = y, x
print(x, y)
numbers = [1, 2, 3, 4]
numbers[0], numbers[-1] = numbers[-1], numbers[0]
print(numbers)Output:
y x [4, 2, 3, 1]
Which method should you use?
Method | Extra variable | Works for | Use it when |
|---|---|---|---|
Tuple swap | No | Any value | Almost always |
Temporary variable | Yes | Any value | You want the classic approach |
Arithmetic | No | Integers (best) | Interview practice |
XOR | No | Integers only | Interview practice |
Take the numbers from the user (on your computer)
a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
a, b = b, a
print('After swapping:', a, b)Our online compiler does not support input() yet, so use fixed values there.
Common mistakes
- Writing a = b and then b = a. Both variables end up with the same value, because the old value of a is lost.
- Using the arithmetic method with decimals. It can give rounding errors.
- Swapping without saving. If you print inside the function but do not return the values, the swap is lost outside the function.
Practice questions
- Swap two strings using the temporary variable method.
- Swap the first and last items of a list.
- Swap three variables in a cycle, so that a gets b, b gets c, and c gets a.
- Write a function that returns two numbers in swapped order.
FAQs
How do you swap two numbers in Python without a third variable?
Use a, b = b, a. You can also use the arithmetic or XOR method for integers.
Is the tuple swap really a tuple?
Yes. b, a creates a tuple, and Python then unpacks it into a and b.
Which method is the fastest?
For normal programs, the difference is too small to matter. Use the clearest one, which is the tuple swap.
