Shahriar Kabir

Software Engineer

MCPD

OCJP

MCSE

Shahriar Kabir

Software Engineer

MCPD

OCJP

MCSE

Blog Post

Python Math Operators with Examples

February 25, 2022 Python
Python Math Operators with Examples

Python Math OperatorsArithmetic operators also known as Math Operators are used to perform mathematical operations like addition, subtraction, multiplication and division.

There are 7 arithmetic operators in Python :

Addition

Subtraction

Multiplication

Division

Modulus

Exponentiation

Floor division

Examples of the 7 Python Math Operators are as follows:

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

Example :

value1 = 10

value2 = 20

# using the addition operator

res = value1 + value2

print(res)

Output : 30

2. Subtraction Operator : In Python, – is the subtraction operator. It is used to subtract the second value from the first value.

Example :

value1 = 10

value2 = 20

# using the subtraction operator

res = value2 – value1

print(res)

Output : 10

3. Multiplication Operator : In Python, * is the multiplication operator. It is used to find the product of 2 values.

Example :

value1 = 10

value2 = 20

# using the multiplication operator

res = value1 * value2

print(res)

Output: 200

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 :

value1 = 10

value2 = 20

# using the division operator

res = value2 / value1

print(res)

Output: 2

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 :

value1 = 10

value2 = 25

# using the modulus operator

res = value2 % value1

print(res)

Output: 5

6. Exponentiation Operator : In Python, ** is the exponentiation operator. It is used to raise the first operand to power of second.

Example :

value1 = 2

value2 = 3

# using the exponentiation operator

res = value1 ** value2

print(res)

Output : 8                                                           [2 x 2 x 2 =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. In simple words floor division // rounds the result down to the nearest whole number

Example :

value1 = 30

value2 = 7

# using the floor division

res = value1 // value2

print(res)

Output: 4            [30/7 = 4.28; result down to the nearest whole number is 4]

That’s all from Python Math Operators with Examples

Write a comment