How to swap two numbers in python?
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)
- By using comma operator.
e.g.,
a=12 b=24 a, b= b, a print(a, b)
- By using addition and subtraction.
e.g.,
x=22 y=56 x=x+y y=x-y x=x-y print(x, y)
This is all about swapping two numbers in python.
Subscribe
Login
Please login to comment
0 Discussion