Python Program to Swap Two Variables

Python Program to Swap Two Variables

2 mins read3.1K Views Comment
Updated on Aug 8, 2024 12:12 IST

In this article, we will learn how to swap two variables using the Python programming language. Here’s the agenda!

swap two variables using Python

Python Program to Swap Two Variables

There are multiple ways to do this. So, Swapping 2 variables in python simply means swapping their values!

swapping

Explore- Python Online Courses & Certifications

For example consider 2 variables a and b and their values to be 10 and 20. So using python, we can swap the values of a and b to be 20 and 10. So without further delay, let us jump into examples and see what are the different ways in which you can swap 2 variables using python.

Also Read: Why Learn Python? Reasons and Top Resources to Learn Python

Program 1: Using Arithmetic Operators

We can make use of arithmetic operators in python to swap 2 variables

  • Using the addition and subtraction operator

The goal is to find the sum in one of the two numbers provided. The sum and subtraction from the sum can then be used to swap the integers.

  • Using multiplication and division operator

The goal is to multiply the two integers that are supplied. The division can then be used to calculate the numbers.


 
#declaring 2 variables
i = 3
j = 4
#swapping the values of 2 variables using addition and subtraction operator
i = i+j
j = i-j
i = i-j
#printing values after swapping
print("value of i after swapping : ",i)
print("value of j after swapping : ",j)
#declaring 2 variables
c = 5
d = 6
#swapping the values of 2 variables using multiplication and division operator
c = c*d
d = c/d
c = c/d
#printing values after swapping
print("value of c after swapping : ",c)
print("value of d after swapping : ",d)
Copy code

Output:

output1
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Program 2: Using Bitwise addition and subtraction for swapping

Here we use Bitwise operation of addition and subtraction for swapping 2 variables


 
#declaring 2 variables
e = 8
f = 9
#swapping the values of 2 variables Using Bitwise addition and subtraction for swapping
e = (e & f) + (e | f)
f = e + (~f) + 1
e = e + (~f) + 1
#printing values after swapping
print("value of e after swapping : ",e)
print("value of f after swapping : ",f)
Copy code

Output:

output2

Program 3: Using XOR Operator

To swap two variables, you can also use the bitwise XOR operator. When you perform an XOR operation on two integers x and y, the result is a number with all bits set to 1 wherever the bits of x and y differ.


 
#declaring 2 variables
y = 1
z = 2
#swapping the values of 2 variables using XOR operator
y = y^z
z = y^z
y = y^z
#printing values after swapping
print("value of y after swapping : ",y)
print("value of z after swapping : ",z)
Copy code

Output:

output3

Program 4: Using Comma Operator / without using the temporary variable


 
#declaring 2 variables
h = 30
v = 60
#swapping the values of 2 variables using comma operator
h,v = v,h
#printing values after swapping
print("value of h after swapping : ",h)
print("value of v after swapping : ",v)
Copy code

Output:

output4
The Difference Between C and Python
Python vs. C++ – What’s the Difference?
All About Polymorphism in Python

Program 5: Using Temporary Variable

The simplest way is to store the value of one variable (say x) in a temporary variable and then assign the value of variable y to the variable x. Finally, assign the value of the temporary variable to the variable y.


 
#declaring 2 variables
a = 20
d = 70
#swapping the values of 2 variables using temp variable
temp = a
a = d
d = temp
#printing values after swapping
print("value of a after swapping : ",a)
print("value of d after swapping : ",d)
Copy code

Output:

output5

Conclusion

Hope this article was helpful for you to understand how to swap 2 variables!

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio