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

## Getting Started

The task is to swap two variables a and b in python without using any other variable.

Let’s say **a = 5** and **b = 7**, then output should be **a = 7** and **b = 5**.

There are multiple ways to achieve above tasks.

- Using Tuple Packing and Unpacking
- Using XOR bitwise operation
- Using Multiplication and Division Operation
- Using Bitwise Operation

## 1. Using Tuple Packing and Unpacking

Python provides the simplest way, i.e. Tuple Packing and Unpacking, to swap two variables without using third variable as shown below –

num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = num2, num1 print("After swap: num1 =", num1, "num2 =", num2)

Here,

- Statement
**num1, num2 = num2, num1**does the actual swapping process. Rest of the lines in above code example are just to show the output.

Output:

Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5

## 2. Using XOR bitwise operation

We can also use XOR bitwise operation to write python program to swap two variables without using third variable 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: 5 Enter the value of b: 10 After swapping: a = 10 b = 5

## 3. Using Multiplication and Division Operation

We can also use multiplication and division operation to swap two variables without using third variable in python as shown 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)

Here,

- We have used multiplication and division only to swap two variables in python.

Output:

Before swapping: Value of a: 10 and b: 20 After swapping: Value of a: 20.0 and b: 10.0

## 4. Using Bitwise Operation

We can also use bitwise operation to swap variables without using third variable and print them in python 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: 5 and b: 10 After swapping: Value of a: 10 and b: 5

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