Python program to Swap Two Values


#Python program to swap two values 

#To take input from the user 
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

x = 5
y = 10

#create a temporary variable and swap the values 
temp = x
x = y
y = temp

print('The value of x after swapping: {}' .format(x))
print('The value of y after swapping: {}' .format(y))


Output

The value of x after swapping: 10
The value of y after swapping:  5


Source code : Without using third variable 

x,y = y,x

Addition and subtraction

x = x + y 
y = x - y 
x = x - y 

Multiplication and Division

x = x * y 
y = x / y 
x = x / y 

XOR swap

x = x ^ y 
y = x ^ y 
x = x ^ y