Swapping the values of two variables means exchanging their values so that the value of first variable is replaced by the second variable and the value of second variable is replaced by the first.

There are many different ways in which two numbers can be swapped in python.
This article will explain all of them in detail.

Method 1: Using a temporary variable
This method uses a third variable to hold a temporary value. Algorithm for this method consists of following steps.

  1. Assign the value of first variable to the temporary or third variable so that both first and third variables hold the same value.
  2. Assign the value of second variable to the first variable so that now first variable contains the value of second variable.
  3. Finally assign the value of third variable(which holds the value of first variable) to the second variable so that the second variable contains the value of first variable.

Both the variables are now swapped. Example program for this method is given below.

# initialize two variables
first = 10
second = 20
print('Value of first variable before swapping:', first)
print('Value of second variable before swapping:', second)
# store value of first variable
temp = first
# assign second variable to first
first = second
# assign temporary variable to second
second = temp
print('Value of first variable after swapping:', first)
print('Value of second variable after swapping:', second)

Output will be

Value of first variable before swapping: 10
Value of second variable before swapping: 20
Value of first variable after swapping: 20
Value of second variable after swapping: 10

Method 2: Using addition and subtraction
This method swaps two numbers without using temporary variable and is based on the following steps.

  1. Add both the numbers or variables and assign their sum to one of the two variables.
  2. Now, when you subtract the value of a variable from this sum, obviously the result will be the value of the other variable.
    Assign this result to the same variable which was subtracted which means that this variable will now hold the value of other variable.
  3. Now, again subtract the variable in step 2 from the sum and assign the result to the other variable. This variable will now contain swapped value.

Python program based on the above steps is below.

# initialize two variables
first = 10
second = 20
print('Value of first variable before swapping:', first)
print('Value of second variable before swapping:', second)
# store the sum in first variable
first = first + second
# subtract second variable from sum. Remaining value will be first variable.
# assign it to second variable
second = first - second
# subtract first variable from sum. Remaining value will be second variable.
# assign it to first variable
first = first - second
print('Value of first variable after swapping:', first)
print('Value of second variable after swapping:', second)

Output of above code will be

Value of first variable before swapping: 10
Value of second variable before swapping: 20
Value of first variable after swapping: 20
Value of second variable after swapping: 10

Method 3: Using python builtin method
Python provides an in-built method to swap the values of two variables without using a third or temporary variable.
Python program for swapping two variables is given below.

# initialize two variables
first = 10
second = 20
print('Value of first variable before swapping:', first)
print('Value of second variable before swapping:', second)
# swap variables with builtin method
first, second = second, first
print('Value of first variable after swapping:', first)
print('Value of second variable after swapping:', second)

Note the syntax of swapping the variables

first, second = second, first

Reason that above syntax swaps variables is that python evaluates expressions from left to right but while evaluation an assignment(which contains = operator), the right side of = is evaluated first.
Thus, if you evaluate the above assignment from right to left, then this means that second is assigned to first and first is assigned to second.
Output of above program is

Value of first variable before swapping: 10
Value of second variable before swapping: 20
Value of first variable after swapping: 20
Value of second variable after swapping: 10

Method 4: Using multiplication and division
This method is the same as Method 2 above which uses addition and subtraction. This method also does not use any third or temporary variable.
In this method,

  1. First both the variables(say, v1 and v2) are multiplied and the result is assigned to one variable(say, v1).
  2. Now, this product is divided by the other variable(v2). Obviously, the result of division will be the value of other variable(v1) which is assigned to v2.
  3. Now v2 contains the original value of v1 while v1 still contains the product of variables. When v1 is divided by v2, the result will be the original value of v2. This is assigned to v1.

Thus, both the variables are swapped.
Python program written on the above description is given below.

# initialize two variables
first = 10
second = 20
print('Value of first variable before swapping:', first)
print('Value of second variable before swapping:', second)
# multiply both variables
first = first * second
# divide the product by the second variable so that the result will
# be the first variable.
second = int(first / second)
# divide the product by the second variable so that the result will
# be the first variable.
first = int(first / second)
print('Value of first variable after swapping:', first)
print('Value of second variable after swapping:', second)

Note that the result of division is casted to an integer using python’s int function otherwise it will show the values in decimal format.

Output of above code will be

Value of first variable before swapping: 10
Value of second variable before swapping: 20
Value of first variable after swapping: 20
Value of second variable after swapping: 10

This method will not work if the value of any variable is 0.
Method 5: Using XOR operator
XOR operator is represented by the caret(^) symbol. This method is based on the following steps.

  1. Perform XOR operation of both variables(say, v1 and v2) and assign the result to any of the two variables(say, v1).
  2. Perform XOR operation of second variable(v2) with the result of Step 1. Its result will the original value of first variable(v1). Assign it to second variable(v2).
  3. Finally, perform the XOR of Step 1 result with second variable(v2). Result of this operation will the original value of second variable(v2). Assign it to first variable(v1). Both the variables will be swapped.

Python program for this method is written below. This method also does not use any temporary variable.

# initialize two variables
first = 10
second = 20
print('Value of first variable before swapping:', first)
print('Value of second variable before swapping:', second)
# XOR of both variables
first = first ^ second
# XOR of second variable with XOR of both variables
second = second ^ first
# XOR of both variables with XOR of second variable
first = first ^ second
print('Value of first variable after swapping:', first)
print('Value of second variable after swapping:', second)

Output of this program will be as below.

Value of first variable before swapping: 10
Value of second variable before swapping: 20
Value of first variable after swapping: 20
Value of second variable after swapping: 10

Click the clap if the article was useful for you.

Leave a Reply