The right side of an assignment can do math. One operator here shows up all over the exam and nowhere in your math class — so we give it celebrity treatment.
Covers the arithmetic operators. The MOD section is the one to slow down on. Enrolled in our Runestone course? Open it from there so your progress counts.
Still in Programs. In 2.2 the right side of an assignment was simple. Today it becomes an expression — a calculation the machine works out before storing the result.
That operation — divide and keep what’s left over — is MOD. It has no symbol in the math you already know, which is exactly why it feels alien and exactly why the exam leans on it: it separates students who memorized from students who understand. By the end of today it’ll feel obvious.
An expression is anything the machine can evaluate to a
single value. 3 + 4 is an expression; so is price * quantity. Python
uses the operators you’d expect, with two twists worth pinning down:
| Do this | Python | AP Pseudocode | Example |
|---|---|---|---|
| add / subtract / multiply | + - * | + - * | 3 * 4 → 12 |
| divide (real) | / | / | 7 / 2 → 3.5 |
| divide, whole part only | // | (use MOD + /) | 7 // 2 → 3 |
| remainder | % | MOD | 7 % 2 → 1 |
The two twists: Python’s / always gives a decimal (4 / 2 is
2.0, not 2), and // throws away the fractional part to give
a whole number. The exam writes remainder as MOD; Python writes it %.
Same operation, and it’s the star of the show.
total = 137 dollars = total // 100 # 1 cents = total % 100 # 37
total ← 137 dollars ← total / 100 cents ← total MOD 100
Hover or tap a line to light its twin. MOD pulls the leftover;
the whole-number part is the other half of the same division.
* / // %, then + -, left to right. When in doubt, add parentheses
— they cost nothing and remove all argument.
Set the two numbers and watch every result at once. Then try the presets to see what MOD is really for.
% in Python,
MOD in AP Pseudocode. 17 MOD 5 is 2.Five questions. Trace them on paper first — that’s the exam skill. Pick an answer for instant feedback.
MOD is how computers do anything that wraps around. A 12-hour clock is arithmetic MOD 12. Days of the week are MOD 7. The hue wheel that gives you every color is MOD 360 degrees. When a game character walks off the right edge of the screen and reappears on the left, that’s a MOD. It is one of the quietest, most reused ideas in all of computing.
It also does real security work. When a website stores your password, it often runs it through a hash — and remainder arithmetic like MOD is part of how those functions scramble data into a fixed range. You met hashing’s cousin in Unit 1 (fixed-width representation) and you’ll meet it head-on in Unit 7. A humble remainder turns out to be load-bearing.
input() lets the
user supply them — with one famous gotcha attached.n % 2 == 0 is the even/odd test at the heart of
countless if statements. MOD and conditionals are a lifelong pair.DISPLAY(a MOD b) traces are guaranteed.
Practice them until the remainder is instant.