top of page

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

0 comments

Comments


bottom of page