ARITHMETIC OPERATORS IN PYTHON

What is Operator?

Operators are characters which denotes a special symbol to accomplish mathematical operations or logical or comparisons etc.

Operands are the objects used in the expression which is connected with operators to provide valuable results post calculation

Below are the types of operators used in python

  • Arithmetic
  • Assignment
  • Comparison
  • Logical
  • Identity
  • Membership
  • Bitwise

In this article, arithmetic operators in python are going to be discussed.

ARITHMETIC OPERATORS:

BODMAS (brackets, division, multiplication, addition, subtraction) is the rules which is used to calculate the arithmetic operations in an order. Always the operators are used from left to right in the expression for equal precedence of same symbols.

BEDMAS(Brackets, exponential, division, multiplication, addition, subtraction) is similar to BODMAS rules which is used to calculate the arithmetic operations in an order. But there is slight difference in the operators used like exponential and modulus. Always the operators are used from left to right in the expression for equal precedence of same symbols.

Below are the arithmetic operators which are calculated in pythonic way which has two positive integers

#Declare variable
 x = 3
 y = 6
 
#print the operations done by arithmetic operators for each value
print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x+y,\
                          x-y,\
                          x*y,\
                          x/y,\
                          x//y,\
                          x%y,\
                          x**y))

Output:

Addition : 9      
Subtraction : -3      
Product : 18      
Float Division : 0.5      
Floor Division : 0      
Modulo : 3      
Power : 729

Below are the arithmetic operators which are calculated in pythonic way which has one decimal and one negative integer

#Declare variable

 x1 = 0.3
 y1 = -6

#print the operations done by arithmetic operators for each value

print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x1 + y1,\
                          x1 - y1,\
                          x1 * y1,\
                          x1 / y1,\
                          x1 // y1,\
                          x1 % y1,\
                          x1 ** y1))

Output:

Addition : -5.7      
Subtraction : 6.3      
Product : -1.7999999999999998      
Float Division : -0.049999999999999996      
Floor Division : -1.0      
Modulo : -5.7      
Power : 1371.7421124828536

If there is a floating point value or operand in an expression in any one of the position, then the result will always be floating point.

Below are the arithmetic operators which are calculated in pythonic way which has one negative decimal and one negative integer

#Declare variable

 x2 = -0.6
 y2 = -9

#print the operations done by arithmetic operators for each value

print('Addition : {0}\
      \nSubtraction : {1}\
      \nProduct : {2}\
      \nFloat Division : {3}\
      \nFloor Division : {4}\
      \nModulo : {5}\
      \nPower : {6}'.format(x2 + y2,\
                          x2 - y2,\
                          x2 * y2,\
                          x2 / y2,\
                          x2 // y2,\
                          x2 % y2,\
                          x2 ** y2))

Output:

Addition : -9.6      
Subtraction : 8.4      
Product : 5.3999999999999995      
Float Division : 0.06666666666666667      
Floor Division : 0.0      
Modulo : -0.6      
Power : -99.22903012752124

Expression with multiple operators with multiple operands

#Declare variable

x3 = -0.6
y3 = -9
z3 = 3

#print the operations done by arithmetic operators for each value

print(x3 + y3 - z3 * x3 / y3 // z3 % x3 ** y3)

Output:

-9.6

Real World Use Cases:

Code:

#How much total mark is scored by a student xyz in exam
#Addition operator
outof = 400
maths = 80
science = 50
computer = 90
biology = 30

total_marks = maths + science + computer + biology
print('Student xyz scored ',total_marks,'out of ',outof)

Output:

Student xyz scored  250 out of  400

Code:

#Product Use case
#Check the validity of the internet pack by converting it from day to hours, minutes and seconds

internet_validity_days = 30
hours = 24 * internet_validity_days
minutes = 1440 * hours 
seconds = 86400 * minutes

print("There are {0} hours, {1} minutes and {2} seconds validity for internet pack to expire".format(hours, minutes, seconds))

Output:

There are 720 hours, 1036800 minutes and 89579520000 seconds validity for internet pack to expire

Code:

#Floor Division
#Check the number of people who need to share money for a new year party

people = 10
party_charge = 100000
individual_share = party_charge // people
print("Each individual should contribute ${}".format(individual_share))

Output:

Each individual should contribute $10000

Summary:

In real world, there are lot of applications which depend on simple arithmetic operators which acts as a solution in many ways like

  • basic amenities which are purchased daily
  • time or distance taken to reach a destination
  • money withdrawal from atm

Every thing that happens in a day to day real world, are based on numbers and numbers are from mathematics.

Leave a Reply