Topic 3.3: Mathematical Expressions | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.3 Mathematical Expressions
3.3
Big Idea 3 • Algorithms & Programming

Mathematical Expressions

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Expression Evaluator game 💻 Live Python editor AAP-2.A • AAP-2.B • AAP-2.C

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
📈 Exam weight: Mathematical expressions run through nearly every Big Idea 3 tracing and spot-the-bug item, and they are how your Create Task program processes input into output. Order of operations, MOD results, and the difference between / and // are favorite trap topics.
💡 Think about this first

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.

🎯 What the exam expects

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:

  1. Parentheses first: anything inside ( ) is evaluated before it is used.
  2. Then multiplication, division, and MOD (*, /, %), left to right.
  3. 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 parentheses trap

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.

✍ Mini Exercise 1 • Predict the value
Multiplication happens before addition. What does Python display?
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 % 2 is 0 when n is even and 1 when n is odd.
  • Last digit: n % 10 gives the ones digit of a whole number, for example 3947 % 10 is 7.
  • Wrapping or cycling: n % k keeps a value in the range 0 to k - 1, which is how clocks, calendars, and turn counters loop back around.
✍ Mini Exercise 2 • What does MOD give?
Here 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), so 7 / 2 is 3.5 and even 6 / 2 is 3.0, not 3.
  • // is integer (floor) division. It divides and then drops the fractional part, so 7 // 2 is 3. This is the AP pseudocode idea of a whole-number quotient.
  • % is MOD, the remainder that is left over, so 7 % 2 is 1.
# 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 (//).

🎯 Integer vs float

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.

✍ Mini Exercise 3 • Type the exact output
Remember that / is float division. Type exactly what Python prints.
print(7 / 2)
prints:

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
📋 Create Performance Task • Mathematical expressions in your program

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:

correct <- 17 total <- 20 percent <- (correct / total) * 100 DISPLAY(percent)

In your written response, name the expression and describe how it transforms the input:

  • "The expression (correct / total) * 100 uses 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 percent and 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 →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: which operation runs before the others?
What value does this code segment display?
x = 2 + 3 * 4 - 1 print(x)
Incorrect. 19 comes from doing 2 + 3 first as if there were parentheses. There are none, so multiplication goes first.
Correct. Multiplication first: 3 * 4 is 12. Then left to right: 2 + 12 is 14, and 14 - 1 is 13.
Incorrect. 20 is (2 + 3) * 4 and ignores the minus 1. Without parentheses the multiplication binds only 3 and 4.
Incorrect. 9 adds everything as 2 + 3 + 4. The * means multiply, and it runs before the additions.
Question 2 of 6Spot the bug
Predict first: what does the buggy line actually compute?
This segment is meant to print the average of a and b, but it prints 8.0 instead of 6.0. Which replacement line fixes it?
a = 4 b = 8 print(a + b / 2)
Incorrect. Adding parentheses around only b / 2 changes nothing; it still computes 4 + 4.0, which is 8.0.
Incorrect. a * b / 2 is 16.0. That is not an average at all.
Correct. The average needs both values added before dividing: (a + b) / 2 is 12 / 2, which is 6.0.
Incorrect. Writing 2 as 2.0 does not change the order of operations; b is still divided first, giving 8.0.
Question 3 of 6I, II, III
A positive integer is stored in 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
Incorrect. I is correct, but II also gives the last digit, so I only is too narrow.
Correct. I is the direct remainder. II removes the last digit with 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.
Incorrect. III drops the last digit rather than keeping it, so it cannot be right. II is correct.
Incorrect. III computes everything except the last digit, so it is not a correct expression for the last digit.
Question 4 of 6NOT question
Each expression below is evaluated with x equal to 12. Which one does NOT evaluate to 2?
Incorrect. 12 % 5 is 2, because 5 goes into 12 twice with 2 left over. This does evaluate to 2.
Incorrect. 12 // 5 is 2, since 5 fits into 12 two whole times. This does evaluate to 2.
Incorrect. 12 % 10 is 2, the last digit of 12. This does evaluate to 2.
Correct. 12 % 7 is 5, because 7 goes into 12 once with 5 left over. It is the only one that is not 2.
Question 5 of 6Spot the bug
Predict first: what type of value does the current line produce?
A program needs 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?
d = 30 weeks = d / 7 print(weeks)
Incorrect. % gives the leftover days (30 % 7 is 2), not the number of whole weeks.
Correct. // is integer division, so 30 // 7 is 4, the number of full weeks with the remainder dropped.
Incorrect. Making 7 a float pushes the result further toward a decimal, the opposite of what is needed.
Incorrect. * multiplies to 210, which is not a count of weeks at all.
Question 6 of 6Int vs float
Predict first: will the result be a whole number or a decimal?
What does this code segment display?
a = 7 print(a / 2)
Correct. In Python / is float division, so 7 / 2 is 3.5. It never drops the remainder.
Incorrect. 3 is what 7 // 2 gives. The single slash keeps the decimal part.
Incorrect. The result is 3.5, not 3.0. Float division does not round down to a whole value.
Incorrect. Python does not round; 7 / 2 is exactly 3.5, not 4.
🎮 Lesson Game
Expression Evaluator
Evaluate each arithmetic expression and predict what Python displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what value is displayed?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • evaluate an expression
a and b are set. Print the value of the expression a + b * 2. Remember that multiplication happens before addition. Target output: 14
Level 2 • MOD
Problem 2 of 8 • the remainder
n is set. Print the remainder when n is divided by 5, using the MOD operator %. Target output: 3
Level 3 • Last digit
Problem 3 of 8 • ones place with MOD
n is a positive integer. Print only its last digit (the ones place) using % 10. Target output: 4
Level 4 • Fix the bug
Problem 4 of 8 • integer division
This should print the whole number of full weeks in 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
Level 5 • Order of operations
Problem 5 of 8 • compute an average
a and b are set. Print their average. You will need parentheses so the addition happens before the division. Target output: 6.5
Level 6 • Create Task style
Problem 6 of 8 • a bill with a tip
A meaningful calculation like one you might use in your Create Task. Given 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
Level 7 • Challenge
Problem 7 of 8 • seconds to h / m / s
Open ended. Write the entire program yourself. total_seconds is given. Using // and %, compute each of these:
  • the whole number of hours
  • the whole minutes left over
  • the leftover seconds
Print hours, then minutes, then seconds, each on its own line. Target output (three lines): 1 then 2 then 5
Level 8 • Challenge
Problem 8 of 8 • a bill with tax
Open ended. Write the entire program yourself. subtotal and tax_rate (a percent) are given. Compute these two values:
  • the tax, as subtotal * tax_rate / 100
  • the total, as subtotal + tax
Print the subtotal, then the tax, then the total, each on its own line. Target output (three lines): 40 then 3.2 then 43.2

Frequently Asked Questions

An expression is any code that evaluates to a single value. A number, a variable, and a combination like 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.
MOD gives the remainder after division. For example 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, %.
The single slash / 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.
Parentheses are evaluated first, then multiplication, division, and MOD from left to right, and finally addition and subtraction from left to right. So 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. When in doubt, add parentheses to force the order you want.
They let your program process input into output, such as an average, a percentage, or a running total, which is exactly what the rubric rewards. In your written response you name the expression and describe how it transforms the input, and you make sure the operators and parentheses produce the value you intend.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]