Swapping two integer numbers is same as what we have seen in swapping two variables in python. For the sake of simplicity, here are few ways that we can directly use to write python program to swap two integer numbers with example.
Getting Started
The task is to swap two integer numbers using python programming. For example,
Let’s say a = 80 and b = 90, then output should be a = 90 and b = 80.
There are multiple ways to achieve above tasks.
- Using a Temporary Variable
- Using Arithmetic Operations
- Using Tuple Packing and Unpacking
- Using XOR bitwise operation
- Using map Function
- Using globals() function
- Using a class with instance variables
- Using setattr() function
- Using a Lambda Function and Dictionary
- Using a generator function
- Using deque Data Structure
- Using a Single Line of Code with Arithmetic Operations
1. Using a Temporary Variable
We can use a third variable which stores value temporarily. Program to swap two integers using third variable is as shown below –
def swap_numbers(a, b): temp = a # store value of a in temp. a = b # store value of b in a. b = temp # store value of temp in b. return a, b # Example usage a = 5 b = 10 print("Before swap: a =", a, "b =", b) a, b = swap_numbers(a, b) print("After swap: a =", a, "b =", b)
Here,
- There are two integer variables a and b which we want to swap or interchange.
- temp is temporary variable which is being used to store value of a temporarily.
Output:
Before swap: a = 5 b = 10 After swap: a = 10 b = 5
2. Using Arithmetic Operations
We are going to use subtract i.e. – and addition i.e. + operations to swap two integer numbers in python programming.
Pseudo code to such tasks –
- Define two variables a and b.
- Then, get sum of a and b. Then, store sum in a.
- After that, subtract b from a. Then, store the difference in b.
- Then, subtract b from a and store in a.
- Now, value of a and b are interchanged value.
Program –
def swap_numbers(a, b): a = a + b # Take sum of a and b and store in a. b = a - b # Subtract b from a and store in b. a = a - b # Subtract b from a and store in a. return a, b # Example usage num1 = 50 num2 = 60 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Output:
Before swap: num1 = 50 num2 = 60 After swap: num1 = 60 num2 = 50
3. Using Tuple Packing and Unpacking
We can also use techniques of tuple packing and unpacking to write python program to swap two integer numbers with examples as shown below –
This is simplest way to interchange two integers in python.
def swap_numbers(a, b): return b, a # Example usage num1 = 50 num2 = 60 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
In above code, we are just interchanging the position and return the values.
Output
Before swap: num1 = 50 num2 = 60 After swap: num1 = 60 num2 = 50
4. Using XOR Bitwise Operation
We can also use XOR bitwise operator i.e. ^ to interchange or swap two integer numbers in python as shown below –
def swap_numbers(a, b): a = a ^ b b = a ^ b a = a ^ b return a, b # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
5. Using map Function
We can also use map function in python to interchange or swap two integer numbers in python as shown below –
num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = map(lambda x: num2 if x == num1 else num1, (num1, num2)) print("After swap: num1 =", num1, "num2 =", num2)
Here,
We use the map function with a lambda function. Then, we compare each number with num1 and replaces it with num2 if it matches, and vice versa.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
6. Using globals() Function
We can also use globals() function to swap two integer numbers in python with examples as shown below
def swap_numbers(a, b): globals()['num1'], globals()['num2'] = b, a # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
7. Using a Class with Instance Variables
If two numbers are defined inside class as member, we can interchange the values as shown below –
class NumberSwapper: def __init__(self, a, b): self.num1 = a self.num2 = b def swap(self): self.num1, self.num2 = self.num2, self.num1 # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) swapper = NumberSwapper(num1, num2) swapper.swap() num1 = swapper.num1 num2 = swapper.num2 print("After swap: num1 =", num1, "num2 =", num2)
Here,
– NumberSwapper is a class with instance variables num1 and num2.
– The swap() method swaps the values of these variables.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
8. Using setattr() Function
We can also use setattr() function to swap two integer numbers in python as shown below –
def swap_numbers(a, b): setattr(swap_numbers, 'num1', b) setattr(swap_numbers, 'num2', a) # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) swap_numbers(num1, num2) num1 = swap_numbers.num1 num2 = swap_numbers.num2 print("After swap: num1 =", num1, "num2 =", num2)
Here,
setattr() function to dynamically set the values of num1 and num2 as attributes of the function itself.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
9. Using a Lambda Function and Dictionary
We can also use lambda function and dictionary to write python program to swap two integer numbers with examples as shown below –
swap_numbers = lambda a, b: {'num1': b, 'num2': a} # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) result = swap_numbers(num1, num2) num1 = result['num1'] num2 = result['num2'] print("After swap: num1 =", num1, "num2 =", num2)
Here, we have used swap_numbers() to return a dictionary with swapped values for num1 and num2. We can get the values by extracting them from the dictionary.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
10. Using a Generator Function
Now, we will use generator function that returns the swapped values of a and b when iterated over.
We can use it as follows –
def swap_numbers(a, b): yield b yield a # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Here, we get swapped values when swap_numbers() function is called. These values are stored in variables num1 and num2.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
11. Using deque Data Structure
We can use deque to swap two integer numbers in python with examples as shown below –
from collections import deque def swap_numbers(a, b): queue = deque([b, a]) return queue.popleft(), queue.popleft() # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Here, we are using deque data structure from collection module. In this deque, we are storing swapped values of a and b. The values are then dequeued and assigned to num1 and num2.
Output:
Before swap: num1 = 5 num2 = 10 After swap: num1 = 10 num2 = 5
12. Using Single Line of Code with Arithmetic Operations
We can use walrus operator to write python program to swap two integer numbers as shown below –
def swap_numbers(a, b): return a + b - (b := a), a # Example usage num1 = 5 num2 = 10 print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Output:
This program uses a single line of code with arithmetic operations and the walrus operator (:=) to swap the values of a and b.
That’s how we can swap two numbers in python with examples. Learn more about python at official documentation