Swap Two Variables in Python

In this Basic Python Program, let’s have a look at “How to “Swap Two Variables in Python”.

Here, Swapping means exchanging the value of variables.

If x=2 & y=3

After swapping the value of x & y will change

x=3 & y=2

Python Program To Swap Two Variables in Python

 
x = 3
y = 2

# create a temporary variable temp & swap the values

temp = x
x = y
y = temp

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

 

Output:

The value of x after swapping: 3
The value of y after swapping: 2

Leave a Reply

Shopping cart