In this sample program, you will learn to swap two numbers without using a temporary variable and show the result using the print() function.
To understand this demo program, you should have the basic Python programming knowledge:
Write a Program to Swap Two Numbers Without a Temp
There are two main methods for swapping two numbers without a temp variable in Python:
Using arithmetic: This method uses the addition and subtraction operators to swap the values of the two numbers.
Using bitwise XOR: This method uses the bitwise XOR operator to swap the values of the two numbers.
In the sample below, we are taking inputs from the user and storing them in two different variables. To swap them without a temp variable, we need to add both numbers and store the result in the first one. Next, we’ve to subtract the second variable from the first one and save the result for the second one.
Finally, we’ll use the first variable to subtract from the second and also to store the result of this operation. At this point, both variables have swapped their values. We’ll now print the result.
Sample Code: Swap Two Numbers Without Temp Variable
Here is an example of a Python program to swap two numbers without a temp variable using the arithmetic method:
# This program swaps two numbers int1 = int(input("Enter first number: ")) int2 = int(input("Enter second number: ")) print('Old value of int1 is {0} and int2 is {1}'.format(int1, int2)) int1 = int1 + int2 int2 = int1 - int2 int1 = int1 - int2 # Display the result print('New value of int1 is {0} and int2 is {1}'.format(int1, int2))
The output of the above code is as follows:
Enter first number: 11 Enter second number: 22 Old value of int1 is 11 and int2 is 22 New value of int1 is 22 and int2 is 11
By the way, as we have said in the beginning you can use the Python xor operator for the same purpose, so you can also try it. Moreover, you may even like to explore our list of 40 Python exercises for beginners.
Conclusion
Overall, swapping without a temp variable is a simple and efficient technique that can be used in a variety of Python programs. Here are some additional points for you to think over.
- Performance: The two methods for swapping two numbers without a temp variable in Python have similar performance. However, the bitwise XOR method is slightly faster for integers.
- Memory usage: Both methods for swapping two numbers without a temp variable in Python use the same amount of memory.
- Use cases: Swapping two numbers without a temp variable can be useful in situations where memory usage is a constraint, such as in embedded systems. It can also be useful in situations where performance is a constraint, such as in real-time systems.
Also Check: Python Program to Print Diamond Pattern
Also, do let us know if you seek our help in creating any Python program which you are facing difficulty with. We’ll promptly provide you with the desired solution.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.