Python Examples

Python Program to Swap Two Variables 

There are times when one needs to exchange two variables. When such a case occurs, the program for Python swap first asks the user to enter two variables that need to be swapped. 

python swap

Source

The process to swap the variables may require a third variable. Along with it, one should understand that the operator that plays the most important role in such a program is the assignment operator, or, the ‘=’.  

Source Code: Using a Temporary Variable 

The source code for Python swap two variables is provided below. It first asks the user to enter two variables. Then, with the help of a third variable, it will swap the values of both variables.

                    

var1 = input (“Enter the first variable”)

var2 = input (“Enter the second variable”)

temp = var1

var1 = var2 

var2 = temp

print (“The value of the first variable after swapping is:”, var1)

print (“The value of the second variable after swapping is:”, var2)

 

In the program, the temp variable is used as a temporary third variable that holds the value of the first variable. After that, the value of the second variable is assigned to the first variable. The third step is to assign the value stored in the temporary variable, that is, the value of the first variable, to the second variable. 

This way, the value of var1 and var2 is exchanged by using a third variable as a placeholder. 

Source Code: Without Using Temporary Variable 

The Python swap of the two variables can also be created without a third temporary variable as a placeholder. The source code that is provided below works the same as the above code. However, it directly prints the value of the first variable as the value of the second variable and vice versa. 

The program provided below uses the comma operator. 

                    

var1 = input (“Enter the first variable”)

var2 = input (“Enter the second variable”)

var1, var2 = var2, var1

print (“The value of the first variable after swapping is:”, var1)

print (“The value of the second variable after swapping is:”, var2)

 

Another way through which the Python swap program to exchange the values of two variables can be created is with the help of the XOR operator. 

The XOR operator is a bitwise operator. The XOR operator of two numbers will return a number that has all the bits as 1 when the bits between the two numbers differ. Here is how the source code will look like. 

                    

var1 = input (“Enter the first variable”)

var2 = input (“Enter the second variable”)

var1 = var1 ^ var2

var2 = var1 ^ var2

var1 = var1 ^ var2

print (“The value of the first variable after swapping is:”, var1)

print (“The value of the second variable after swapping is:”, var2)

 

All these methods provide the same result. However, the program that does not use an additional variable is much more efficient as it requires less memory space. 

 Frequently Asked Questions 

Is there a swap function in Python?

There is no pre-defined or in-built swap function available in Python. The comma operator can be used instead to swap the value of two variables. 

How do you swap numbers in Python?

One can swap numbers in Python by creating an additional variable that acts as a placeholder or with the comma or XOR operator. 

How do you swap two numbers in a list in Python?

To swap the values of two numbers in a list, the index of those numbers should be known. The process of swapping will be the same as provided above. 

How do I swap a list in Python?

To swap a complete list in Python, save the list in a variable and exchange the value of this variable with another. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.