In this topic students learn two more arithmetic operators: modulo (%), which
gives the remainder after division, and exponentiation (**), which raises a
number to a power.
Be able to read, comprehend, trace, adapt and create Python code that:
% to find the remainder after division// and % together to split an amount into whole units and what is
left over** to raise a number to a powerint() or
float()This chapter was written for Dodona from new material by the course author, after the rest of the course had been converted, so it has no deck of its own. The author’s Intermediate Python series has a Numbers deck that covers the modulo half of this chapter with different examples; it does not mention exponentiation at all: on Google Slides. A PDF copy is kept on Dodona too.
This chapter extends the Calculations chapter. Both operators appear in that chapter’s operator table as well, so a class that skips this series still meets them; this series is where they are practised. They are here because the exam specification lists them alongside the other arithmetic operators, and the earlier chapter did not cover them.
Modulo is the one that needs the teaching. Students know division and they know
remainders from primary school, but they rarely connect the two, and the %
sign itself suggests percentages. The most useful framing is that // and %
are a pair that answer the two halves of one question: how many whole groups,
and how many left over. Every task in this series that uses % can be read
that way: sweets shared between people, energy into batteries, seconds into
minutes, cupcakes into boxes.
Exponentiation is straightforward once students have seen that ** is the
typed form of a raised power. It is worth pointing out that one star and two
stars mean different things, because 2 * 4 and 2 ** 4 look alike and give
8 and 16.
The series comes before selection, so the classic use of modulo, testing
whether a number is even with if number % 2 == 0, is shown on the theory page
as a calculation only. It is worth returning to once the selection chapters
have introduced if.
a % b is the remainder when a is divided by b. 17 % 5 is 2.a // b is the number of whole times b fits into a. 17 // 5 is 3.number % 2 is 0 for even numbers and 1 for odd ones.a ** b is a to the power of b. 4 ** 2 is 16, 4 ** 3 is 64.4.0 ** 2 is 16.0.% as a percentage. 50 % 10 is 0, not 5. The theory page
says so explicitly; it is worth saying out loud as well.// and %. In the Modify task the whole minutes need // and
the seconds left over need %. Students who swap them get Minutes: 10 and
Seconds: 2 for 130 seconds, which the first test shows at once./ for the remainder. 130 / 60 is 2.1666666666666665. The
source’s Modify task is built around this mistake, and the description names
it.2 * 4 is 8 where 2 ** 4 is 16. The
Investigate task’s third question is where this shows.int() where a float is needed. The carpet task reads a
length that can have a decimal point; int() stops with a ValueError on
2.5.