Arithmetic operators in Python

In this article, we would cover Arithmetic operators in Python. These help us perform certain mathematical operations – Addition, Division, Exponentiation, Floor Division, Modulus, Multiplication and Subtraction.

Arithmetic operators in Python

We use two variables x and y here, which would be common for all except for few.

x = 100
y = 10

I. Addition (+) – sum of two numbers.

print(x+y)

It would result in –

110

II. Division (/) – to split a larger part into smaller equal parts.

print(x/y)

It would return with –

10.0

III. Exponentiation (**) – raise x to the power the of y i.e. xy

print(x**y)

Output –

100000000000000000000

IV. Floor division (//) – Pretty similar to regular division we do. But, there is a difference. It results in outcome which gets rounded-off to the greatest possible integer. Here, we need different variables a and b.

a = 24
b = 7
print(a//b)

It would result in –

3

But, what if a is equal to 27 –

a = 27
b = 7
print(a//b)

Again, it would return with –

3

Clearly, we can see how its rounds-off the outcome (i.e. 3.42 and 3.85) to the greatest integer value (i.e 3).

V. Modulus (%) – It returns the remainder of division. Again, using different variables a and b.

a = 25
b = 7
print(a%b)

It would result in –

4

VI. Multiplication (*) – product of two numbers.

print(x*y)

It would return with –

1000

VII. Subtraction (-) – difference between two numbers.

print(x-y)

Output –

90

In conclusion, we have discussed arithmetic operators in Python here.

Similar Posts