Have you ever tried to know how to swap two numbers in Python? This is a common question among developers, and in this article, we will explore 6+ detailed solutions step-by-step.
Swapping two numbers means exchanging their values with each other. The word “swap” itself clearly indicates the act of switching or exchanging. In programming, swapping is used in sorting algorithm, data manipulation, mathematical calculations, memory management and many more programming task.
1. Using a Temporary Variable
In this method, we will simply store first variable value into a temporary variable and then assign second variable value into first variable, now we will transfer temporary variable value into second variable, following these steps we can easily swap two numbers in python without using any third variable.
x = 50
y = 500
temp = x
x = y
y = temp
print("Value of x:", x)
print("Value of y:", y)
Output:
Value of x: 500
Value of y: 50
2. Using the Comma Operator for Swapping
This is simplest Python method. This method uses the comma “,” operator to swap two numbers.
x = 800
y = 325
x, y = y, x
print("Value of x:", x)
print("Value of y:", y)
Output
Value of x: 325
Value of y: 800
You can also read:
3. Using Arithmetic Operators
This method swaps values by adding and subtracting them, so there is no need for extra memory.
# Swap using arithmetic operators
a = 5
b = 10
a = a + b # a becomes 15
b = a - b # b becomes 5
a = a - b # a becomes 10
print("After swapping: a =", a, ", b =", b)
Output
After swapping: a = 10, b = 5
4. Using Multiplication and Division Operators
This method swaps numbers using multiplication and division, but it only works if neither of the numbers is zero .
# Swap using multiplication and division
a = 5
b = 10
a = a * b # a becomes 50
b = a // b # b becomes 5
a = a // b # a becomes 10
print("After swapping: a =", a, ", b =", b)
Output
After swapping: a = 10, b = 5
5. Using Bitwise Operators
Program to swap two number Python. Bitwise operators perform swaps at the binary level.
# Swap using bitwise AND and OR
a = 5
b = 10
a = a | b # OR operation
b = a & b # AND operation
a = a & ~b # Negation of b
print("After swapping: a =", a, ", b =", b)
Output
After swapping: a = 10, b = 5
6. Using Bitwise XOR
In below code, we will perform XOR on two variables three times. After 3rd XOR, we can swap the values without any third variable.
# Swap using XOR
a = 5
b = 10
a = a ^ b # XOR operation
b = a ^ b # XOR operation
a = a ^ b # XOR operation
print("After swapping: a =", a, ", b =", b)
Output
After swapping: a = 10, b = 5
7. Using a User-Defined Function
We can define a function that accepts two numbers as parameters and returns the swapped data.
# Swap using a user-defined function
def swap_numbers(a, b):
return b, a
a = 5
b = 10
a, b = swap_numbers(a, b)
print("After swapping: a =", a, ", b =", b)
Output
After swapping: a = 10, b = 5
8. Handling Input from the User
You can capture the input from user dynamically to perform swap operation.
# Swap with user input
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
a, b = b, a # Using comma operator for swap
print("After swapping: a =", a, ", b =", b)
Output
Enter the first number: 7 Enter the second number: 12 After swapping: a = 12, b = 7
Common Errors and Debugging Tips
- Division by Zero: Ensure values are non-zero when using multiplication and division.
- Type Mismatch: Use int() to convert input values when taking user input.
- Bitwise Methods: Avoid these for non-integer types.
Use Cases of Swapping Numbers in Python
- Swapping values in algorithms: Used in bubble sort, quick sort, etc.
- Data structure manipulation: Swapping values in arrays, lists, or tuples.
- Swapping variables in simulations: Dynamic memory allocations and thread switches.