Swapping basically means interchanging the data of two variable. Here we’ll how we can two numbers in python.
- By using temporary variable.
a=10 b=20 temp=a a=b b=temp print(a, b)
- By using comma operator.
a=12 b=24 a, b= b, a print(a, b)
- By using addition and subtraction.
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.