 # Python - What is a modulo?

I simply have not recalled going through a lesson on modulos.
Could someone please give me a hand and tell me what I am meant to be doing?

I am not one for mathematics, so you can tell me that this is easier than it seems.

I actually have no idea how it is done now, but in “the old days”, somewhere around third grade, after we had begun to learn division, but before we had learned fractions, the teacher pointed out that some divisions do not work out quite right. 10 divided by 5 is 2, but what is 13 divided by 5? We were taught that the answer to that is “2 remainder 3” or “2 R 3”. Then came fractions and decimals, and we never once gave a thought to remainder until taking a course in computer programming.

It turns out that the remainder concept is very useful, so it has its own symbol, % and name, “modulo.”

In terms of the above problem, `13 / 5 = 2 remainder 3`, the modulo operator tells us that `13 % 5 = 3`.

Now, in terms of the given question, we don’t know how many people there are, but we do know that they are standing in line during gym class “counting off” (back again to the old days!!), and you are 27th in line. What you hear as the class counts is “one, two, three, four, one, two, three, four” … etc.

Bottom line: modulo magic predicts that you, standing there at number 27, will count “three!” when it is your turn. Moreover, person number 26 will call out “two”, and person number 28 will call out … “zero”??
Yes, the modulo operator obviously returns zero, not 4. So, I guess the students are actually counting off, “one, two, three,zero, one, two, three, zero, …”! If we would like the cycle actually to go 1 - 2 - 3 - 4 instead of 1 - 2 - 3 - 0, it takes a bit of tweaking.

You should definitely play around with it on your terminal. We can use mod 12 to give us “clock addition”. if it’s nine o’clock, and I add 7 hours, what does a 12-hour clock face show? Well, `9 + 7 = 16`, and `16 % 12 = 4`, so the answer is four o’clock.

The long division as it was termed back in the day (the new math of the 50s and 60s) is the equivalent of Euclidean Division.

``````D => Dividend
d => divisor (sometimes called the modulus)
q => quotient
R => Remainder
``````

where q is an integer, and where,

``````D = dq + R
``````

If we for the purposes of this demonstration stipulate that D and d are also integers it will fit in with our typical usage. Eg.

``````12 = 5 * 2 + 2
``````

where d is 5, q computes to 2, and R is 2.

We will typically be working with only positive numbers but in any case,

``````|r| < |d|
``````

In other words, r can never equal d and will fall in the range of, `0..d-1`.

Aside

The computer will always return a remainder whether dividing floats or integers.

``````3.14 % 2 => 1.14
``````

Also, there is more than one school of thought as to the sign on the remainder when one of the operands is negative. This consideration, and the one above will not likely come up in any course material on CC so leave that bridge until you cross it in your advanced studies.