Python Program to Swap two Integer Numbers without Using Third Variable

In this article, we will learn about how to write python program to swap two integer numbers without using third variable.

Getting Started

The task is to swap two numbers in python without using third variable.

For example,

Assume a = 100 and b = 200, then output should be a = 200 and b = 100

Now, we need to write python program to swap two integer numbers without using third variable.

There are multiple ways to achieve above tasks.

  1. Using Tuple Packing and Unpacking
  2. Using XOR bitwise operation
  3. Using Multiplication and Division Operation
  4. Using Bitwise Operation

1. Using Tuple Packing and Unpacking

This is simplest way to swap integer numbers without using third variable. Here, we use tuple packing and unpacking technique.

It can be done as shown below –

a = 50
b = 60
print("Before swap: a =", a, "b =", b)
a, b = b, a
print("After swap: a =", a, "b =", b)

Here,

  • Statement a, b = b, a does the actual swapping process.

Output:

Before swap: a = 50 b = 60
After swap: a = 60 b = 50

2. Using XOR bitwise operation

XOR operation can also be used to write python program to swap two integer numbers without using third variable. It can be done as shown below –

a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))

# Swapping without using a third variable
a = a ^ b
b = a ^ b
a = a ^ b

# Output swapped values
print("After swapping:")
print("a =", a)
print("b =", b)

Here,

  • We have used XOR operator, i.e. ^, for our swap operation.

Output:

Enter the value of a: 50
Enter the value of b: 60
After swapping:
a = 60
b = 50

3. Using Multiplication and Division Operation

If we won’t want to use XOR operation, we can use multiplication and division operation too. This is similar to what we have seen in above program. Using multiplication and division operation, below is the sample python program to swap two integer numbers without third variable.

It can be shown as below –

a = 10
b = 20

# Print the values before swapping
print("Before swapping:")
print("Value of a:", a, "and b:", b)

# Swapping without using a third variable
a = a * b  # Multiply a by b. Then, assign the result to a
b = a / b  # Divide a (i.e now, it is a*b) by b. Then, assign the result to b
a = a / b  # Divide a by b. Then, assign the result to a

# Print the values after swapping
print("After swapping:")
print("Value of a:", a, "and b:", b)

Output:

Before swapping:
Value of a: 50 and b: 60
After swapping:
Value of a: 60 and b: 50

4. Using Bitwise Operation

Bitwise operation can also be used to write python program to swap two integer numbers without using third variable as shown below –

a = 5
b = 10

print("Before swapping:")
print("Value of a:", a, "and b:", b)

# Swapping without using a third variable using bitwise operations
a = (a & b) + (a | b)  # a = (a AND b) + (a OR b)
b = a + (~b) + 1      # b = a + (~b) + 1 (two's complement)
a = a + (~b) + 1      # a = a + (~b) + 1 (two's complement)

print("After swapping:")
print("Value of a:", a, "and b:", b)

Here,

  • We are using two’s complement feature and swapping the variables.

Output:

Before swapping:
Value of a: 50 and b: 60
After swapping:
Value of a: 60 and b: 50

We have also covered many other ways to swap two numbers in python. You can have a look at them if you want. Learn more about python at official site.

Leave a Reply