codingstreets
Search
Close this search box.

Python Program: Swap Two Values in Python

numbers on the dice
Photo by Digital Buggu on Pexels.com

Swap Two Values in Python – In this article, learn how to swap two values efficiently in Python with step-by-step instructions and code examples. Discover different techniques for swapping variables and enhance your Python programming skills today.

Also, let’s see another Python Program.

Table of Contents

Swap Two Values in Python

Without using third variable

Example 1: Swap values without using third variable.

				
					a = 5
b = 4

print("Before swapping of a:",a)
print("Before swapping of b:",b)

#swap values - without third variable
a = a+b #9
b = a-b #5
a = a-b #4

print("After swapping of a:",a)
print("After swapping of b:",b)
				
			

Swap Two Values in Python

Explanation:

We first find the Sum of two variables i.e., a and b. Now, to swap the value of b, we subtracted variable b from the sum. Finally, to swap the value of a, we subtracted a from the swapped value of b.

Swap Two Values in Python

Example 2: Swap values without using third variable.

				
					def swap_numbers(a,b):
    a = a+b #9
    b = a-b #5
    a = a-b #4
    
    return a,b

if __name__ == "__main__":
    a = 5
    b = 4
    
print("Before swapping: a = {}, b = {}".format(a,b))
a,b = swap_numbers(a,b)
print("After swapping: a = {}, b = {}".format(a,b))
				
			

Swap Two Values in Python

Explanation:

We first defined a function, swap_numbers(a,b) and passed two parameters (a,b) that takes numbers as an input.

Later, main() function was created that passed the numbers as argument to the parameters a and b.

After the operation, function returns the swapped value of a,b.

This method uses the fact that the sum of two numbers is equal to the product of the numbers and their difference. In other words, here we have two variables a and b, then a + b = ab – a – b. This means that we can swap the values of a and b by first adding them together, then subtracting b from the sum, and then subtracting a from the result.

Swap Two Values in Python

With using third variable

Example: Swap values with using third variable.

				
					a = 10
b = 8

#swap values - by using third variable
c = a #10
a = b #8
b = c #10

print("After swapping of a:",a)
print("After swapping of b:",b)
				
			

Explanation:

We first defined a reference variable c to store the value of variable a, to remain the same value of a during value exchange.

Later, we simply swapped value of a by putting b equal to a.

Finally, to swap value of b, we put b equal to c (reference variable of a).

Conclusion

Through this article, we’ve explored various techniques for swapping variables, from using a temporary variable. As you continue your programming journey, don’t hesitate to apply and adapt the swapping techniques learned here to real-world projects. With this newfound knowledge, you’re well on your way to becoming a more confident and capable Python programmer.

Recent Articles