Here is a simple Python program to swap two variables:
# Declare two variables x = 5 y = 10 # Swap the values of the two variables x, y = y, x # Print the values of the variables after swapping print("x =", x) print("y =", y)
This program first declares two variables
y and assigns them initial values
10, respectively. Then, it uses multiple assignment to swap the values of the two variables. Finally, it prints the values of the variables after swapping. The output of this program would be:
x = 10 y = 5
Note that this program uses the multiple assignment feature of Python, which allows us to assign multiple variables at the same time. This is a convenient and efficient way to swap the values of two variables in Python.
Thanks for reading. Happy coding!