If you’re new to programming, you may have come across the concept of swapping variables. Swapping variables in Python is a quick way to change the values of different variables. Variables are essential for storing information in your code and can be used to store any data. This tip is beneficial if you want to keep track of the values of several variables simultaneously.
This article will cover the examples for Python program to swap two variables in detail. By the end of this article, you’ll have a good understanding of how to swap variables in Python.
Table of Contents
Swapping Two Variables In Python
Swapping two variables is an essential mathematical concept that can be used to solve problems. It is a mathematical operation that allows you to change the value of one variable to see how it affects the value of another. Two variables are said to swap if the value of one variable changes so that it equals the value of the other.
Algorithm
- Declare two variables as integers n1 and n2.
- n=n1; n1=n2; n2=n1;
- Print n1 and n2
Flow Chart

Python Program To Swap Two Variables : Using Third Variable
You will need to use the substitution principle to swap two variables using a third variable. The substitution principle states that if one of the original variables is constant while the other changes, the value of the third variable will also change.
Let us consider the below example.
Here, we want to swap n1 and n2, and we have used n as a reference variable.
n1 = input('Enter First Number: ')
n2 = input('Enter Second Number: ')
n = n1
n1 = n2
n2 = n
print("value after swapped: ", n1)
print("value after swapped: ", n2)
Output

Python Program To Swap Two Variables | Without Using Third Variable
1. Python Built-in Function
You may find a few built-in Python functions helpful, including left, right = right, left, or comma operator. This function can be used for swapping variables.
Let us consider the following example for better understanding:
n1 = 5
n2 = 7
print ("Before swapping: ")
print("Value of x : ", n1, " and y : ", n2)
# code to swap left, right = right, left
n1, n2 = n2, n1
print ("After swapping: ")
print("Value of x : ", n1, " and y : ", n2)
)
Output

2. Python Operators
-
- By using Mathematical Operator
- By XOR Bitwise Operator
- – Addition & Subtraction Method
- – Division & Multiplication Method
1. Using Mathematical Operator to Swap Two Variables
The swapping of variables by mathematical/ arithmetic operators is a well-known concept in a python programming language. It transforms one data set into another without altering the original values. This is done by replacing each variable in the first set with its corresponding value from the second set while keeping all other variables unchanged.
There are a few mathematical operators that can be used to swap variables, including addition (+), subtraction (-), multiplication (x), division (y), and exponentiation (*).
Addition & Subtraction Method
Addition & subtraction method can be used to swap variables. It helps solve equations by manipulating the operations on both sides and resolving any conflicts that may arise.
So let’s look at the example given below.
n1 = 8.8
n2= 9.3
print ("Before swapping: ")
print("Value of n1 : ", n1, " and n2 : ", n2)
# Swap numbers
n1 = n1 + n2 # 8.8 = 8.8 + 9.3
n2 = n1 - n2 # 9.3 = 8.8 - 9.3
n1 = n1 - n2 # 8.8 = 8.8 - 9.3
print ("After swapping: ")
print("Value of n1 : ", n1, " and n2 : ", n2)
Output

Division & Multiplication Method
In Python, the division and multiplication method is used for swapping variables. It works by taking two numeric values as input and first multiplying them, then dividing both values and storing them.
To understand the logic, consider the following code snippet.
n1 = 3.4
n2 = 11.3
print ("Before swapping: ")
print("Value of n1 : ", n1, " and n2 : ", n2)
# Swap code
n1 = n1 * n2 # 3.4= 3.4 * 11.3
n2 = n1 / n2 # 11.3= 3.4/ 11.3
n1 = n1 / n2 #3.4 = 3.4/ 11.3
print ("After swapping: ")
print("Value of n1 : ", n1, " and n2 : ", n2)
Output

2. XOR Bitwise Operator
Bitwise operator is a logical operation that allows you to manipulate the bits of data within operands.It is a built-in function in python that allows you to perform mathematical operations on bits, such as AND and OR.
Following code snippet shows how XOR bitwise operator is use to swap variables
n1 = int(input(" Enter the First number : "))
n2 = int(input(" Enter the Second number : "))
print("Before: n1 = {0} and n2 = {1}".format(n1, n2))
n1 = n1^n2
n2 = n1^n2
n1 = n1^n2
print("After: n1 = {0} and n2 = {1}".format(n1, n2))
Output

How to Swap Three Variables in Python?
Swapping variables is a fundamental mathematical concept that helps to simplify complicated equations. Swapping three variables is a maths problem that asks you to change one of the given numbers by replacing it with another number while retaining the same values the user is inputting. Swapping three variables can lead to some exciting results.
Let us go through the following code snippet to understand the swapping of three variables:
n1 = input("Enter first number")
n2 = input("Enter second number")
n3 = input("Enter third number")
print('The value of n1 is', n1)
print('The value of n2 is', n2)
print('The value of n3 is',n3)
v= n1
n1 = n2
n2 = n3
n3 = v
print('The value of n1 after swapping is', n1)
print('The value of n2 after swapping is',n2)
print('The value of n3 after swapping is',n3)
Output

FAQs
How do you swap two words in a string?
You may replace or swap strings by utilizing the same technique as you would for replacing or swapping variables, taking two lines, s1 and s2, and using replace method().
Also learn about, How to compare strings in Python.
What is the purpose of swap?
Swapping two variables means that the values of the variables are swapped in computer programming. The data is usually stored in memory when this is done.
Conclusion
In the blog, we have provided code examples of how swapping variables can be used to solve mathematical problems in the python programming language. We hope this helped you understand the Python program to swap two variables.Thanks for reading.