• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Python
    • Tutorial- Java
    • Tutorial- HTML & CSS
    • Tutorial- MySQL
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2020
  • Linux
  • Trend
    • (AI) Artificial Intelligence
    • BigData
    • Cloud Computing
    • (ML) Machine Learning
  • Write for Us
    • Write for Us
    • Submit Source Code or Program
    • Share Interview Experience
  • Tools

Python Program to Swap Two Variables without using Third Variable

Aniruddha Chaudhari/16992/4
CodePython

In this Python programming, we will see how to swap two values without temporary variable and with temporary variables.

Suppose x and y are the two variables. We want to swap the values of these two variables.

How to Write a Python Program to Swap Two Variables?

Here is the program you can use to swap two values using the third variable.

x = 5
y = 10

print "The value of x before swapping:", x
print "The value of y before swapping:", y

# create a temporary variable 'temp'
# swap two values from two different variables

temp = x
x = y
y = temp

print "The value of x after swapping:", x
print "The value of y after swapping:", y

Here we save the value of x in a temp variable. Overwrite value of x with the value of y. Replace the value of y by temp variable.

But Python makes the thing simple. I mean very simple.

Write a Python Program to Swap Two Variables without using Third Variable:

Here is a simple line of code that works for you.

x, y = y, x

You can check out the complete program for swapping two values without using a temporary variable.


x = 5
y = 10

print "The value of x before swapping:", x
print "The value of y before swapping:", y

# No need of temporary variable
# swap the values

x, y = y, x

print "The value of x after swapping:", x
print "The value of y after swapping:", y

Note: As Python uses dynamic data type. This program will work to swap any types of data values such as string, int, float. In the above program, I am using two numeric data type variables for swapping.

Python program to swap two variables without using third variable is most efficient as it does not use any temporary variable. Always prefer to write efficient code which takes less memory in every project.

If you have any other Python trick to swap two variables without using third variable, write in the comment section below.

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.

Comments

  • Reply
    Rajesh
    June 6, 2018 at 11:52 pm

    Python is really interesting, nice and super.

    • Reply
      Aniruddha Chaudhari
      July 1, 2018 at 11:41 am

      Absolutely!

      Pythos is Gem 🙂

  • Reply
    Chris
    July 1, 2018 at 10:59 am

    The statement ‘x, y = y, x’ does not mean that an intermediate hidden variable is not used.

    Actually, you can swap two variables without extra register by the binary operation ‘xor’:

    x=x^y
    y=x^y
    x=x^y
    • Reply
      Aniruddha Chaudhari
      July 1, 2018 at 11:45 am

      Yeah, we can do that as well.

      There are also some more other techniques to swap two numbers without a third variable.

      We can also use addition-subtraction, multiplication-division operations as well to swap two numbers.

Leave a Reply Cancel reply

© 2019 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard