Topic 3.3: Mathematical Expressions | AP CSP Big Idea 3 | APCSExamPrep.com
Mathematical Expressions
After this lesson, you will be able to:
- Use the arithmetic operators +, -, *, /, and MOD to build expressions
- Explain that an expression evaluates to a single value an algorithm can use
- Apply the order of operations: parentheses, then multiplication, division, and MOD, then addition and subtraction
- Use MOD to test even or odd, extract the last digit, and wrap values
- Tell apart Python float division, integer division, and remainder
- Predict whether an expression produces an integer or a float result
/ and // are favorite trap topics.
Almost every program does math. A game adds up a score, a store applies a tax, a clock wraps from 59 minutes back to 0. All of that is built from a handful of arithmetic operators and one special operator, MOD, that hands you the remainder after a division. This lesson is about writing expressions that evaluate to exactly the value you intend, and knowing whether that value comes out as a whole number or a decimal.
Expressions and the Arithmetic Operators
An expression is any piece of code that the computer evaluates to a single value. The value 5 is an expression, the variable x is an expression, and 3 * x + 1 is an expression too. When a program runs, it replaces the whole expression with the one value it produces, and an algorithm can then store that value, display it, or test it. This is the idea the CED calls out: expressions evaluate to a value, and algorithms are built out of them.
AP Computer Science Principles uses five arithmetic operators. Four are the ones you already know: + for addition, - for subtraction, * for multiplication, and / for division. The fifth is MOD, which gives the remainder after division. In Python the MOD operator is written %, but the AP exam reference sheet writes it as the word MOD. They mean exactly the same thing.
a = 7 b = 2 print(a + b) print(a - b) print(a * b)
a <- 7 b <- 2 DISPLAY(a + b) DISPLAY(a - b) DISPLAY(a * b)
Notice that each operator combines two values and produces one new value. Because an expression is always worth a single value, you can nest expressions inside larger expressions: the result of a + b can itself be multiplied, compared, or stored. That composability is what lets a short line of math do real work inside an algorithm.
On the AP CSP exam, arithmetic appears in pseudocode using +, -, *, /, and MOD. The reference sheet states that a MOD b evaluates to the remainder when a is divided by b, and that the result has the same behavior as Python % for the non-negative values you will see. Read MOD as "remainder" and you will not be surprised.
Order of Operations
When an expression mixes operators, the computer does not simply work left to right. It follows a fixed order of operations, the same precedence you learned in math class:
-
Parentheses first: anything inside
( )is evaluated before it is used. - Then multiplication, division, and MOD (
*,/,%), left to right. - Then addition and subtraction (
+,-), left to right.
print(2 + 3 * 4) print((2 + 3) * 4) print(20 - 6 / 2)
DISPLAY(2 + 3 * 4) DISPLAY((2 + 3) * 4) DISPLAY(20 - 6 / 2)
The first line prints 14 because 3 * 4 is done before the +. The second line prints 20 because the parentheses force 2 + 3 first. The third line prints 17.0 because 6 / 2 runs before the subtraction. If you ever want a different order, add parentheses, they are the one tool that always overrides precedence.
The most common expression bug is a missing pair of parentheses. To average a and b you must write (a + b) / 2. Writing a + b / 2 divides only b by 2 and then adds a, which is a completely different value. When an average or a percentage comes out wrong, check your parentheses first.
print(2 + 3 * 4)
MOD: the Remainder Operator
MOD (written % in Python) gives the amount left over after a division. For example 17 % 5 is 2, because 5 goes into 17 three times with 2 left over. The result of a % b is always between 0 and b - 1 for the non-negative values you will meet on the exam, which is exactly why MOD is so useful for cycling and classifying numbers.
n = 47 print(n % 2) # 1 -> n is odd print(n % 10) # 7 -> the last digit
n <- 47 DISPLAY(n MOD 2) # 1 -> n is odd DISPLAY(n MOD 10) # 7 -> the last digit
Three patterns come up constantly, and each is worth memorizing:
-
Even or odd:
n % 2is0whennis even and1whennis odd. -
Last digit:
n % 10gives the ones digit of a whole number, for example3947 % 10is7. -
Wrapping or cycling:
n % kkeeps a value in the range 0 tok - 1, which is how clocks, calendars, and turn counters loop back around.
n is 8, an even number. What does n % 2 display?n = 8 print(n % 2)
Division Three Ways: / vs // vs %
Division is where Python trips people up, because Python has three different operators and the AP exam cares about the difference between the values they produce.
-
/is float division. It always produces a decimal (a float), so7 / 2is3.5and even6 / 2is3.0, not3. -
//is integer (floor) division. It divides and then drops the fractional part, so7 // 2is3. This is the AP pseudocode idea of a whole-number quotient. -
%is MOD, the remainder that is left over, so7 % 2is1.
# three ways to "divide" in Python print(7 / 2) # float division -> 3.5 print(7 // 2) # integer division -> 3 print(7 % 2) # remainder (MOD) -> 1
# AP pseudocode has two operators DISPLAY(7 / 2) # division -> 3.5 DISPLAY(7 MOD 2) # remainder -> 1
Together, // and % split a division into its two parts: 7 // 2 is how many whole 2s fit (3) and 7 % 2 is what is left over (1). AP pseudocode writes remainder as MOD and a plain / for division, so when you translate pseudocode to Python you must decide whether the problem wants the decimal answer (/) or the whole-number answer (//).
A favorite trap: is the result an integer or a float? 7 / 2 is 3.5, but 7 // 2 is 3. When a distractor differs only by 3 versus 3.5 versus 3.0, the question is testing whether you know which division operator was used. Predict the exact printed form before you look at the choices.
/ is float division. Type exactly what Python prints.print(7 / 2)
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Expression | Code that evaluates to a single value | 3 * x + 1 |
| MOD | The remainder after division; written % in Python |
17 % 5 is 2 |
| Order of operations | Parentheses, then * / %, then + -
|
2 + 3 * 4 is 14 |
| Float division | Python /; always gives a decimal result |
7 / 2 is 3.5 |
| Integer division | Python //; drops the fractional part |
7 // 2 is 3 |
| Even/odd test |
n % 2 is 0 for even, 1 for odd |
8 % 2 is 0 |
The Create Task rubric rewards a program that uses mathematical or logical concepts to process input into output. A well-chosen arithmetic expression, an average, a percentage, a running total, is one of the cleanest ways to show that your code actually computes something rather than just storing values.
A point-earning example
Imagine a quiz app that turns a number of correct answers into a percentage score:
In your written response, name the expression and describe how it transforms the input:
- "The expression
(correct / total) * 100uses division and multiplication to convert a raw count into a percentage." - "The parentheses force the division to happen before the multiplication, so the value is computed in the correct order."
- "The single value this expression produces is stored in
percentand then displayed, so the program processes input into meaningful output."
The trap to avoid
Two topic-specific mistakes cost points here. First, dropping the parentheses: correct / total * 100 happens to work by left-to-right rule, but correct / (total * 100) would not, so always be deliberate. Second, using integer division when you need a decimal: in Python 17 // 20 is 0, which would make every percentage look like zero. Match the operator to the value you actually want. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
* means multiply, and it runs before the additions.a and b, but it prints 8.0 instead of 6.0. Which replacement line fixes it?b / 2 changes nothing; it still computes 4 + 4.0, which is 8.0.a * b / 2 is 16.0. That is not an average at all.(a + b) / 2 is 12 / 2, which is 6.0.b is still divided first, giving 8.0.n. Which of the following expressions evaluate to the last digit of n for every such n?-
I.
n % 10 -
II.
n - (n // 10) * 10 -
III.
n // 10
n // 10, scales it back up, and subtracts, which leaves exactly the last digit. III is n // 10, which drops the last digit instead of keeping it.x equal to 12. Which one does NOT evaluate to 2?weeks to hold the whole number of full weeks in d days, with any leftover days dropped. It currently runs weeks = d / 7, which stores a decimal. Which single change fixes it?% gives the leftover days (30 % 7 is 2), not the number of whole weeks.// is integer division, so 30 // 7 is 4, the number of full weeks with the remainder dropped.* multiplies to 210, which is not a count of weeks at all./ is float division, so 7 / 2 is 3.5. It never drops the remainder.7 // 2 gives. The single slash keeps the decimal part.a and b are set. Print the value of the expression a + b * 2. Remember that multiplication happens before addition. Target output: 14
n is set. Print the remainder when n is divided by 5, using the MOD operator %. Target output: 3
n is a positive integer. Print only its last digit (the ones place) using % 10. Target output: 4
d days (7 days per week), dropping any leftover days. It uses / and prints a decimal. Change it to integer division so it prints a whole number. Target output: 4
a and b are set. Print their average. You will need parentheses so the addition happens before the division. Target output: 6.5
bill and tip_pct (a percent), compute the tip as bill * tip_pct / 100, then print the total bill including the tip. Target output: 60.0
total_seconds is given. Using // and %, compute each of these:- the whole number of hours
- the whole minutes left over
- the leftover seconds
subtotal and tax_rate (a percent) are given. Compute these two values:- the tax, as
subtotal * tax_rate / 100 - the total, as
subtotal + tax
Frequently Asked Questions
3 * x + 1 are all expressions. When the program runs, the whole expression is replaced by the one value it produces, and an algorithm can then store, display, or test that value.17 MOD 5 is 2, because 5 goes into 17 three times with 2 left over. AP pseudocode writes it as the word MOD; in Python the same operation is written with the percent sign, %./ is float division and always gives a decimal, so 7 / 2 is 3.5 and 6 / 2 is 3.0. The double slash // is integer division; it divides and drops the fractional part, so 7 // 2 is 3. Choose based on whether you want a decimal or a whole-number answer.2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. When in doubt, add parentheses to force the order you want.🔗 Continue studying
The Superpack includes an editable Topic 3.3 slide deck with animated expression traces, a MOD and order-of-operations problem bank, a print-ready worksheet on integer versus float division, and a unit quiz. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]