Home »
Python »
Python programs
Python program for swapping the value of two integers
Here, we are going to learn how to swap the value of two integers using third variable in Python?
Submitted by Anuj Singh, on June 25, 2019
In this article, we will learn how to swap the value of two integers (or can be float)?
The basic idea is:
- Storing the value of one variable (x) in a different variable (namely temporary - temp).
- Then changing the value of x by copying the value of y.
- Then changing the value of y by copying the value of temp.
Three simple steps, three simple variables and its all done.
So here is the code:
x = float(input('ENTER THE VALUE OF X: '))
y = float(input('ENTER THE VALUE OF Y: '))
temp = x
x = y
y = temp
print('X :', x,' Y :', y)
Output:
ENTER THE VALUE OF X: 10
ENTER THE VALUE OF Y: 20
X : 20.0 Y : 10.0
Python Basic Programs »