A Program to Swap Two Variables

In this Python lesson, we will practice how to write different programs to swap two different values using variables. Python has different methods which can be used to swap values and we are going to learn how to use these methods individually to execute this task in this lesson.

Example 1: Swap Values using Temp Variable

# Request inputs from the user
x = input('Enter x value: ')
y = input('Enter y value: ')

# create a temporary variable and swap the values
temp = x
x = y
y = temp

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

Enter x value: 20
Enter y value: 56

The value of x after swapping: 56
The value of y after swapping: 20

In the above program, we created a variable known as temp to save the values of x. The assignment of temp is only temporary and used to swap the value of x during the compilation process. We assigned the value of y to x and the value of temp is assigned to y. This helps us to achieve our purpose of swapping two values of two numbers using the temp variable.

NOTE: The temp variable can be named anything of your choice.

Example 2: Swap Values using Arithmetic [+, -] Operators

Another method of swapping values in Python is by the use of simple arithmetic operators which we learned in the basics of Python programming. Let’s go ahead and look at a very simple example of this method below.

# Request inputs from the user
x = input('Enter x value: ')
y = input('Enter y value: ')

# use of basic arithmetic [+ & -] operator
x = x + y;
y = x - y;
x = x - y;

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

Enter x value: 21
Enter y value: 67

The value of x after swapping: 67
The value of y after swapping: 21

In the above practice, these are the steps involved in swapping the two values where x is assigned the value of 21 and y is assigned the value of 67.

  • x = x + y is equivalent to 21 + 67 and the value of x = 88.
  • y = x – y is equivalent to 21 – 67 and the value of y = -46.
  • x = x – y is equivalent to 21-(-46) and the value of x = 67.

At this final point, we were able to generate x as 67 and y as 21.

The arithmetic method of swapping values accepts only numbers and the arithmetic operators used in this case are + and – and these help us to swap the two values effectively. We can also use other arithmetic operators such * and / as shown below

Example 3: Swap Values using Arithmetic [*, /] Operators

# Request inputs from the user
x = input('Enter x value: ')
y = input('Enter y value: ')

# use of basic arithmetic operators
x = x * y;
y = x / y;
x = x / y;

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

Enter x value: 21
Enter y value: 67

The value of x after swapping: 67
The value of y after swapping: 21

Example 4: Swap Values using a Simple Constructor

# Request inputs from the user
x = input('Enter x value: ')
y = input('Enter y value: ')

# apply simple constructor below
x, y = y, x


print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

Enter x value: 21
Enter y value: 67

The value of x after swapping: 67
The value of y after swapping: 21

We can also use simple constructors in Python to swap different values of variables as shown in the program above.

Example 5: Swap Values using Bitwise XOR operator

# Request inputs from the user
x = input('Enter x value: ')
y = input('Enter y value: ')

# apply simple constructor below
x = x ^ y
y = x ^ y
x = x ^ y

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

Input first value: 21
Input second value: 67

Value of x after swapping = 67
Value of y after swapping = 21

This method can only be used on integer/whole numbers. When decimal numbers or fractions are inserted by the user, it will only generate an error. Below is the step-by-step process involved in swapping values using the above Bitwise XOR operator method where x is assigned 21 and y is assigned 67.

  • x = x ^ y is equivalent to 21 ^ 67 and the value of x = 86.
  • y = x ^ y is equivalent to 86 ^ 67 and the value of y = 21.
  • x = x ^ y is equivalent to 86 ^ 21 and the value of x = 67.

At this final point, we were able to generate x as 67 and y as 21.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials