Python

Python Program to Swap Two Variables1 min read

Write a program to Swap Two Variables in Python

Code:




Output:

The code you provided is a simple Python program that swaps the values of two variables, x and y.

The program starts by using the input() function to prompt the user to enter the values of the variables x and y. The user’s input is stored as a string.

Next, the program creates a temporary variable temp, and assigns the value of x to it. Then it assigns the value of y to x and the value of temp to y. This effectively swaps the values of x and y.

Finally, the program uses the print() function to output the values of x and

yafter the swap to the console. Theformat()method is used to insert the values ofxandyinto the strings that are passed to theprint() function.

The output will depend on the input provided by the user. For example, if the user inputs “5” for x and “10” for y, the output will be “The value of x after swapping: 10” and “The value of y after swapping: 5”

Leave a Comment