13

Sep# Swapping Numbers in Python

## How to Swap Two Numbers in Python?

Swapping two numbers means exchanging their values. You might be familiar with the term, "swapping". You might even have performed swapping of numbers while learning **C**, **C++**, or **Java**. Swapping is used in sorting algorithms, mathematical calculations, memory management, and various programming tasks.

Well, do you know how to swap two numbers in Python? Have you ever tried swapping numbers using various techniques in Python? In this **Python tutorial**, we'll study how to swap two numbers in a Python program.

But, before proceeding refer to these:

## Ways to Swap Two Numbers in Python

### 1. Using a Temporary Variable

In this approach, we store one of the variables' values in a temporary variable. The value of the other variable is then stored in the first variable. At last, the value of the temporary variable is stored in the second variable.

#### Program to Swap Two Numbers in Python Using a Temporary Variable

```
x = 100
y = 507
temp = x
x = y
y = temp
print("Value of x:", x)
print("Value of y:", y)
```

We have two numbers inside the variables x and y. We have used the temporary variable, temp to store the value of x.

#### Output

```
Value of x: 507
Value of y: 100
```

### 2. Using Comma Operator

This is the most simple method. We use the comma "," operator to swap two numbers.

#### Program to Swap Two Numbers in Python Using Comma Operator

```
x = 790
y = 353
x, y = y, x
print("Value of x:", x)
print("Value of y:", y)
```

The variables x and y are swapped using the "," operator.

#### Output

```
Value of x: 353
Value of y: 790
```

### 3. Using Arithmetic Operators

#### 1. Using Addition and Subtraction Operator

Here, we add two numbers and store the sum in one of the two given numbers. The numbers can then be swapped using the sum and the result of subtraction from the sum.

#### Program to Swap Two Numbers in Python Using Addition and Subtraction

```
x = 900
y = 530
# Swapping of two variables using arithmetic operations
x = x + y
y = x - y
x = x - y
print("Value of x:", x)
print("Value of y:", y)
```

The variables x and y are added and stored in the variable x. The value in y is subtracted from the sum in x and stored in y. Again the new values of x and y are subtracted from each other and stored in x.

#### Output

```
Value of x: 530
Value of y: 900
```

#### 2. Using Multiplication and Subtraction Operator

Here, we multiply two numbers and store their product in one of the two given numbers. The division is then performed and swapping takes place.

#### Program to Swap Two Numbers in Python Using Multiplication and Division

```
x = 108
y = 289
x = x * y
y = x / y
x = x / y
print("Value of x: ", x)
print("Value of y: ", y)
```

The variables x and y are multiplied and stored in the variable x. The product value in x is divided by y and stored in y. Again the new values of x and y are divided and stored in x.

#### Output

```
Value of x: 289.0
Value of y: 108.0
```

Read More: Arithmetic Operators in Python |

### 4. Using Bitwise Operators

#### 1. Using Bitwise Addition and Subtraction

#### Program to Swap Two Numbers in Python Using Bitwise Addition and Subtraction

```
x = 50
y = 102
x = (x & y) + (x | y)
y = x + (~y) + 1
x = x + (~y) + 1
print("x after swapping: ", x)
print("y after swapping: ", y)
```

We've used Bitwise AND(&), Bitwise OR(|), and Bitwise NOT(~) operators to perform swapping of x and y.

#### Output

```
x after swapping: 102
y after swapping: 50
```

#### 2. Using Bitwise XOR

We perform the XOR operation three times on the two variables. Then we can swap their values without using a third variable.

#### Program to Swap Two Numbers in Python Using Bitwise XOR

```
x = 109
y = 507
x = x ^ y
y = x ^ y
x = x ^ y
print("Value of x:", x)
print("Value of y:", y)
```

#### Output

```
Value of x: 507
Value of y: 109
```

Read More: Bitwise Operators in Python |

### 5. Using User-Defined Function

We can swap two variables using a **function in Python**. We can pass the two numbers as arguments to the functions and return or print the swapped data.

We can do this in four different ways:

#### 1. With arguments and return value

```
def swap(x, y):
print("Before swapping a: ", x)
print("Before swapping b: ", y)
x, y = y, x
return x, y
a = 87
b = 94
a, b = swap(a, b)
print("After swapping a becomes: ", a)
print("After swapping b becomes: ", b)
```

#### Output

```
Before swapping a: 87
Before swapping b: 94
After swapping a becomes: 94
After swapping b becomes: 87
```

Read More: Top 50 Python Interview Questions and Answers |

#### 2. With arguments and without return value

```
def swap(x, y):
print("Before swapping a: ", x)
print("Before swapping b: ", y)
x, y = y, x
print("After swapping a becomes: ", x)
print("After swapping b becomes: ", y)
a = 98
b = 67
swap(a, b)
```

#### Output

```
Before swapping a: 98
Before swapping b: 67
After swapping a becomes: 67
After swapping b becomes: 98
```

#### 3. Without arguments and with return value

```
def swap():
x = 76
y = 89
print("Before swapping a: ", x)
print("Before swapping b: ", y)
x, y = y, x
return x, y
a, b = swap()
print("After swapping a becomes:", a)
print("After swapping b becomes:", b)
```

#### Output

```
Before swapping a: 76
Before swapping b: 89
After swapping a becomes: 89
After swapping b becomes: 76
```

#### 4. Without arguments and return value

```
def swap():
x = 87
y = 43
print("Before swapping a: ", x)
print("Before swapping b: ", y)
x, y = y, x
print("After swapping a becomes: ", x)
print("After swapping b becomes: ", y)
swap()
```

#### Output

```
Before swapping a: 87
Before swapping b: 43
After swapping a becomes: 43
After swapping b becomes: 87
```

Read More: |

##### Summary

Python has above all methods that make number switching easy. These methods show how Python can perform simple operations with little syntax and simplify a lot of typical programming jobs. I hope that you have understood all the mentioned methods.