Python program to Swap Two Values

432
Python program to Swap Two Values

In this Progam, we will be creating Python Progam to Swap Two Values. A swap is a subordinate agreement through which two gatherings trade the incomes or liabilities from two unique monetary instruments.

To take input from the user to uncomment the code and comment the x and y

Python program to Swap Two Values [Code]

#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 the 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 

In case of any question feel free to ask me on social media

Also, Read

LEAVE A REPLY

Please enter your comment!
Please enter your name here