Python Program to Swap Two Variables

Python Program to Swap Two Variables

2 mins read3.1K Views Comment
Updated on Jan 19, 2023 14:42 IST

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

2022_07_swap-numbers.jpg

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

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
The Difference Between C and Python
This article provides a detailed comparison between the programming languages C and Python. It explores their differences, similarities, applications, advantages, and disadvantages. The article explains the fundamental nature of C,...read more
Python vs. C++ – What’s the Difference?
Python vs. C++ – What’s the Difference?
Python and C++ both are general-purpose programming languages. However, their usage and syntax differ widely. In this article, we will highlight the most prominent differences between the two languages.
All About Polymorphism in Python
All About Polymorphism in Python
In Python, polymorphism means the same function name is being used for different types. In this article, we will learn about polymorphism and how to use it with inheritance.

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