Python – Swap Two Numbers
To swap two numbers, say a,b , in Python, you can use assignment operator as shown below:
a,b = b,a
Thats it. Now b holds the original value of a. And a holds the original value of b. Let use see an example.
Example
In this example, we take two variables holding numbers. Then we swap the data in those variables.
Output
a: 45
b: 5
Summary
In this tutorial of Python Examples, we learned how to swap two numbers, using assignment operator.
Next Topic ⫸ Python - Tower of Hanoi
⫷ Previous Topic Python - Sum of squares of first N natural numbers