Unit 2 · Programming 1 · Lesson 2.3

Expressions, Arithmetic, and MOD

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.

Big Idea 3 — Algorithms & ProgrammingMOD starts today, drilled forever

Before this lesson — read on Runestone

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.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

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.

2

The Hook

17 MOD 5 = 2 Your microwave clock does this math every day. It’s 11:58 and you add 5 minutes — how does it know to roll to 12:03 and not show 11:63? It divides and keeps the remainder.

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.

3

The Idea

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 thisPythonAP PseudocodeExample
add / subtract / multiply+   -   *+   -   *3 * 4 → 12
divide (real)//7 / 2 → 3.5
divide, whole part only//(use MOD + /)7 // 2 → 3
remainder%MOD7 % 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.

Python

total = 137
dollars = total // 100   # 1
cents = total % 100      # 37

AP Pseudocode

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.

Two facts about MOD that answer most exam questions: n %  2 is 0 for even, 1 for odd. And n %  10 gives the last digit of n. Learn those two and you’ve got the common cases cold.
Order of operations. Python follows the math you know: parentheses first, then * / // %, then + -, left to right. When in doubt, add parentheses — they cost nothing and remove all argument.
4

Try It — The MOD Playground

Set the two numbers and watch every result at once. Then try the presets to see what MOD is really for.

a = b =
Presets:
a / b  =  —
a // b  =  —
a % b  (a MOD b)  =  —
5

Vocabulary

expression Programs
A combination of values, variables, and operators that the machine works out to a single value.
evaluate Programs
To compute the single value an expression represents — what the machine does to the right side of an assignment before storing it.
MOD (modulus) Programs
The operator that gives the remainder after division. % in Python, MOD in AP Pseudocode. 17 MOD 5 is 2.
6

Check

Five questions. Trace them on paper first — that’s the exam skill. Pick an answer for instant feedback.

0 of 5 answered
7

Impact Check

The operator that makes clocks, colors, and calendars work

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.

8

Connections

Came from 2.2 — the right side of an assignment was a value. Now it’s a calculation the machine evaluates first.
Next in 2.4 — where do the numbers come from? input() lets the user supply them — with one famous gotcha attached.
Returns in 2.12 — n % 2 == 0 is the even/odd test at the heart of countless if statements. MOD and conditionals are a lifelong pair.
Returns on the exam — DISPLAY(a MOD b) traces are guaranteed. Practice them until the remainder is instant.
← 2.2 Variables and Assignment