3.03 Math Expressions
Math Expressions Lesson
1. LxD Cycle Process
Empathize: We noticed that students learning basic Python math expressions often get overwhelmed and confused when we wrap basic arithmetic in long, multi-step storylines, creating unnecessary cognitive load before they even grasp the core syntax.
Define:
- POV: CSP students need a streamlined way to practice Python operators and variables without confusing storylines because wrapping basic math in unrelated themes creates unnecessary cognitive load.
- Learning Goal: Students will understand how to assign values to variables and use Python arithmetic operators (
+,-,*,/) with clean, straightforward examples.
Ideate:
- HMW Question: How might we teach Python math expressions cleanly without distracting stories or overly complex themes?
- Activity: A direct, theme-free code walkthrough using
value_aandvalue_b, paired with a straightforward currency conversion homework task.
Prototype & Test: We originally drafted lessons with multi-step real-world themes, but feedback showed students preferred a clean, distraction-free structure. We streamlined the lesson to focus strictly on foundational operators.
2. Lesson Plan
Learning Objective: By the end of this lesson, you will be able to write dynamic math expressions using Python operators so your program automatically calculates outputs based on variable inputs for the Poway Recovery Center platform.
Success Criteria: You can take raw numbers for session capacity and group bookings, assign them to variables, apply the correct arithmetic operators, and print the evaluated result.
Tech Talk (3 minutes) When building the Poway Recovery Center website, you need code that can handle real-time user bookings. This means you donβt need to do the math yourself. Instead of figuring out remaining slots and typing them in, you tell the computer how to calculate it using variables and operators.
The Rule: Use variables and operators. Let Python handle the evaluation.
- β
Do this:
remaining_capacity = total_capacity - (group_size * sessions)followed byprint(remaining_capacity) - β Donβt do this:
print("There are 88 spots left")
Why do we do this? It ensures our platform updates dynamically when users book sessions, makes our code reusable, and powers real-world app features.
LESSON
Part 1 β The Basics: What is a Math Expression?
Why this matters: Every program that calculates a price, a score, a distance, or a time is built on math expressions. A math expression combines numbers and operators so the computer evaluates them into a single new value β without expressions, a program can only store numbers, it can never actually do anything with them.
Python uses the same basic operators as a calculator:
| Operator | Meaning | Description | Usage Example |
|---|---|---|---|
+ |
Addition | Adds two values together. | 10 + 5 evaluates to 15 |
- |
Subtraction | Subtracts the second value from the first. | 10 - 5 evaluates to 5 |
* |
Multiplication | Multiplies two values. | 10 * 5 evaluates to 50 |
/ |
Division | Divides the first value by the second. | 10 / 5 evaluates to 2.0 |
** |
Exponentiation | Raises the first value to the power of the second. | 2 ** 3 evaluates to 8 |
// |
Floor Division | Divides and rounds down to the nearest whole number. | 10 // 3 evaluates to 3 |
% |
Modulus | Divides and returns only the remainder. | 10 % 3 evaluates to 1 |
Setting Up Your Environment
For these two popcorn hacks, as well as the homework, youβll work in your own portfolio repository instead of this shared lesson.
How to Create a New Notebook in Your Portfolio
_notebooks folder, create a new file: YYYY-MM-DD-math-expressions-hw.ipynb.--- layout: post categories: [Python, Mathematical-Expressions] lesson_language: Python lesson_topic: Mathematical-Expressions lesson_part: interactive lesson_type: lesson title: 3.3 Math Expressions HW permalink: /csp/python/math-expressions/hw Authors: YourName ---
Popcorn Hack 1: Try the Basics
Fill in the TODOs below using the operators you just learned, then press Run to check your results.
Code Runner Challenge
Popcorn Hack - Basic Arithmetic Operators
View IPYNB Source
# CODE_RUNNER: Popcorn Hack - Basic Arithmetic Operators
value_a = 100
value_b = 10
value_c = 25
# TODO: calculate total of A, B, and C using the variables above
addition = 0 # Replace with your expression (e.g., value_a + value_b + value_c)
# TODO: calculate the difference between items A and B using the variables above
subtraction = 0 # Replace with your expression
# TODO: calculate the product of items B and C using the variables above
multiplication = 0 # Replace with your expression
# TODO: divide value A by value B and output the division using the variables above
division = 0 # Replace with your expression
print("Total:", addition)
print("Difference:", subtraction)
print("Product:", multiplication)
print("Division:", division)
Going Deeper: Mathematical Expressions
Most expressions you write use infix notation β the operator sits between the numbers, like 5 + 3. Computers donβt have to evaluate math that way.
RPN removes the need for parentheses and order-of-operations rules β every expression can be evaluated left to right using an algorithm. Create a data structure stack (like plates), first-in, first-out.
| Step | Input Queue | Operation | Stack |
|---|---|---|---|
| 1 | 5 3 - 2 * | Push 5 | [5] |
| 2 | 3 - 2 * | Push 3 | [5, 3] |
| 3 | - 2 * | Pop 3, pop 5 β 5 - 3 β push 2 | [2] |
| 4 | 2 * | Push 2 | [2, 2] |
| 5 | * | Pop 2, pop 2 β 2 Γ 2 β push 4 | [4] |
| 6 | (empty) | Result | [4] |
This is important as we often think the human representation of a problem is simple, but computer algorithms are required behind the scenes for everything we do on computers. RPN is still used today in some scientific and financial calculators because itβs fast and never ambiguous.
Popcorn Hack 2: Project Based Math-Expression Hack
The significance of the Popcorn hack is that it teaches you how to write code that actually scales. Instead of hardcoding numbers into the print statement messages, you learn to use variables so features like the Poway Recovery Center booking system can automatically update and calculate data in real time.
In your new notebook, add a code cell and use math expressions to calculate the weekly and monthly cost of attending sessions at a recovery center.
Given:
cost_per_session= 35sessions_per_week= 3weeks_per_month= 4
Calculate and print:
weekly_costβ cost per session Γ sessions per weekmonthly_costβ weekly cost Γ weeks per month
To help aid your understanding, we have provided a COMPLETED popcorn hack in Pseudocode so that it may help you in writing the Python code.
Run your cell to confirm the output before moving on to the homework below.
Code Runner Challenge
Pseudocode Template (COMPLETED EXAMPLE) of Popcorn Hack 2
View IPYNB Source
# CODE_RUNNER: Pseudocode Template (COMPLETED EXAMPLE) of Popcorn Hack 2
total_monthly_capacity β 100
spots_requested_per_group β 4
sessions_booked β 3
// 1. Multiplication: total spots taken by the booking group
total_spots_taken β spots_requested_per_group * sessions_booked
// 2. Subtraction: remaining capacity for the center
remaining_capacity β total_monthly_capacity - total_spots_taken
// 3. Division: distribute remaining spots evenly across 2 rooms
users_per_room β remaining_capacity / 2
DISPLAY("Total Spots Taken: " + total_spots_taken)
DISPLAY("Remaining Capacity: " + remaining_capacity)
DISPLAY("Users per Room: " + users_per_room)
Code Runner Challenge
Python Version of Popcorn Hack 2
View IPYNB Source
# CODE_RUNNER: Python Version of Popcorn Hack 2
# Grouping operations for the Poway Recovery Center booking system
total_monthly_capacity = 100
spots_requested_per_group = 4
sessions_booked = 3
# 1. Multiplication: Calculate total spots taken by the booking group
# PUT YOUR CODE HERE
# 2. Subtraction: Calculate remaining capacity for the center
# PUT YOUR CODE HERE
# 3. Division: Distribute remaining spots evenly across 2 active rooms
# PUT YOUR CODE HERE
# Print the final outputs - EDIT THE CODE BELOW
print("Total Spots Taken: ", )
print("Remaining Capacity ", )
print("Users per Room: ", )
AP CSP Practice Questions: Variables & Mathematical Expressions
1. Consider the following code segment:
Code Runner Challenge
HOMEWORK
View IPYNB Source
a β 4
b β 6
c β 2
a β b + c
b β a - c
c β a + b
What are the final values of a, b, and c after this code segment is executed?
A) a = 4, b = 6, c = 2
B) a = 8, b = 6, c = 14
C) a = 8, b = 4, c = 12
D) a = 8, b = 8, c = 16
2. Consider the following expression:
result β 10 + 17 MOD 5 * 2
According to the standard order of operations used in AP CSP, what value is stored in result?
A) 14
B) 16
C) 10
D) 20
3. In the AP CSP pseudo-code, which of the following expressions evaluates to 3.5?
A) 7 MOD 2 + 2
B) 7 / 2
C) 14 MOD 4 / 2
D) 3 + 1 / 2 * 0
Answer Key & Explanations
1. Answer: B (a = 8, b = 6, c = 14)
a β 4,b β 6,c β 2(Initial values)a β b + c(a becomes 6 + 2 = 8)b β a - c(b becomes 8 - 2 = 6)c β a + b(c becomes 8 + 6 = 14)
2. Answer: A (14)
- According to the AP CSP reference sheet,
MOD,*, and/have the same precedence and are evaluated left to right before addition+. - Step 1:
17 MOD 5evaluates to 2 (the remainder of 17 divided by 5). - Step 2:
2 * 2evaluates to 4. - Step 3:
10 + 4evaluates to 14.
3. Answer: B (7 / 2)
- Unlike AP CSA where
7 / 2would truncate to the integer3, AP CSP evaluates division to an exact decimal. Therefore,7 / 2evaluates exactly to3.5.
π Homework (10 Minutes): Recovery Center Cost Report
In the same portfolio notebook, add new code cell below your interactive task and build a full cost report.
Given:
cost_per_session = 35sessions_per_week = 3weeks_per_month = 4enrollment_fee = 75(one-time cost)
Calculate and print each of the following:
weekly_costβ cost per session Γ sessions per weekmonthly_costβ weekly cost Γ weeks per monthtotal_cost_with_feeβ monthly cost plus the one-time enrollment feeaverage_daily_costβ monthly cost divided by 30
Commit and push your notebook when youβre done β youβll link it in the submission form below.
# CODE_RUNNER: HOMEWORK
# Recovery Center Cost Report
cost_per_session = 35
sessions_per_week = 3
weeks_per_month = 4
enrollment_fee = 75
# 1. Calculate weekly cost
# 2. Calculate monthly cost
# 3. Calculate total cost with fee
# 4. Calculate average daily cost
# Print all results with labels
print("Weekly Cost: ", )
print("Monthly Cost: ", )
print("Total Cost with Fee: ", )
print("Average Daily Cost: ", )
References
AP CSA Unit 1, Topic 1.3 Expressions and Assignment Statements. Quoted from the course and exam description (College Board, 2025, p. 34):
1.3.A.1 βThe arithmetic operators consist of addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).β
1.3.B.1 βAn arithmetic expression consisting only of int operands evaluates to an int result; the portion of the quotient representing the fractional part is truncated.β
1.3.B.2 βAn arithmetic expression involving at least one double operand evaluates to a double result.β
1.3.C.1 βArithmetic expressions can be evaluated using the standard order of operations.β
1.3.D.1 βThe assignment operator (=) allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.β
The College Board also says βthe use of the modulo operator (%) with negative operands is outside the scope of the course and examβ (College Board, 2025, p. 34). This lesson still covers it because it is handy in real code, but it will not be on the exam.
π Grading Plan
weekly_cost and monthly_cost expressiontotal_cost_with_fee and average_daily_cost expressionπ€ Submit Your Homework
Once your notebook is committed and pushed, submit your portfolio link using the form below.
Submit Assignment
Self-Assessment
Rate your work on this assignment, then add a short reflection. All 5 fields are required before you can submit.
Need to update a submission later? Open the submissions dashboard.