Python Program to Swap Two Numbers

Python Program to Swap Two Numbers | Here, we will discuss all possible methods to write the python program to swap two numbers. We will give two numbers and store them to a and b variables. The python program will swap these numbers using various methods. Swapping of two numbers means to exchange the values of the variables with each other.

Example:-
a=5 and b=8
After swapping a and b, we get :
a=8 and b=5

Python Program to Swap Two Variables using a Temporary Variable

In this program, We will take two numbers while declaring the variables and create a temporary variable. Then, exchange the values of the variables and finally, it will be displayed to the screen.

# Python program to swap two variables using temporary variable

# take inputs
a = input('Enter the value of a: ')
b = input('Enter the value of b: ')

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# create a temporary variable and swap the value
temp = a
a = b
b = temp

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 10
Enter the value of b: 20
Values Before Swapping
a = 10 and b = 20
Values After Swapping
a = 20 and b = 10

In this program, Inputs (a and b) will be provided by the user. Inputs are scanned using the input() function.

a = input('Enter the value of a: ')
b = input('Enter the value of b: ')

Now, create a temporary variable and we are storing the value of a variable in temp so that when the value of a variable is overwritten by b variable we have the backup of the a value. Finally, assign the variable b with the value of the temp variable.

temp = a
a = b
b = temp

Then, the value of the swapping variables are displayed using the print() function.

print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Python Program to Swap Two Numbers without using Temporary Variable

This is the simplest and easiest way to swap two variables in python. In this program, we will swap the value of two variables without using any temporary variable.

# Python program to swap two numbers 
# without using temporary variable

# take inputs
a = input('Enter the value of a: ')
b = input('Enter the value of b: ')

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# swap the value
a, b = b, a

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 5.5
Enter the value of b: 7
Values Before Swapping
a = 5.5 and b = 7
Values After Swapping
a = 7 and b = 5.5

Swapping of Two Numbers in Python

Now, we will discuss different ways to swap two numbers. We can use a combination of operators like (+, -), (*,/), XOR operator, and bitwise operators with arithmetic operators.

Addition and Subtraction

a = a + b
b = a – b
a = a – b

Multiplication and Division

a = a * b
b = a / b
a = a / b

XOR operator

a = a ^ b
b = a ^ b
a = a ^ b

Bitwise operators and Arithmetic operators

a = (a & b) + (a | b)
b = a + (~b) + 1
a = a + (~b) + 1

Now let’s see how we can implement this in a python program.

Swap two numbers in Python using + and – operator

In this program, we will swap two numbers using addition and subtraction operators. This method works for variables that have numeric values.

# Python program to swap two numbers using + and – operator

# take inputs
a = float(input('Enter the value of a: '))
b = float(input('Enter the value of b: '))

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# swapping of the numbers
a = a + b
b = a - b
a = a - b

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 15
Enter the value of b: 22
Values Before Swapping
a = 15.0 and b = 22.0
Values After Swapping
a = 22.0 and b = 15.0

Swap two numbers in Python using * and / operator

In this program, we will swap two numbers using multiplication and division operators. This method works for variables that have numeric values other than 0.

# Python program to swap two numbers using * and / operator

# take inputs
a = float(input('Enter the value of a: '))
b = float(input('Enter the value of b: '))

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# swapping of the numbers
a = a * b
b = a / b
a = a / b

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 13.6
Enter the value of b: 10
Values Before Swapping
a = 13.6 and b = 10.0
Values After Swapping
a = 10.0 and b = 13.6

Swap two numbers in Python using XOR operator (^)

In this program, we will swap two numbers using the XOR operator (^). This method only works for integers and this program works faster than other programs because this method uses bit operation.

# Python program to swap two numbers using XOR operator

# take inputs
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# swapping of the numbers
a = a ^ b
b = a ^ b
a = a ^ b

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 5
Enter the value of b: 10
Values Before Swapping
a = 5 and b = 10
Values After Swapping
a = 10 and b = 5

Using Bitwise Operators and Arithmetic operators

# Python program to swap two numbers using 
# bitwise and arithmetic operators

# take inputs
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))

print('Values Before Swapping')
print('a = ',a, 'and b = ',b)

# swapping of the numbers
a = (a & b) + (a | b)
b = a + (~b) + 1
a = a + (~b) + 1

# display swapping values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:-

Enter the value of a: 2
Enter the value of b: 4
Values Before Swapping
a = 2 and b = 4
Values After Swapping
a = 4 and b = 2

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Also see:- Python program to find area of circle

Leave a Comment

Your email address will not be published. Required fields are marked *