Sunday, 12 December 2021

Python Swap Variable Program - Function For Swapping Variable In Python

 Please find the below code for swapping the variable in Python with line by line explanation


a = input("a: ") // This input commands get the value from user for "a" variable
b = input("b: ") // This input commands get the value from user for "b" variable

var =a; // This line of code is to store value of "a" in different variable in order to
facilitate swap.
a = b; //Now value of "b" is stored in "a"
b=var; //The mdofiied value of "a" is now stored in "b"

print("a: " + a) //This one prints the swapped value of "a"
print("b: " + b) //This one prints the swapped value of "b"

Note: the print function is used to print a value
     the input function is used to get the input value from the user

No comments:

Post a Comment