0% found this document useful (0 votes)
11 views

Mathematical Operators

mathematical operator notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Mathematical Operators

mathematical operator notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication
and division.
There are 7 arithmetic operators in Python :
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exponentiation
7. Floor division

1. Addition Operator : In Python, + is the addition operator. It is used to add 2 values.


Example :
val1 = 2
val2 = 3

# using the addition operator


res = val1 + val2
print(res)

Output :
5
2. Subtraction Operator : In Python, – is the subtraction operator. It is used to subtract the second value from
the first value.
Example :
val1 = 2
val2 = 3
# using the subtraction operator
res = val1 - val2
print(res)

Output :
-1
3. Multiplication Operator : In Python, * is the multiplication operator. It is used to find the product of 2
values.
Example :
val1 = 2
val2 = 3

# using the multiplication operator


res = val1 * val2
print(res)

Output :
6
4. Division Operator : In Python, / is the division operator. It is used to find the quotient when first operand is
divided by the second.
Example :
val1 = 3
val2 = 2

# using the division operator


res = val1 / val2
print(res)
Output :
1.5
5. Modulus Operator : In Python, % is the modulus operator. It is used to find the remainder when first
operand is divided by the second.
Example :
val1 = 3
val2 = 2

# using the modulus operator


res = val1 % val2
print(res)

Output :
1
6. Exponentiation Operator : In Python, ** is the exponentiation operator. It is used to raise the first operand
to power of second.
Example :
val1 = 2
val2 = 3

# using the exponentiation operator


res = val1 ** val2
print(res)

Output :
8
7. Floor division : In Python, // is used to conduct the floor division. It is used to find the floor of the quotient
when first operand is divided by the second.
Example :
val1 = 3
val2 = 2

# using the floor division


res = val1 // val2
print(res)

Output :
1
Below is the summary of all the 7 operators :

Operator Description Syntax

+ Addition: adds two operands x+y

– Subtraction: subtracts two operands x–y

* Multiplication: multiplies two operands x*y

/ Division (float): divides the first operand by the second x/y

// Division (floor): divides the first operand by the second x // y

Modulus: returns the remainder when first operand is divided by the


% second x%y

** Power : Returns first raised to power second x ** y


% Modulus Divides left hand operand by right hand operand and returns If a = 10 and b = 20, then
remainder
b%a=0

if a = 10 and b = 21, then

b%a=1

** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20

// Floor Division - The division of operands where the result is the 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4,
quotient in which the digits after the decimal point are removed. -11.0//3 = -4.0
But if one of the operands is negative, the result is floored, i.e.,
rounded away from zero (towards negative infinity) –

Floor division - division that results into whole number adjusted to the
left in the number line
Modulo Operator With int
>> 15 % 4
3

>>> 17 % 12
5

>>> 240 % 13
6

>>> 10 % 16
10

Modulo Operator With float


Similar to int, the modulo operator used with a float will return the remainder of division, but as a float value:

>>> 12.5 % 5.5


1.5

>>> 17.0 % 12.0


5.0

Modulo Operator With a Negative Operand


In Python and other languages, the remainder will take the sign of the divisor instead:
8 % -3 = -1

r = a - (n * (a//n))

1. r is the remainder.
2. a is the dividend.
3. n is the divisor.
4. r = 8 - (-3 * floor(8/-3))
5. r = 8 - (-3 * floor(-2.666666666667))
6. r = 8 - (-3 * -3) # Rounded away from 0
7. r = 8 - 9
8. r = -1

Modulo Operator Precedence


Like other Python operators, there are specific rules for the modulo operator that determine its precedence when
evaluating expressions. The modulo operator (%) shares the same level of precedence as the multiplication (*),
division (/), and floor division (//) operators.

>>> 4 * 10 % 12 - 9
-5

1. 4 * 10 is evaluated, resulting in 40 % 12 - 9.
2. 40 % 12 is evaluated, resulting in 4 - 9.
3. 4 - 9 is evaluated, resulting in -5.
a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c = a - b
print "Line 2 - Value of c is ", c

c = a * b
print "Line 3 - Value of c is ", c

c = a / b
print "Line 4 - Value of c is ", c

c = a % b
print "Line 5 - Value of c is ", c

a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c

a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c

When you execute the above program, it produces the following result −
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2

You might also like