The Tech Platform

Dec 16, 20211 min

Program to Swap two Variables in Python

The most native approach is to store the value of one variable(say x) in a temporary variable, then assigning the variable x with the value of variable y. Finally, assign the variable y with the value of the temporary variable.

Code:

X = int( input("Please enter value for X: "))
 
Y = int( input("Please enter value for Y: "))
 

 
# To swap the value of two variables
 
# we will user third variable which is a temporary variable
 
temp_1 = X
 
X = Y
 
Y = temp_1
 

 
print ("The Value of X after swapping: ", X)
 
print ("The Value of Y after swapping: ", Y)

Output:

Read More:

The Tech Platform

www.thetechplatform.com

    0