C. Algebra · Algebra: Expressions, Equations and CodingCoding with Variables, Expressions and Functions
C2.1 C2.3 A1About 70 minutes
Learning goals- I can use variables and assignment in Python code, and explain how x = x + 1 in code differs from an equation.
- I can write algebraic expressions in code, including multiplication, division and powers, and predict their values.
- I can write a function with a parameter to represent a relationship, such as cost = 28 + 6h.
- I can use == and inequality symbols in code to test equations and inequalities.
Words to know
Python
A popular text-based programming language. Each line is an instruction the computer follows in order.
assignment
Storing a value in a variable. In Python, x = 5 means “put 5 in the box named x”.
function
A named block of code that takes input, does something, and can return an output: def cost(h): return 28 + 6 * h
parameter
A variable in a function's definition that receives a value when the function is called. In cost(h), h is the parameter.
Boolean
A value that is either True or False. Comparisons like 3 * x + 2 == 11 or x < 4 give Booleans.
trace
Follow code line by line on paper, keeping track of each variable's value.
Remember from Grade 8
In Grade 8 you wrote code on paper with SET, FOR EACH loops, IF ... THEN ... ELSE and lists, and you ran it by hand. In Grade 9 you will write the same kinds of ideas in Python, a real programming language. You can type the code into any Python editor (several free ones run in a web browser; ask a grown-up which one to use), or you can still trace it by hand. Every program in these lessons shows its exact output, so you can check your tracing.
Let's start back at the ski hill
The ski hill from the expressions lesson charges $28 plus $6 per hour. Here it is in Python:
hours = 3
cost = 28 + 6 * hours
print(cost)
Output: 46
hours = 3 stores 3 in a variable called hours.28 + 6 * hours is the expression 28 + 6h. In Python, the multiplication sign is an asterisk, and you must always write it: 6h is written 6 * hours.print(cost) shows the value: 46.
Change the first line to hours = 5 and run it again: the output becomes 58. The expression stays the same; only the value of the variable changes. That's exactly what a variable in algebra does.
| Math | Python | Example | Value |
|---|
| + − | + - | 7 - 10 | -3 |
| × | * | 4 * 2.5 | 10.0 |
| ÷ | / | 9 / 3 | 3.0 |
| x² | x ** 2 | 5 ** 2 | 25 |
| brackets | ( ) | 2 * (3 + 4) | 14 |
Python follows the same order of operations as math (brackets, exponents, multiplication and division, addition and subtraction). Division with / always gives a decimal, so 9 / 3 gives 3.0.In code, = means “store”, not “equals”
In algebra, x = x + 3 is an equation with no solution (subtract x from both sides and you get 0 = 3). In Python, x = x + 3 is perfectly fine. It means: take the value in x, add 3, and store the result back in x.
x = 5
x = x + 3
x = x * 2
print(x)
Output: 16
Think of a variable as a labelled box. x starts at 5, becomes 8, then becomes 16. Only the most recent value is kept.Functions and parameters
A function packages an expression so you can use it again and again. The parameter is the function's variable.
def cost(h):
return 28 + 6 * h
print(cost(1))
print(cost(5))
Output: 34, then 58 on the next line. The indented line belongs to the function.
A function is like a machine. You give it an input; the parameter h takes that value; the function returns the output.Equations and inequalities in code
To test whether two things are equal, Python uses a double equals sign: ==. The result is True or False. Inequalities use <, >, <= (≤) and >= (≥).
This gives you a quick way to verify a solution of an equation, or to check whether a value satisfies an inequality.
x = 3
print(3 * x + 2 == x + 8)
print(3 * x + 2 > 20)
Output: True, then False. When x = 3, both sides equal 11, but 11 is not greater than 20.
Worked exampleExample 1: A function for a polynomial
Write a function for f(x) = 2x² − 3x + 1 and use it to find f(4).
- Write x² as
x ** 2 and put * between every coefficient and variable. def f(x): return 2 * x ** 2 - 3 * x + 1 (a short function can fit on one line).- print(f(4)) calculates 2(16) − 3(4) + 1 = 32 − 12 + 1.
Worked exampleExample 2: Code that makes a decision with an inequality
Use the snow-removal companies (A: 150 + 25n, B: 50n). Write code that tells which company is cheaper for a given number of snowfalls.
- Write one function for each company.
- Compare them with < and >, and print a message.
- With n = 4: A costs 150 + 100 = 250, B costs 200, so A < B is False and A > B is True.
Program below. Output for n = 4: B is cheaper.
def company_a(n):
return 150 + 25 * n
def company_b(n):
return 50 * n
n = 4
if company_a(n) < company_b(n):
print("A is cheaper")
elif company_a(n) > company_b(n):
print("B is cheaper")
else:
print("Same cost")
Output: B is cheaper. Try n = 6 (Same cost) and n = 10 (A is cheaper).
Python must-knows
- Python needs a multiplication sign every time: write
3 * x, never 3x. = stores a value; == asks “are these equal?”.- Indentation (4 spaces) shows which lines belong to a function, an if or a loop.
- Variable names can't have spaces. Use
snow_days, not snow days. - A short function can be written on one line:
def f(x): return 3 * x + 1.
Hands-on activityBe the computer, then run it
You need: sticky notes or small pieces of paper, a pencil. Optional: a computer or Chromebook with a Python editor (ask a grown-up to help find one).
- Each sticky note is a variable “box”. Write the variable's name at the top.
- Trace this program:
a = 4, b = a * 3, a = b - a, print(a, b). When a variable changes, cross out the old value and write the new one. - Predict the output before you look at the answer below.
- If you have a computer, type the program in and run it. Did it match?
- Write your own function for something at home, like the cost of a pizza:
def pizza(t): return 14 + 1.5 * t for t toppings. Test it with 0, 2 and 4 toppings.
Model answer: b = 12; then a = 12 − 4 = 8. Output: 8 12. Pizza: 14.0, 17.0 and 20.0 (Python shows .0 because 1.5 is a decimal).
Just for funWhy do programmers mix up Halloween and Christmas?
Because Oct 31 = Dec 25. (In base 8, called octal, the number 31 means 3 × 8 + 1 = 25. Base systems aren't new: the Babylonians used base 60!)
Your turn
1Predict the output:
a = 7
b = a * 2 - 3
print(b)
2Predict the output:
x = 10
x = x - 4
x = x * 2
print(x)
12. x becomes 6, then 12.
3Predict the output of print(2 + 3 * 4 ** 2).
50. Exponent first: 4² = 16. Then 3 × 16 = 48. Then 2 + 48 = 50.
4Predict the output:
def p(w): return 2 * w + 2 * (w + 5)
print(p(12))
58. 2(12) + 2(17) = 24 + 34 = 58. This is the perimeter of a rectangle whose length is 5 more than its width.
5Predict the output:
x = -2
print(5 - 2 * (x + 4) == 3 * x + 7)
True. LS: 5 − 2(2) = 1. RS: 3(−2) + 7 = 1. (This is Example 3 from the solving lesson.)
6Write a Python function taxi(k) for a fare of $4.50 plus $1.95 per km. What does print(taxi(12)) show?
def taxi(k): return 4.50 + 1.95 * k. It prints 27.9 (that is, $27.90).
7Predict the output of print(7 > 3 * 2).
True. 3 × 2 = 6, and 7 > 6.
More practice: warm-up
1Predict: n = 4 then print(n * n).
2Predict: print((8 + 2) / 5).
2.0. Brackets first: 10 / 5. Division with / gives a decimal.
3Predict: y = 3, y = y + 1, print(y).
4Predict: print(10 - 2 * 3).
True. 5 is less than or equal to 5.
More practice: on your own
1Predict the output:
def area(w): return w * (2 * w + 3)
print(area(4))
44. 4 × (8 + 3) = 44. (The garden from the distributive property lesson.)
2The ski hill changes its prices to $32 plus $5 per hour. Alter the cost(h) function and find cost(4).
def cost(h): return 32 + 5 * h. cost(4) = 32 + 20 = 52.
3Predict the output:
x = 3
y = x ** 2 - 2 * x
x = y + 1
print(x, y)
y = 9 − 6 = 3. Then x = 3 + 1 = 4. Output: 4 3.
4This function should give a rectangle's perimeter, but perimeter(15, 10) gives 40 instead of 50. Find and fix the bug.
def perimeter(l, w): return 2 * l + w
It adds only one width. Fix: return 2 * l + 2 * w (or 2 * (l + w)). Now perimeter(15, 10) = 30 + 20 = 50 ✓.
5Write one line of code that checks whether x = 9 solves 4(x − 3) = 2x + 6 (after x = 9). What does it print?
print(4 * (x - 3) == 2 * x + 6) prints True (both sides are 24).
Challenge
1Mia wants to swap the values of a and b:
a = 5
b = 8
a = b
b = a
print(a, b)
What prints, and why doesn't it swap? Fix it.
It prints 8 8. After a = b, the 5 is gone, so b = a copies 8 back into b. Fix with a temporary box: temp = a, a = b, b = temp, which prints 8 5. (Python also allows a, b = b, a.)
2Write a function break_even(fee_a, rate_a, rate_b) that returns the number of uses where plan A (fee plus rate per use) and plan B (rate per use only) cost the same. Test it with the snow companies.
Solve fee_a + rate_a·n = rate_b·n: n = fee_a ÷ (rate_b − rate_a). Code: def break_even(fee_a, rate_a, rate_b): return fee_a / (rate_b - rate_a). break_even(150, 25, 50) returns 6.0 ✓.
3Predict the output:
def f(x): return 3 * x + 1
print(f(f(2)))
Inside first: f(2) = 7. Then f(7) = 22. Output: 22.
Check your understandingQuiz
1. In Python, what does x = x + 1 do?
2. What does print(3 + 4 * 2) show?
3. In def cost(h): return 28 + 6 * h, what is h?
4. What does print(2 * 5 == 10) show?
5. In Python, what does ** mean?
6. What does print(9 / 3) show in Python?
For parents and teachersBig idea: C2.1 asks students to use coding to demonstrate an understanding of algebraic concepts, including variables, parameters, equations and inequalities. This lesson connects each algebra idea to its Python form, and the practice asks students to read and alter short programs (C2.3).
Common difficulties:
- Writing 3x instead of
3 * x. - Confusing = (store) with == (compare).
- Forgetting that variables are updated in order: tracing line by line on paper fixes most mistakes.
- Surprise at decimals: 9 / 3 gives 3.0 in Python.
How to help: No computer is required; tracing on paper is a real skill. If you do use one, any free Python editor works. Ask your teen to predict the output before running the code; the prediction is where the learning happens. Managing frustration when code doesn't work is part of the Strand A skills.