Custom Search

PYTHON: Arithmetic Operators

+ Addition x + y

Simples!

- Subtraction x - y

Simples!

* Multiplication x * y

Computers always use '*' rather than 'x' for multiplication.

/ Division x / y

Computers always use '/' rather than '÷' for division.

% Modulus x % y

A modulus is the number at which we start over when we are dealing with modular arithmetic. 

For example, when telling the time on a clock we are dealing with a modulus of 12.

Therefore:

print(15%12)

will result in an output of 3 because 15:00h on a 24 hour clock is 3 o'clock in the afternoon.

The output number will always be smaller than the modulus we have set.

So, print(24%12) will result in an output of 0

** Exponentiation x ** y

This is raising a number to a power - for example 32 is 3 raised to the power of 2 - 3 is the 'base number' and 2 is the power it is raised to or the exponent.

Therefore:

print(3**2)

will result in 9 as (3 x 3) = 9

whereas

print(2**3)

will result in 8 as (2 x 2 x 2) = 8

// Floor division x // y

Floor division is a normal division operation except that it only returns the integer value of the division sum - it ignores any decimal or fraction that forms part of the answer.

Therefore:

print(3//2)

will result in 1 as (3 / 2) = 1.5 = 1

and

print(2//3)

will result in 0 as (2 / 3) = 0.67 = 0