Write a Python program to Swap two variables


example 1:-

# Python program to demonstrate
# swapping of two variables

x = 10
y = 40

# Swapping of two variables
# Using third variable
temp = x
x = y
y = temp

print("Value of x:", x)
print("Value of y:", y)

output:-

Value of x: 40
Value of y: 10
>>> 

example 2:-

# Python program to demonstrate
# swapping of two variables

x = 10
y = 30

# Swapping of two variables
# using arithmetic operations
x = x + y
y = x - y
x = x - y

print("Value of x:", x)
print("Value of y:", y)

output:-
Value of x: 30
Value of y: 10
>>> 

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post