Menu Close

Python Program to Swap Two Numbers

Python Program To Swap Two Numbers

In this example, we are going to write a Python program to swap two numbers. swapping of two numbers is a very common question of any programming language like c, c++, c#, etc but here we will see a python program to swap two variables with different methods.

In this guide, we will see the python program to swap two numbers using the third variable, without using a third variable and using a function.

Swapping Two Numbers

swapping of two numbers in python is the process of interchange the value of two variables.
Here we will take various examples to interchange the value of two variables so that you can understand very easily swapping two numbers in python.

Swapping of two numbers in one line


#Python program to swap two numbers in one line 

a = int(input("Enter first number:- "))
b = int(input("Enter second number:- "))
print('\n')
#a = 10
#b = 15

print("Before swap, a is : ", a)
print("Before swap, b is : ", b)
a, b = b, a
print("After swap, a is : ", a)
print("After swap, b is : ", b)

Output


Enter first number:- 12
Enter second number:- 21


Before swap, a is :  12
Before swap, b is :  21
After swap, a is :  21
After swap, b is :  12

In the above example, we have used only one line to exchange the two numbers. Here we have sued the assignment operator for swapping two variables.

Swapping of two numbers with the third variable.


#Python program to swap two numbers in using the third variable

a = int(input("Enter first number:- "))
b = int(input("Enter second number:- "))
print('\n')

print("Before swap a is : ", a)
print("Before swap b is : ", b)

temp = a
a = b
b = temp

print("After swap a is : ", a)
print("After swap b is : ", b)

Output


Enter first number:- 12
Enter second number:- 30


Before swap a is :  12
Before swap b is :  30
After swap a is :  30
After swap b is :  12

In the above example we have used the temp variable to hold the value of a temporarily, and then stored value of b in a and value of temp in b.

Swapping of two numbers using multiplication and division method


#Python program to swap two numbers in using multiplication and division method

a = int(input("Enter first number:- "))
b = int(input("Enter second number:- "))

print("\n")
print("Before swap, a is: ", a)
print("Before swap, b is: ", b)

a = a*b
b = a/b
a = a/b
print("After swap, a is: ", int(a))
print("After swap, b is: ", int(b))

Output


Enter first number:- 50
Enter second number:- 100


Before swap, a is:  50
Before swap, b is:  100
After swap, a is:  100
After swap, b is:  50

Swapping of two numbers using the function

You can swap two numbers using the function. Basically, you can place the above code inside a function and call that function.


#Python program to swap two numbers using the function

x = int(input("Enter first number:- "))
y = int(input("Enter second number:- "))


def swapping(a, b):
    print("Before swap a is : ", a)
    print("Before swap b is : ", b)
    a, b = b, a
    print("After swap a is : ", a)
    print("After swap b is : ", b)
swapping(x, y)

Output


Enter first number:- 30
Enter second number:- 90
Before swap a is :  30
Before swap b is :  90
After swap a is :  90
After swap b is :  30

Conclusion

In this Python example, you have learned how to write a python program to swap two numbers with the third variable and without a third variable.

If you are a beginner python programmer, then the swapping of two numbers in python example very useful for you. This type of program boosts up your programming logic.

I hope this example very helpful to you. if you like this article, please share it with others who want to write this type of Python code.

Other example


For more information:- Click Here

Python Program to Find Smallest Number in a List

Related Posts