Swapping of Two Numbers in Python

Swapping two numbers is a fundamental operation in programming, and Python provides several ways to execute the task. Whether you’re a beginner or an experienced Python developer, understanding how to swap two numbers efficiently is essential. In this tutorial, we will explore various methods to swap two numbers in Python, the methods we covered are temp variable, without a temporary variable, arithmetic operations and Bitwise XOR Swap.

What is Swapping?

So precisely what is swapping? Swapping means taking the value from variable a to variable b and vice versa. In other words, Swapping is the exchanging of the values of the two variables.

For a moment, forget about programming. Just assume we have two containers A and B. Now we need to take the content of container A into container B and vice versa. So what we will do. We will take the third container. Call it C. And then we will put the content of A into C. Now we will put container B’s content into A. Finally, the content of container C into B. You see the contents are swapped. Let’s implement the same logic in programming.

Why Swapping Matters

Swapping numbers is essential in many programming methods, such as sorting algorithms, data manipulation, and mathematical operations. It allows you to exchange the values of two variables, which can be handy in various real-world applications.

For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.

The above video explains only one method for Swapping Two Numbers in Python. But you can find source code and explanations of different methods over here.

Python Program to Swap Two Numbers

Method 1: Swapping of Two Numbers in Python using temp variable

Let’s write a program which Swap Two Numbers.

x = int(input("Enter the Value of x:"))
y = int(input("Enter the Value of y:"))

temp = x
x = y
y = temp

print("Values after swapping x=",x,"y=",y)
Output

Now you see, we got the outputs 20 and 10.

Enter the Value of x: 10
Enter the Value of y: 20
Values after swapping x= 20 y= 10

Here’s the link to watch your YouTube video.

Method 2: Swapping of Two Numbers in Python Without a Temporary Variable

x = int(input("Enter the Value of x:"))
y = int(input("Enter the Value of y:"))

x,y = y,x

print("Values after swapping x=",x,"y=",y)

Output

Enter the Value of x: 50
Enter the Value of y: 60
Values after swapping x= 60 y= 50

Method 3: Swapping of Two Numbers in Python Using Arithmetic Operations

x = int(input("Enter the Value of x:"))
y = int(input("Enter the Value of y:"))

x = x+y
y = x-y
x = x-y

print("Values after swapping x=",x,"y=",y)
Output
Enter the Value of x: 5
Enter the Value of y: 6
Values after swapping x= 6 y= 5

Method 4: Swapping of Two Numbers in Python Using Bitwise XOR Swap

x = int(input("Enter the Value of x:"))
y = int(input("Enter the Value of y:"))

x = x ^ y
y = x ^ y
x = x ^ y

print("Values after swapping x=",x,"y=",y)

Output

Enter the Value of x: 50
Enter the Value of y: 60
Values after swapping x= 60 y= 50

Conclusion

Swapping two numbers in Python is a basic skill that every programmer should master. Whether you prefer the traditional approach with a temporary variable, without a temporary variable, and arithmetic operations, the choice is yours. Each method has its advantages and can be tailored to suit your specific needs.

Now that you’ve learned various techniques to swap two numbers in Python, it’s time to put your knowledge into practice and explore how these methods can be applied in different programming scenarios.

FAQs

Why do we need to swap two numbers in Python?

Swapping two numbers is essential for various programming tasks, such as sorting algorithms, data manipulation, and mathematical operations, where you need to exchange the values of variables.

What is the most efficient way to swap two numbers in Python?

The most efficient way is to use tuple unpacking: x, y = y, x. It’s concise and easy to read.

Can I swap numbers without using a temporary variable in Python?

Yes, you can swap numbers without a temporary variable using tuple unpacking or arithmetic operations.

Why is bitwise XOR used for swapping numbers?

Bitwise XOR is a bitwise operation that can be used to swap numbers, but it’s less common than other methods and may be harder to understand.

About The Author