Hello everyone, In this post you will learn How to Swap Two Numbers in Python. The sample program has been tested and shared in this post.

# defining a function
def swap(a, b):
print("Before: a = %s, b = %s" % (a, b))
a = a + b
b = a - b
a = a - b
print("After: a = %s, b = %s" % (a, b))
# calling the function
swap(10, 20)

Output

Before: a = 10, b = 20
After: a = 20, b = 10

Tags:

No responses yet

Leave a Reply

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