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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.