Python Program to Swap Two Variables
In this article, we will learn how to swap two variables using the Python programming language. Here’s the agenda!
- Method-1: Using Arithmetic Operators
- Method-2: Using Bitwise addition and subtraction for swapping
- Method -3: Using XOR Operator
- Method-4: Without using the temporary variable
- Method-5: Using Temporary Variable
- Conclusion
There are multiple ways to do this. So, Swapping 2 variables in python simply means swapping their values!
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 variablesi = 3j = 4#swapping the values of 2 variables using addition and subtraction operatori = i+jj = i-ji = i-j#printing values after swappingprint("value of i after swapping : ",i)print("value of j after swapping : ",j)#declaring 2 variablesc = 5d = 6#swapping the values of 2 variables using multiplication and division operatorc = c*dd = c/dc = c/d#printing values after swappingprint("value of c after swapping : ",c)print("value of d after swapping : ",d)
Output:
Best-suited Python courses for you
Learn Python with these high-rated online courses
Program 2: Using Bitwise addition and subtraction for swapping
Here we use Bitwise operation of addition and subtraction for swapping 2 variables
#declaring 2 variablese = 8f = 9#swapping the values of 2 variables Using Bitwise addition and subtraction for swappinge = (e & f) + (e | f)f = e + (~f) + 1e = e + (~f) + 1#printing values after swappingprint("value of e after swapping : ",e)print("value of f after swapping : ",f)
Output:
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 variablesy = 1z = 2#swapping the values of 2 variables using XOR operatory = y^zz = y^zy = y^z#printing values after swappingprint("value of y after swapping : ",y)print("value of z after swapping : ",z)
Output:
Program 4: Using Comma Operator / without using the temporary variable
#declaring 2 variablesh = 30v = 60#swapping the values of 2 variables using comma operatorh,v = v,h#printing values after swappingprint("value of h after swapping : ",h)print("value of v after swapping : ",v)
Output:
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 variablesa = 20d = 70#swapping the values of 2 variables using temp variabletemp = aa = dd = temp#printing values after swappingprint("value of a after swapping : ",a)print("value of d after swapping : ",d)
Output:
Conclusion
Hope this article was helpful for you to understand how to swap 2 variables!
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