Skip to content
Home » Swap Two Numbers Python

Swap Two Numbers Python

Consider two variables a and b. Swapping the values of those two variables without using third variable is explained in below code.

a = 20
b = 5
#swapping values
a,b = b,a

#printing the values
print('a :',a)
print('b :',b)

a : 5
b : 20

Program to swap the two numbers using third variable.

a = 3
b = 6

#swapping values
temp = a
a = b
b = temp

#printing values
print('a :',a)
print('b :',b)

a : 6
b : 3

Also Read : How to sort lists in python

Tags: