- Sheikh Aman

# Swapping of two numbers in python. | Proper code.

Updated: Aug 10, 2020

## Topics you will learn now.

*using a temporary variable.**without using a temporary variable.**using bitwise XOR operator.**using addition and subtraction operators.**using division and multiplication operators.**using function.*

## The facility you will get.

Type and Run the code by yourself at the end.

Comment down your problem and get your answer within a short period of time.

## Swapping of two numbers in python using a temporary variable.

```
number1 = input('Enter First Number: ')
number2 = input('Enter Second Number: ')
print("Value of num1 before swapping: ", num1)
print("Value of num2 before swapping: ", num2)
# swapping two numbers using temporary variable
temp = numberb1
number1 = number2
number2 = temp
print("Value of num1 after swapping: ", number1)
print("Value of num2 after swapping: ", number2)
```

### Output

```
Enter First Number: 52
Enter Second Number: 25
Value of num1 before swapping: 52
Value of num2 before swapping: 25
Value of num1 after swapping: 25
Value of num2 after swapping: 52
```

## Swapping of two numbers in python without using a temporary variable.

```
number_1 = input('Enter First Number: ')
number_2 = input('Enter Second Number: ')
print("Value of num1 before swapping: ", number_1)
print("Value of num2 before swapping: ", number_m2)
# swapping two numbers without using temporary variable
number_1, number_2 = number_2, number_1
print("Value of num1 after swapping: ", number_1)
print("Value of num2 after swapping: ", number_2)
```

### Output

```
Enter First Number: 52
Enter Second Number: 25
Value of num1 before swapping: 52
Value of num2 before swapping: 25
Value of num1 after swapping: 25
Value of num2 after swapping: 52
```

## Swapping of two numbers in python using bitwise XOR operator.

```
x = 5 # x = 0101
y = 10 # y = 1010
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swap code
x ^= y # x = 1111, y = 1010
y ^= x # y = 0101, x = 1111
x ^= y # x = 1010, y = 0101
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
```

### Output

```
Before swapping:
Value of x : 5 and y : 10
After swapping:
Value of x : 10 and y : 5
```

## Swapping of two numbers in python using addition and subtraction operators.

```
x = 6
y = 7
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swap code
x = x + y
y = x - y
x = x - y
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
```

### Output

```
Before swapping:
Value of x : 6 and y : 7
After swapping:
Value of x : 7 and y : 6
```

## Swapping of two numbers in python using multiplication and division operators.

```
x = 6
y = 7
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swap code
x = x * y
y = x / y
x = x / y
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
```

### Output

```
Before swapping:
Value of x : 6 and y : 7
After swapping:
Value of x : 6 and y : 7
```

## Swapping of two numbers in python using the function.

```
def swap_numbers(a, b):
temp = a
a = b
b = temp
print("After Swapping two Number: num1 = {0} and num2 = {1}".format(a, b))
num1 = float(input(" Please Enter the First Value : "))
num2 = float(input(" Please Enter the Second Value : "))
print("Before Swapping two Number: num1 = {0} and num2 = {1}".format(num1, num2))
swap_numbers(num1, num2)
```

### Output

```
Please Enter the First Value :15
Please Enter the Second Value :25
Before Swapping two Number: num1 = 15 and num2 = 25
After Swapping two Number: num1 = 25 and num2 = 15
```

## Type and Run the code by your self.

## Conclusion.

Hello Folks,

I have tried my best to give knowledge on this topic. Hope you have liked it. I will always look forward to your comments. Please be expressive in the comment section. I have here completed almost all possibilities of swapping of two numbers in python.

Stay tuned for more interesting posts and follow me on twitter link you will on the side pinned container.

Thanks.

25 views0 comments