Swapping of Two Numbers in Python Without Temporary Variable

Swapping of Two Numbers in Python Without Temporary Variable
(Last Updated On: 13/09/2022)

In this blog, we will be explaining how to write different programs to swap two numbers in python without temporary variables.

What is Swapping?

Swapping means replacing the values of a variable with other variables.

For all the practice Videos and Explanations on Python, See Here Python Practice Series.

Program to Swap Two Numbers in Python Without Temporary Variable

Let’s write a program that Swaps Two Numbers.

Source Code with Explanation

x = int(input("Enter the Value of x:"))
y = int(input("Enter the Value of y:"))

x,y = y,x

print("Values after swapping x=",x,"y=",y)
Output
Enter the Value of x: 50
Enter the Value of y: 60
Values after swapping x= 60 y= 50

To write a swapping program in python, The first takes the content of the variable “x”. Then the content of variable “y” in a variable. Then the content of the x swapped variable goes to y.

If you print the result, the value is swapped.

If you want to learn python programming, you can refer to this Python Online Course with Certification.