How to Swap Two Variables using Python?

PythonServer Side ProgrammingProgramming

By using a temporary variable −

>>> x=10
>>> y=20
>>> z=x
>>> x=y
>>> y=z
>>> x,y
(20, 10)

Without using temporary variable

>>> a,b=5,7
>>> a,b
(5, 7)
>>> a,b=b,a
>>> a,b
(7, 5)
raja
Published on 28-Feb-2018 00:16:55
Advertisements