Multiple ways to Swap two Variables in Python

In this Python example series, we will cover the program to swap two variables.

1. What is Swapping and How to Swap two numbers?

Swapping two numbers or variables simply means that the first variable will get the value of the second variable and vice versa.

In python, there are different ways by which we can swap two variables. We will discuss each of them with their proper implementation and understanding.

Before deep-diving into the topic, some of the topics which may be beneficial for understanding the program implementation better are:

Let’s understand each of the implementations one by one.


2. Swap Two variables {values} using third Variable

In this type of swap, we generally take one extra variable apart from the two data stored variables for making a swap between them. Let’s add two numbers from the basic method by using the addition operator.

#Python Program to Swap two variables using third variable

  # We can take user input as well
  # num1 = int(input("Enter first number"))

num1 = 9
num2 = 1

print("Variable values before swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
 
temp = num1
num1 = num2
num2 = temp

print("\nVariable values after swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
Output
Variable values before swap 
Value of num1:  9 
Value of num2:  1

Variable values after swap 
Value of num1:  1 
Value of num2:  9

In the above program:

  • First, the temp variable will store the num1 variable’s value.
  • Second, the num1 variable will now store the value of num2 variable.
  • Third, the num2 will get the value of temp variable which had the value of num1.

3. Swap Two variables without using third variable in Python

In Python, there are several different techniques by which we can swap two variables and it will not require the use of any third variable.

The advantage of not using any third variable is that it saves storage. Taking one more variable will definitely take one space more as compared to not taking any extra variable.

3.1. Swap two variables at their position

In Python, there is a simple trick by which we can swap two variables usta t their location.

Let’s see how it works with program implementation.

#Python Program to Swap two variables at same position { without using third variable}

  # We can take user input
  # num1 = int(input("Enter first number"))

num1 = 14
num2 = 51

print("Variable values before swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
 
num1, num2 = num2, num1

print("\nVariable values after swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
Output
Variable values before swap 
Value of num1:  14 
Value of num2:  51

Variable values after swap 
Value of num1:  51
Value of num2:  14

In the above program, the value of num2 will be assigned to num1 and vice versa at their location. This is one of the easiest ways to swap two variables.

3.2. Swap two variables using Bitwise XOR Operator

One of the ways is using the Bitwise XOR gate. This method is only limited to work with integers because it uses bit operation. This method is fast to perform.

Let’s implement the code to see them working.

#Python Program to Swap two variables using XOR operator { without using third variable}

num1 = 14
num2 = 51

print("Value of num1: ", num1, "\nValue of num2: ", num2)
 
num1 = num1 ^ num2
num2 = num1 ^ num2
num1 = num1 ^ num2

print("\nVariable values after swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
Output
Value of num1:  14 
Value of num2:  51

Variable values after swap 
Value of num1:  51 
Value of num2:  14

3.3. Swap Two variables using Arithmetic Operator

The final way of swapping two variables is by using arithmetic operators. By using simple addition and subtraction operations.

Let’s see the implementation of the program.

#Python Program to Swap two variables using Arithmetic Operator { without using third variable}

num1 = 3450
num2 = 1267

print("Value of num1: ", num1, "\nValue of num2: ", num2)
 
num1 = num1 + num2 
num2 = num1 - num2
num1 = num1 - num2

print("\nVariable values after swap ")
print("Value of num1: ", num1, "\nValue of num2: ", num2)
Output
Value of num1:  3450 
Value of num2:  1267

Variable values after swap 
Value of num1:  1267 
Value of num2:  3450

In the above program, the sum of both the numbers is stored in num1 variable, then from the new num1 value num2 is removed and the resultant value is stored in num2 and finally num1 is assigned the value from whole removing the new num2.


4. Conclusion

In this article, we discussed how can we swap two numbers in python using different ways. We used:

  • One way by using third variable
  • Three different way by not using third variable

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index