how to swap two variables
1.Using Temporary Variable
2.Using two variable
3.Using EX-or Gate
4.Swap using two variable and code should be written in single line
Python :
1.Using Temporary Variable :
In this method we have to define three variables one temporary variable and two another variables whos values we want to swap
a= 10
b=20
//let temp be the Temporary Variable
temp =a
a=b
b=temp
so by using above code we can swap the values of variable
Now if we print a and b
a=20
b=10
2.Using Two variables :
We can swap the values of two variables without using the temporary
variables ,this method is also called as special formula method to swap the values ,the formula/syntax is
a = a + b
b = a - b
a = a - b
to understand this formula consider one example where a=5,b=10.
at first step
a = a + b / a =15
b = a - b / b = 5
a = a - b/ a=10
Now if we print a and b values get swap successfully ,This method is vary important and easy, as because it not required third variables, question ask vary often in Interviews that swap the values only with two variable
Now if we print a and b values get swap successfully ,This method is vary important and easy, as because it not required third variables, question ask vary often in Interviews that swap the values only with two variable
3.Using EX-or Gate ;
Here is another method to swap the values of Variables with the help of Ex-or gate and to understand this method we must be familiar with truth table of Ex-or gate here is table
Input Input Output
0 0 0
1 0 1
0 1 1
1 1 0
a = a ^ b
b = a ^ b
a = a ^ b
to understand this formula consider one example where a=5,b=10
a = a ^ b // a = 1111
b = a ^ b //b=0101 …... From Above table
a = a ^ b //a=1010
Now if we Print value of a and b the values get swap
a = 1010 ( Decimal 10)
Input Input Output
0 0 0
1 0 1
0 1 1
1 1 0
a = a ^ b
b = a ^ b
a = a ^ b
to understand this formula consider one example where a=5,b=10
a = a ^ b // a = 1111
b = a ^ b //b=0101 …... From Above table
a = a ^ b //a=1010
Now if we Print value of a and b the values get swap
a = 1010 ( Decimal 10)
b = 0101 (Decimal 5)
so in this way by using Ex-or gate we can swap the values of two variables
4.Swap using two variable and code should be written in single line:
Here are two conditions that we have to swap the values with two variables and the code must be written on single line to execute this there is syntax In python
a , b = b , a
let a =5, b=10
a , b = b , a
Now if we Print a and b the values get swap ,to understand how it actually execute simply debug the code
So These are the 4 ways to swap the variables in Python that we have discuss above
so in this way by using Ex-or gate we can swap the values of two variables
4.Swap using two variable and code should be written in single line:
Here are two conditions that we have to swap the values with two variables and the code must be written on single line to execute this there is syntax In python
a , b = b , a
let a =5, b=10
a , b = b , a
Now if we Print a and b the values get swap ,to understand how it actually execute simply debug the code
So These are the 4 ways to swap the variables in Python that we have discuss above
Thank u😊