Swapping of Two Numbers in Python

In this Python tutorial, you will learn multiple ways to swap two numbers in Python using practical examples and realistic scenarios.

While working on a Python Project, I needed to sort the data, so I used some sorting algorithms that required me to swap the numbers while sorting. I found four different solutions to swap two numbers in Python.

4 Ways to Swap Two Numbers in Python

Let’s understand the scenario with more clarity so you can use these approaches in the correct situation.

value_1 = 39
value_2= 45

These are two random values we took for the examples after swapping the values of both variables, and the output should look like this,

Value 1 is 45
Value2 is 39

Let’s see how we can swap two numbers in Python in multiple ways.

Swapping of Two Numbers in Python using a Temporary Variable

First, we will swap the numbers using the temporary variable, which means we will take support of one extra variable to swap the values in Python.

Let’s understand this with a practical example,

emp_id_1 = 1002
emp_id_2 = 1001

print("Id of Employee 1 before swapping: ", emp_id_1)
print("Id of Employee 2 before swapping: ", emp_id_2)

temp = emp_id_1
emp_id_1 = emp_id_2
emp_id_2 = temp

print("After Swapping:")
print("Id of Employee 1 after swapping: ", emp_id_1)
print("Id of Employee 2 after swapping: ", emp_id_2)
Swapping of Two Numbers in Python using a Temporary Variable

In the above example, we have two employee IDs, but we made a mistake while assigning a value, so we needed to swap two values. So, we initialized a variable named temp and assigned emp_id_1 as its value.

READ:  NameError: name is not defined in Python [with 4+ Examples]

Then, we changed the original value of emp_id_1 to emp_id_2. Finally, we changed the value of emp_id_2 and assigned it a temp value, like this: emp_id_2 = temp.

Swapping of Two Numbers in Python Without Using a Temporary Variable

Let’s see how we can swap two numbers in Python without using a temp variable. Here, we will exchange both values directly.

Let’s see how it is possible to swap two numbers without using a temporary variable.

first_num = input('Enter First Number: ')
second_num = input('Enter Second Number: ')

print("Value of first_num before swapping: ", first_num)
print("Value of second_num before swapping: ", second_num)

first_num, second_num = second_num, first_num

print("After swapping:")
print("Value of first_num after swapping: ", first_num)
print("Value of second_num after swapping: ", second_num)
Swapping of Two Numbers in Python Without Using a Temporary Variable

In the above code, we first take the user input for both values and then exchange them together in one line like this: first_num, second_num = second_num, first_num. So, the first_num will take the values of the second number, and the second number will take the value of the first number.

Python Program to Swap Two Numbers Using Function Using Arithmetic Operators

We can swap the values by performing some calculations using addition, subtraction, multiplication, and floor division. Using all these operators to swap the numbers in Python.

Let’s understand this with a practical example,

Swap Two Numbers in Python Using Addition and Subtraction

First, we will see how we can use the addition and subtraction operator to swap two values in Python.

iphone11 = 1200
iphone12 = 999  
   
print ("Prices Before Swapping: ")  
print ("Prices of iphone12: ", iphone12)   
print ("Prices of iphone11 : ", iphone11)  
     
iphone11 =  iphone11 + iphone12 
iphone12 =  iphone11 - iphone12   
iphone11 = iphone11 - iphone12   
   
print ("\n\nPrices After Swapping: ")  
print ("Prices of iphone12: ", iphone12)   
print ("Prices of iphone11: ", iphone11)
Swap Two Numbers in Python Using Addition and Subtraction

In the above code, we have two iPhone models. By mistake, we set the iPhone11 price to the iPhone12 price, and we needed to exchange the prices of both models.

READ:  How to Initialize Dictionary with Default Values

So, first, we changed the value of iPhone 11 to = iPhone11 + iPhone12, then we changed the value of iPhone 12 to = iPhone11—iPhone12. And finally, assign, the value of the iPhone 11 like this: print (“Prices of iPhone11: “, iPhone11).

Swapping of two numbers in Python using Multiplication and Division

Here, we will use Multiplication and Floor division to swap two values in Python by performing mathematical calculations.

Let’s take one example,

Audi_A4 = 47995
Audi_A5 = 48795  
   
print ("Variables Value Before Swapping: ")  
print ("Value of Audi_A5: ", Audi_A5)   
print ("Value of Audi_A4 : ", Audi_A4)  
     
Audi_A4 = Audi_A4 * Audi_A5
Audi_A5 =  Audi_A4 // Audi_A5  
Audi_A4 = Audi_A4 // Audi_A5   
   
print ("Variables Value After Swapping: ")  
print ("Value of Audi_A5: ", Audi_A5)   
print ("Value of Audi_A4: ", Audi_A4) 
Swapping of two numbers in Python using Multiplication and Division

In the above code, we need to swap the prices of both car models. So first, we multiplied both the prices Audi_A4 = Audi_A4 * Audi_A5, then used floor division to get the A4 model price to A5 and finally got the A4 model value using floor division like this: Audi_A4 = Audi_A4 // Audi_A5

Conclusion

In this Python article, you learned how to swap two numbers in Python in multiple ways, including using a temporary variable, then without using a temp variable, and then using arithmetic operators.

You may like to read: