Python Program to Swap Two Variables without using Temporary Variable

Problem: Write a python program to swap values of two variables without using temporary variable.

This python program accepts two numbers and stores them in two seperate variables. Now to exchange the values add first and second variables and store it in first varibale. Now subtract second number from first and store it in second. Lastly subtract second number from first and store it in first. We have successfully exchanged the values of two variables without using any temporary variable.

Python program to Swap Two Variables without using Temporary Variable

x=int(input("Enter value of first variable: "))
y=int(input("Enter value of second variable: "))
x=x+y
y=x-y
x=x-y
print("After swapping: ")
print("Value of First Varibale x is:",x)
print("Value of second variable y is:",y)

Output:

Enter value of first variable: 10
Enter value of second variable: 20
After swapping: 
Value of First Varibale x is: 20
Value of second variable y is: 10