Skip to content
Home » Programming » Python Program to Swap Two Variables

Python Program to Swap Two Variables

Python Program to Swap Two Variables

In this post, we will be investigating the Python Program to Swap Two Variables. We will swap two variables and clarify the linguistic structure; simply referred to as syntax of the program.

Algorithm to Swap Two Variables

In order to swap two variables, we have two methods; using a temporary variable and without using a temporary variable.

Let us first discuss the algorithm to swap two variables using temporary variable.

Step 1: Start
Step 2: Initialize two numbers; a and b
Step 3: Assign the values as follows;
                   temp = a
                   a = b
                   b = temp
Step 4: Display the swapped values
Step 5: End

In the algorithm above, we used the temp variable to carry the value of the variable a temporarily. Then we put the value of b in a and later temp in b. In this way, the values of the two variables are exchanged or swapped.

Now, let us look at the algorithm to swap two variables without using temporary variable.

Step 1: Start
Step 2: Initialize two numbers; a and b
Step 3: Swap the values; a,b = b,a
Step 4: Display the swapped values
Step 5: End

In Python, there is a simple way to construct a program to swap the variables. The algorithm shown above can swap the values of two variables without any temporary variable.

New to Python Programming? Get started with Python Programming!

Swap Two Variables in Python

Focusing on the Python Program to Swap Two Variables, let’s jump into the source code.

Example 1: Program with static input using a temporary variable

a = 2
b = 4

# Swap the values with temporary variable
temp = a
a = b
b = temp

# Display the swapped values
print ("The value of a after swapping: " +a)
print ("The value of b after swapping: " +b)

The output of the above code snippet is;

The value of a after swapping: 4
The value of b after swapping: 2

Example 2: Program with user input using a temporary variable

a = input("Enter value of a: ")
b = input("Enter value of b: ")

# Swap the values with temporary variable
temp = a
a = b
b = temp

# Display the swapped values
print ("The value of a after swapping: " +a)
print ("The value of b after swapping: " +b)

The output of the above code snippet is;

Enter value of a: 9
Enter value of b: 1

The value of a after swapping: 1
The value of b after swapping: 9

Example 3: Program with static input without using temporary variable

a = 7
b = 8

# Swap the values 
a, b = b, a

# Display the swapped values
print ("The value of a after swapping: " +a)
print ("The value of b after swapping: " +b)

The output of the above code snippet is;

The value of a after swapping: 8
The value of b after swapping: 7

Example 4: Program with user input without using temporary variable

a = input("Enter value of a: ")
b = input("Enter value of b: ")

# Swap the values 
a, b = b, a

# Display the swapped values
print ("The value of a after swapping: " +a)
print ("The value of b after swapping: " +b)

The output of the above code snippet is;

Enter value of a: 5
Enter value of b: 6

The value of a after swapping: 6
The value of b after swapping: 5

Additional Tricks:

In the event that the factors are the two numbers; we can utilize integer activities to do likewise. It probably won’t look intuitive at first. In any case, looking at this logically, it is quite simple to sort it out. However, some couple of models are

Addition & Subtraction Only

a = a + b
b = a - b
a = a - b

Multiplication & Division Only

a = a * b
b = a / b
a = a / b

You may also check —>

Leave a Reply

Your email address will not be published. Required fields are marked *