Python Program to swap two numbers without using third variable
Last Updated :
12 Jul, 2025
The task of swapping two numbers without using a third variable in Python involves taking two input values and exchanging their values using various techniques without the help of a temporary variable. For example, if x = 5 and y = 7 then after swapping, x = 7 and y = 5.
Using tuple unpacking
Tuple unpacking is the most preferred way to swap two variables in a single line. It uses Python’s built-in tuple feature, making the swap operation atomic and efficient. This method is widely used because it is both readable and fast.
Python
x, y = 5, 7
print("Before swapping:", x, y)
# Swapping using tuple unpacking
x, y = y, x
print("After swapping:", x, y)
OutputBefore swapping: 5 7
After swapping: 7 5
Explanation:
- x, y = 5, 7 assigns the values 5 to x and 7 to y using tuple packing and unpacking in a single step.
- x, y = y, x swaps the values of x and y by creating a temporary tuple (y, x) and unpacking it back into x and y.
Using airthmetic operations
This approach involves simple arithmetic operations to swap two numbers without using extra memory. It is based on adding and subtracting values but should be used carefully to avoid overflow issues in other languages.
Python
x = 5
y = 7
print("Before swapping:", x, y)
# Swapping using arithmetic operations
x = x + y
y = x - y
x = x - y
print("After swapping:", x, y)
OutputBefore swapping: 5 7
After swapping: 7 5
Explanation:
- x = x + y → x = 5 + 7 = 12 : x holds the sum of both values (12) and y is still 7.
- x: y = x - y → y = 12 - 7 = 5 : Now, y holds the original value of x (5).
- y: x = x - y → x = 12 - 5 = 7 : Now, x holds the original value of y (7).
Using xor bitwise operator
XOR swapping technique operates at the bit level. It is a space-efficient method often used in low-level programming and system development. It avoids using extra space and works well when performance at the bit level is important.
Python
x = 5
y = 7
print("Before swapping:", x, y)
# Swapping using XOR
x = x ^ y
y = x ^ y
x = x ^ y
print("After swapping:", x, y)
OutputBefore swapping: 5 7
After swapping: 7 5
Explanation:
- x = x ^ y → x = 5 ^ 7 = 2 : x holds the XOR result of 5 and 7 (2) and y is still 7.
- y = x ^ y → y = 2 ^ 7 = 5 : Now, y holds the original value of x (5).
- x = x ^ y → x = 2 ^ 5 = 7 : Now, x holds the original value of y (7).
Using multiplcation and division method
This method swaps values using multiplication and division. It is a mathematical approach but is less reliable due to division by zero and precision issues with floating-point numbers. It is not commonly recommended in modern programming practices.
Python
x = 5
y = 7
print("Before swapping:", x, y)
# Swapping using multiplication and division
x = x * y
y = x // y
x = x // y
print("After swapping:", x, y)
OutputBefore swapping: 5 7
After swapping: 7 5
Explanation:
- x = x * y → x = 5 * 7 = 35 : x holds the product of both values (35) and y is still 7.
- y = x // y → y = 35 // 7 = 5 : Now, y holds the original value of x (5).
- x = x // y → x = 35 // 5 = 7 : Now, x holds the original value of y (7).
Python Program to Swap Two Numbers
Similar Reads
Python Program to Swap Two Variables The task of swapping two variables in Python involves exchanging their values without losing any data . For example, if x = 10 and y = 50, after swapping, x becomes 50 and y becomes 10. Using Tuple UnpackingTuple unpacking is the most efficient method as it eliminates the need for extra memory or te
3 min read
Python program to Swap i'th with j'th elements in List Given a list, the task is to write a Python program to the given list of elements, toggle every i and j elements in the list. Examples: Input : test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4], i, j = 4, 8 Output : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8] Explanation : 4 is swapped by 8 at each occurrence. In
4 min read
Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra
3 min read
Python Program to move numbers to the end of the string Given a string, the task is to write a Python program to move all the numbers in it to its end. Examples: Input : test_str = 'geek2eeks4g1eek5sbest6forall9' Output : geekeeksgeeksbestforall241569 Explanation : All numbers are moved to end. Input : test_str = 'geekeeksg1eek5sbest6forall9' Output : ge
6 min read
Python Program to Reverse a Number We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
3 min read
Python3 Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th
3 min read