In this post we are going to describe on Arithmetic Operators in Python.
Before going to Arithmetic Operators let’s know What is a Operator in Python and then we will learn what is Arithmetic Operator.
Operator in Python:
Operators are used to perform operations on Operand.
a + b = 5
Here a and b are operands and the operation is the mathematical operation which is addition of two numbers. Now Let’s discuss about Arithmetic Operators in detail.
Arithmetic Operator in Python:
Arithmetic Operators are used to perform mathematical operations such as addition, subtraction, Multiplication, Division, Modulus and exponential .
Adds two operands.
Example: 2 + 3
Here operator is “+” and operands are 2 & 3.
The result after Addition Operations will be “5”.
It subtracts two operands.
Example: 5 – 3
Here operator is “-” and operands are 5 & 3.
The result after Substraction Opeartions will be “2”.
It multiplies two operands.
Example: 6 * 5
Here operator is “*” and operands are 6 & 5.
The result after Multiplication operation will be “30”.
It divides the first operand by the second.
Example : 6 / 2
Here operator is “/” and operands are 6 & 2.
The result after Division operation will be “3”.
It returns the remainder when 1st operand is divided by the 2nd.
Example : 6 % 2
Here operator is “%” and operands are 6 & 2.
The result after modulus operation will be “0”.
Example : 3 ** 2
3 to the power 2. t means 3 is multiplied 2 times.that is 3 * 3.
Here operator is “**” and operands are 3 & 2.
The result after exponential operation will be “9”.
Now let’s do the coding to perform the above mathematical operations.
#We have taken example of 2 operands l & m to perform arithmetic operations
#l is the variable which assigned the value 4
l = 4
#m is another variable which assigned the value 2
m = 2
#Add two operands
sum = l + m
print(“Sum of l and m is “, sum)
# Subtract the operand(l) from the operand(m)
sub = l – m
print(“subtraction of l and m is “, sum)
# Multiply operands
mult = l * m
print(“Multiplication l and m is “, mult)
# Divide the operand(l) by the operand(m)
div = l / m
print(“Division of l and m is “, div)
# Find modulus of 2 opearands l & m
mod = l % m
print(“modulus/remainder of l and m is “, mod)
# Exponential/power. l to the power m
exp = l**m
print(“l to the power m is “, exp)
In the above program the comment section which is written in # will be helpful for you as i have added the necessary details and it’s a good practice to write comments in programs.
After executing the above program the output will be as below.
I am using IDLE to write and execute the python programs. You can use any preferred editor and execute the program.
I hope this post will be helpful for you.