In this python tutorial, you will learn how to Swap Two Number using arithmetic operators and print function of the python programming language.
How to Swap Two Numbers in Python?
Let’s take a look at the first source code , here the integer values are assigned in the code and the arithmetic operator = carries out the function.
RUN CODE SNIPPET# Python code to swap two numbers x = 45 y = 70 print ("Before swapping: ") print("Value of x : ", x, " and y : ", y) # code to swap 'x' and 'y' x, y = y, x print ("After swapping: ") print("Value of x : ", x, " and y : ", y)
OUTPUT:
Before swapping: Value of x : 45 and y : 70 After swapping: Value of x : 70 and y : 45
- At the start, we declare the variables
x
andy
with the integer values45
and70
. - We display the Statements
"Before Swapping: "
 followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function. - The swap code here uses the equals operator =, which has a built function that swaps the input integers and displays the output, the code goes as
x, y = y, x
- We display the Statements
"After Swapping: "
after the swap code, followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function.
Let’s take a look at the second source code, here the integer values are assigned in the code and the arithmetic operator + and – carries out the function.
RUN CODE SNIPPET# Python code to swap two numbers using + and - operators x = 12.7 y = 4.3 print ("Before swapping: ") print("Value of x : ", x, " and y : ", y) # Swap code x = x + y # x = 17.0, y = 4.3 y = x - y # x = 17.0, y = 12.7 x = x - y # x = 4.3, y = 12.7 print ("After swapping: ") print("Value of x : ", x, " and y : ", y)
OUTPUT:
Before swapping: Value of x : 12.7 and y : 4.3 After swapping: Value of x : 4.3 and y : 12.7
- At the start, we declare the variables
x
andy
with the integer values12.7
and4.3
. - We display the Statements
"Before Swapping: "
 followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function. - The swap code here uses the addition and subtraction operators (+ and -), which has a built function that swaps the input integers and displays the output, the code goes as
x = x + y
,y = x - y
andx = x - y
respectively one below the other. - We display the Statements
"After Swapping: "
after the swap code, followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function.
Let’s take a look at the third source code, here the integer values are assigned in the code and the arithmetic operator * and / carries out the function.
RUN CODE SNIPPET# Python code to swap two numbers using / and * operators x = 12.7 y = 4.3 print ("Before swapping: ") print("Value of x : ", x, " and y : ", y) # Swap code x = x * y # x = 54.61, y = 4.3 y = x / y # x = 54.61, y = 12.7 x = x / y # x = 4.3, y = 12.7 print ("After swapping: ") print("Value of x : ", x, " and y : ", y)
OUTPUT:
Before swapping: Value of x : 12.7 and y : 4.3 After swapping: Value of x : 4.3 and y : 12.7
- At the start, we declare the variables
x
andy
with the integer values12.7
and4.3
. - We display the Statements
"Before Swapping: "
 followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function. - The swap code here uses the multiplication and division operators (* and /), which has a built function that swaps the input integers and displays the output, the code goes as
x = x * y
,y = x / y
andx = x / y
respectively one below the other. - We display the Statements
"After Swapping: "
after the swap code, followed by the statement("Value of x : ", x, " and y : ", y)
which holds the declared variables after the comma. It is displayed using theprint
function.
NOTE:
- The equals operator is the simple built – in method which is easy to use.
- The addition and subtraction method of swapping numbers is used for variables which are assigned numerical values and not strings values or any other.
- The multiplication and division method of swapping number is used for variables which are assigned numerical values other than zero.
- The print statement/string to be displayed in enclosed in double quotes.
- Â The integers are zero, positive or negative whole numbers without a fractional part.