# How to swap two numbers in python

What you will learn here about python number swapping

- How to swap two numbers using temp variable in python
- How to swap two numbers using arithmetic operator
- How to swap two numbers using bitwise operator

## How to swap two numbers using temp variable

Sample code for swapping two numbers using temp variable in python is given below

number1=10; number2=20; print("numbers before swap"); print(number1); print(number2); temp=number2; number2=number1; number1=temp; print("numbers After swap"); print(number1); print(number2);

**Output** After running code is shown below

## How to swap two numbers using arithmetic operator

Sample code for swapping two numbers using arithmetic operator in python is given below

number1=10; number2=20; print("numbers before swap"); print(number1); print(number2); #Swapping logic using addition and subtraction number1=number1+number2; number2=number1-number2; number1=number1-number2; print("numbers After swap"); print(number1); print(number2);

**Output** After running code is shown below

## How to swap two numbers using bitwise operator

Sample code for swapping two numbers using bitwise operator in python is given below

number1=10; number2=20; print("numbers before swap"); print(number1); print(number2); #Swapping logic using bit wise xor number1=number1 ^ number2; number2=number1 ^ number2; number1=number1 ^ number2; print("numbers After swap"); print(number1); print(number2);

**Output** After running code is shown below