Python Program to Swap Two Variables without using Third Variable

Python Program to Swap Two Variables without using Third Variable

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.

6 Comments

  1. 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
  2. Good Evening sir🙂, How are You sir?

    I have one doubt sir ,please clear my doubt sir

    1)>>> a=5
    >>> b=10
    >>> print(‘The value of before swapping:’,a,b); temp=a;a=b;b=temp;print(‘The value of after swapping:’,a,b);
    The value of before swapping: 5 10
    The value of after swapping: 10 5

    here above program i have print output statement at last two line of above program, but my doubt is, if write above program like this as i shown below
    >>> a=5
    >>> b=10
    >>> print(‘The value of before swapping:’,a,b) ”’here its print output of this statement next line i.e, okay but i dont want print output of this statement here , so i need to print this statement at last line of the program code
    The value of before swapping: 5 10
    temp=a
    a=b
    b=temp
    print(‘The value of after swapping:’,a,b) #here its okay this statement print next line only

    The value of after swapping: 10 5
    NOTE:
    My conclusion is that i want to print first printed statement at last line of the program code, So there is any syntax or trick is available for my doubt or my doubt is a wrong statement !!!

    1. Hi Badri,

      I’m doing good. Hope you are doing good as well.

      If you want to print the original values of the two numbers, you have to print them before swapping. So you can not just move the first print statement to the last.

Leave a Reply

Your email address will not be published. Required fields are marked *