Working with Numbers
Due to the fast computational speeds of computers, programs are often written to do complex math.
Using the arithmetic operators in Python will automatically follow order of operations.
Addition, Subtraction, Multiplication
Division & Floor Division
The division operation (/
) in Python will enforce a floating point result.
The floor operation (//
) in Python will enforce an integer result. It will also round down to the nearest integer always.
Both division operations will result in an error when trying to divide by zero.
Modulus Operation
The modulus operation, denoted by the %
symbol in Python calculates the remainder of the division between two numbers. It returns the remainder after dividing one number (the dividend) by another (the divisor).
For example, if you perform the modulus operation x % y
, the result will be the remainder when x
is divided by y
.
In this example, x
is divided by y
, which yields a quotient of 3
with a remainder of 1
. Therefore, the value of result
will be 1
.
Modulus is often used in programming for various purposes:
Checking divisibility: By using the modulus operator, you can determine if a number is divisible by another number. If the result is
0
, it means the number is evenly divisible; otherwise, there is a remainder.Cycling and wrapping values: Modulus is frequently used to wrap values within a specific range. For example, you might use
(x % n)
to ensure thatx
stays within the range of0
to(n-1)
.Generating sequences: Modulus can be used to create patterns or sequences. By applying a modulus operation to an incrementing variable, you can create repetitive patterns or cycle through a fixed set of values.
Last updated