Skip to main content

Swapping of variables


There are a couple of classical ways to do it. Let the variables be:-

a = 5
b = 10

1. By using temporary variable "temp"(name as per your convenience).

temp = a
a = b
b = temp

2. Without "temp" or any extra variable

a = a + b   // 15
b = a - b   // b becomes 5
a = a - b   // a becomes 10

3. In Python like a Boss!! :D
a, b = b, a

Comments