How to swap two numbers in python?
![](https://pythonpoint.net/wp-content/uploads/2020/11/Yellow-Fishing-Boat-Blog-Banner-2-1024x576.jpg)
Swapping basically means interchanging the data of two variable. Here we’ll how we can two numbers in python.
- By using temporary variable.
e.g.,
a=10 b=20 temp=a a=b b=temp print(a, b)
![](https://pythonpoint.net/wp-content/uploads/2020/11/Screenshot-865-1024x442.png)
- By using comma operator.
e.g.,
a=12 b=24 a, b= b, a print(a, b)
![](https://pythonpoint.net/wp-content/uploads/2020/11/Screenshot-867-1024x481.png)
- By using addition and subtraction.
e.g.,
x=22 y=56 x=x+y y=x-y x=x-y print(x, y)
![](https://pythonpoint.net/wp-content/uploads/2020/11/Screenshot-869-1024x515.png)
This is all about swapping two numbers in python.
Subscribe
Login
Please login to comment
0 Discussion