# Python Program to Swap Two Numbers

Hey Shouters!! Today we have come up with the python program to swap two numbers.

In this, we will learn all the methods to swap two numbers in python. You will learn to swap two variables by using a temporary variable and, without using a temporary variable.

If you are a beginner and do not know how to write a python program. Please check the Introduction to Python.

## What is swapping ?

Basically, swapping means transferring the value of one variable to another variable. Swapping is a mathematical process and it can be performed in many ways.

Let’s look upon the various methods in python to perform swapping.

## Swapping Numbers: Using a temporary variable-

In this program, we used a third variable named **temp, **and first we will store one variable in **temp **and then will do the swapping.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Python program to swap two variables # To take inputs from the user #x = input('Enter value of x: ') #y = input('Enter value of y: ') x = 5 y = 10 # create a temporary variable and swap the values temp = x x = y y = temp print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) |

**Output**

The value of x after swapping: 10

The value of y after swapping: 5

### Python Program to Swap two numbers- Without Temporary Variable-

In Python, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable.

1 2 3 4 5 6 7 8 9 10 11 |
#Python Program to swap two numbers x = 5 y = 10 x = x + y y = x - y x = x - y print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) |

**Output**

The value of x after swapping: 10

The value of y after swapping: 5

#### 1. Using Multiplication and Division for swapping:

This is the further method in swapping two variables without using a temporary variable.

In this method, we will first multiple two variables and will store in one variable and will divide the changed variable with another to swap the variable.

1 2 3 4 5 6 7 8 9 10 11 |
#python program to swap two variables x=10 y=5 x = x * y y = x / y x = x / y print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) |

**Output**

The value of x after swapping: 10

The value of y after swapping: 5

#### 2. Using XOR Bit-wise operator to Swap two varibales:

In this method, we will be using the XOR operator to swap the two variables. Bit-wise operator does the operation on the bits.

**Truth Table of XOR- **The XOR operator only gives an output when only one input is active/1.

1 2 3 4 5 6 7 8 9 10 |
# python program to swap two variables x=10 y=5 x = x ^ y y = x ^ y x = x ^ y print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y)) |

**Output**

The value of x after swapping: 10

The value of y after swapping: 5

### Python Program to Swap Two Variables: Using Inbuilt Method-

There is a very simple approach in python to swap two variables and you just need to do the following simple step.

1 2 3 4 5 6 7 |
# python program to swap two numbers x = 5 y = 10 x, y = y, x print("x =", x) print("y =", y) |

**Output**

x= 10

y= 5

Comment down the feedback of the program and if you are on Instagram, do follow us on https://www.instagram.com/shoutcoders/

Very happy to see you very well sir, we will benefit a lot from it, you are working very well very very good

Happy to help you.