Program to Swap Two Variables in Python

Source Code: Using a temporary variable:-

# Python program to swap two variables using third varibale

a = 10
b = 20

temp = a
a = b
b = temp

print('The value of a is :- ', a)
print('The value of b is :- ', b)

output:--

The value of a is :- 20
The value of b is :- 10

Source Code: Without Using a temporary variable:-

# Python program to swap two variables without using third varibale

a = 10
b = 20

a, b = b, a

print('The value of a is :- ', a)
print('The value of b is :- ', b)

output:--

The value of a is :- 20
The value of b is :- 10

 

Total Views: 456