to swap value of two variables, Without using any third variable, see this code
a='Aron'
b='Byte'
print("a is {0} b is {1}".format(a,b))
a,b=b,a
print("swap done : a is {0} b is {1}".format(a,b))
will print like this
a is Aron b is Byte
swap done: a is Byte b is Aron
here magic of Python is swap value with just a,b =b,a
a,b =b,a is short hand code of a=b and b=a
How to swap three variables values with each other in Python?
hmm , same as above by using short hand code
we simply rotating value to each other in three variables
a='Aron'
b='Byte'
c='Rand'
print("a is {0} b is {1}".format(a,b))
a,b,c=c,a,b
print("swap done: a is {0} b is {1} c is {2}".format(a,b,c))
ouput :
a is Aron b is Byte
swap done: a is Rand b is Aron c is Byte
a,b,c=c,a,b means
a=c
b=a
c=b
so value of "c" is goes to "a" , and value of "a" goes into "b" and value of "b" goes to "c"
you can swap any number variables like
a,b,c,d,e=e,a,b,c,d