Register Login

Swapping of Two Variables in Python

Updated Jul 06, 2021

Swapping of variables is an interesting and popular program that is taught in many universities and well-known in competitive coding skills. Programmers actually implement swapping of variables in algorithms like the Bubble sort, the Selection sort, and useful when you want to save space and reverse the storage. In this article, you will learn the different ways of swapping variables in Python. Also, this tutorial focuses on the efficiency level of the program and we will compare - which one is the best (and why) among them all.

What happens when we swap?

Let suppose, you have a variable var1 with a value 6 and a variable var2 with another value 4. Now, swapping of values will change var1 to 4 and var2 to 6.

Swap Variable in Python

Method 1: The usual or Native approach:

The most common approach to exchange the value of one variable into another and vice versa is by storing one of the variable's values into a third variable. Although using another extra variable helps in swapping, but adding this variable increases the space complexity of your program.

Program:

var1 = 6
var2 = 4

print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

tempvar = var1
var1 = var2
var2 = tempvar
print("Value of first variable after swap: ", var1)
print("Value of second variable after swap: ", var2)

Explanation:

Here we have first created a variable var1 and assigned a value 6. Again, we assigned another variable var2, and assigned it with 4. Here we have taken a temporary variable named the tempvar where we will put the var1 value. Then we have assigned the var1 with var2's value. Lastly, we have to assign the tempvar's value to var2. Finally, we print the var1 and var2 values one by one. We will see that they have exchanged their value.

Method 2: Using Comma Punctuator:

As you all know that the comma punctuator in Python helps in multiple assignments; we will use the same to assign the values but changing the position.

Program:

var1 = 6
var2 = 4
print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

var1, var2 = var2, var1

print("Value of first variable after swap: ", var1)
print("Value of second variable after: ", var2)

Example 2: Swap Variables with String Value

var1 = 'Apple'
var2 = 'Orange'

print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

var1, var2 = var2, var1

print("Value of first variable after swap: ", var1)
print("Value of second variable after swap: ", var2)

Explanation:

Here we have first created a variable var1 and assigned a value 6. Again, we assigned another variable var2, and assigned it with 4. Python allows assigning multiple values in a single line using the comm punctuator. In this example, you can see that var1 and var2 are assigned with var2 and var1 respectively. Lastly, we print the var1 & var2 values one by one. We will see that their values got exchanged.

Method 3: Using Arithmetic Operators:

Arithmetic operators in Python perform basic mathematical calculations. Python offers seven arithmetic operators:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Modulus %
  • Exponentiation **
  • Floor division //

In this swapping technique, you will use the + and - operator to exchange the values of both variables. Also, this is efficient because it does not use the third variable and reduce space complexity.

Program:

var1 = 6
var2 = 4
print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

var1 = var1 + var2  
var2 = var1 - var2
var1 = var1 - var2

print("Value of first variable after swap: ", var1)
print("Value of second variable before swap: ", var2)

Explanation:

Here we have first created a variable var1 and assigned a value 6. Again, we assigned another variable var2, and assigned it with 4. Now, we will add the var1 and var2 values and assigned them in var1. Then we will subtract var2 from var1 and assign it in var2. Lastly, we will subtract var2 from var1 and assign it in var1. It will exchange the two variable's values. Then we print the var1 & var2 values one by one. We will see that their values got exchanged.

Method 4: Using Multiplication and division operators:

The intention here is to comprehend the multiplication of the two given numbers, which will then get calculated by using the division operator. Also, in this program, you will notice that if you provide integer values to your variables, you will see that these values turned into floating type values. This method is also efficient as it requires only two variables to process the swapping.

Program:

var1 = 6
var2 = 4

print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

var1 = var1 * var2  
var2 = var1 / var2
var1 = var1 / var2

print("Value of first variable after swap: ", var1)
print("Value of second variable before swap: ", var2)

Explanation:

Here we have first created a variable var1 and assigned a value 6. Again, we assigned another variable var2, and assigned it with 4. Now, we will multiply the var1 and var2 values and assigned them in var1. Then we will divide var1 with var2 and assign it in var2. Lastly, we will divide var1 with var2 and assign it in var1. It will exchange the two variable's values. Then we print the var1 & var2 values one by one. We will see that their values got exchanged.

Method 5: Using the Bitwise XOR operator:

Bitwise operators are one of the fastest working operators because they work at bit-level, and hence all the high-level conversions become easy and simple. You can use the Bitwise XOR to swap two variables. It takes two numbers x and y. Then it returns a number that has all the bits as 1 whenever it finds the bits of x and y different. Also, this method is effective because the entire swapping takes only one XOR operator to perform.

Program:

var1 = 6
var2 = 4
print("Value of first variable before swap: ", var1)
print("Value of second variable before swap: ", var2)

var1 = var1 ^ var2  
var2 = var1 ^ var2
var1 = var1 ^ var2

print("Value of first variable after swap: ", var1)
print("Value of second variable before swap: ", var2)

Explanation:

Here we have first created a variable var1 and assigned a value 6. Again, we assigned another variable var2, and assigned it with 4. Now, we will XOR var1 with var2 and assign it in var1. Then we will again XOR var1 with var2 and assign it in var2. And for the last time, we have to XOR var1 with var2 and assign it in var1. Then we print the var1 & var2 values one by one. We will see that their values got exchanged.

Conclusion:

If you want to swap string values then using Comma Punctuator is best

Among these five methods, the use of multiple assignments and Bitwise XOR are the fastest. Bitwise XOR the fastest not only because it is using a single operator to perform the swapping, but also it uses bits to deal with the data. Bitwise operators are faster than addition and subtraction operators. Again, addition and subtraction are faster than multiplication and division operators. So, method 2 and method 5 are the most preferred ways of swapping in terms of efficient time and space complexity.


×