Swap Two Variables in Python

Share on facebook
Share on twitter
Share on pinterest

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

Subscribe to our Newsletter

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Share this post with your friends

Share on facebook
Share on google
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *