Python-Swap Values of Two Variables without Using Third Variable

Write a program to exchange (swap) the values of two variables without using a third variable

# Exchange (Swap) values of two variables without using a third variable
# Author: Mukesh N Tekwani

print("Swapping two numbers without using a temporary variable")
  
x = 32     #use any integer value here
y = 17     #use any integer value here
print("Before swap x = ", x)
print("Before swap y = ", y)
  
x = x - y
y = y + x
x = y - x

#Question: Can the above three statements be written in any order?  

print("After swap x = ", x)
print("After swap y = ", y)

#EndOfProgram
Advertisements
%d