Coding with Algebra
Computers are very good at doing the same calculation over and over, fast. That makes code a great tool for algebra: one short program can evaluate an expression for a hundred values, build a table of values, or test numbers until it finds the one that solves an equation. On this page you’ll read, run and change small Python programs. You don’t need any coding experience.
Key ideas
Section titled “Key ideas”Running the programs
Section titled “Running the programs”You can run every program on this page in any free online Python editor (search for “online Python editor”), or in Python on your own computer. Type or paste the code, press Run, and compare what you see with the output shown here. Then change something and run it again. Experimenting is the best way to learn.
Python is fussy about two things: spelling, and the indentation (the spaces at the start of a line). The indented lines under a for, if or while line are the ones that belong to it.
Variables in code
Section titled “Variables in code”In Python, x = 5 means “store the value 5 in a box called x”. It’s an instruction, not an equation. That’s why code can say weeks = weeks + 1: it means “take the value of weeks, add 1, and store the result back in weeks”.
Here’s how algebra looks in Python:
| Algebra | Python | Notes |
|---|---|---|
3 * x + 1 | always write * for multiplication | |
x ** 2 | two stars mean “to the power of” | |
x / 4 | division gives a decimal, like 0.5 | |
| ? (test) | x == 9 | two equals signs compare |
| , | x < 5, x >= 5 | inequalities |
print(...) shows values on the screen. If you print several values separated by commas, Python puts a space between them.
A for loop repeats some lines once for each value in a list. range(1, 6) gives the whole numbers . It stops before the second number, so is not included.
A while loop keeps repeating as long as a condition is true.
Parameters
Section titled “Parameters”A parameter is a value you set once, at the top of a program, that controls how the rest of it behaves. In , the slope and the initial value are parameters: change them, and you get a different line. Putting parameters at the top makes them easy to find and change.
Equations and inequalities in code
Section titled “Equations and inequalities in code”An if line checks a condition. Combined with a loop, it lets you solve by testing values: try every value in a range, and print the ones that make the equation (or inequality) true. This is a computer version of guess-and-check. It’s great for whole-number answers, but it only finds solutions inside the range you test.
Worked examples
Section titled “Worked examples”Example 1: Evaluating an expression many times
Section titled “Example 1: Evaluating an expression many times”The toothpick pattern from writing algebraic expressions uses toothpicks in Figure . This program prints the figure number and the number of toothpicks for Figures 1 to 5:
for n in range(1, 6): toothpicks = 3 * n + 1 print(n, toothpicks)Output:
1 42 73 104 135 16How it works. The loop sets n to , then , and so on up to (not ). Each time, it works out 3 * n + 1 and prints both numbers. To see Figures 1 to 100, you would only change range(1, 6) to range(1, 101).
Example 2: A table of values with parameters
Section titled “Example 2: A table of values with parameters”This program makes a table of values for the linear relation with and , for from to :
m = 2 # rate of changeb = -3 # initial value
print("x", "y")for x in range(-2, 4): y = m * x + b print(x, y)Output:
x y-2 -7-1 -50 -31 -12 13 3The text after # is a comment. Python ignores it; it’s a note for people reading the code.
Check one row by hand: when , . ✓ Notice that goes up by (the rate of change ) each time goes up by , and (the initial value ) when .
Changing the parameters. Change the first two lines to m = -1 and b = 5, and the same program makes a table for :
x y-2 7-1 60 51 42 33 2Example 3: Solving by testing values
Section titled “Example 3: Solving by testing values”(a) Solve by testing every whole number from to .
for x in range(-20, 21): if 5 * x - 7 == 38: print("Solution: x =", x)Output:
Solution: x = 9Only one value made the condition true. Check by hand: . ✓
(b) A phone plan costs $20 per month plus $5 per GB of data. You have a budget of $50. How many whole GB can you afford? That’s the inequality :
for g in range(0, 11): cost = 20 + 5 * g if cost <= 50: print(g, "GB costs", cost, "dollars")Output:
0 GB costs 20 dollars1 GB costs 25 dollars2 GB costs 30 dollars3 GB costs 35 dollars4 GB costs 40 dollars5 GB costs 45 dollars6 GB costs 50 dollarsYou can afford up to 6 GB. Algebra agrees: gives , so .
Example 4: Reading code to predict the output
Section titled “Example 4: Reading code to predict the output”Before you run this program, predict what it prints.
savings = 40weeks = 0while savings < 200: savings = savings + 15 weeks = weeks + 1print(weeks, savings)Solution. Think about the story: you start with $40 and save $15 a week until you have at least $200. The loop keeps going while savings < 200. After weeks you have dollars.
- After 10 weeks: . That’s still less than 200, so the loop runs again.
- After 11 weeks: . Now
savings < 200is false, so the loop stops.
The print line isn’t indented, so it runs once, after the loop. Prediction:
11 205Running the program confirms it. This solves the inequality : the smallest whole number that works is .
Common mistakes
Section titled “Common mistakes”Leaving out the multiplication sign. In algebra means , but in Python 3x is an error. Write 3 * x.
Using one equals sign to compare. x = 9 stores 9 in x. To test whether x equals 9, write x == 9. Writing if x = 9: gives an error.
Forgetting that range stops early. range(1, 10) gives to , not to . If you’re solving by testing values and the answer is outside your range, the program prints nothing at all. Widen the range and try again.
Using the caret for powers. In Python, x ^ 2 does not mean . Use x ** 2.
Messing up indentation. Lines that belong inside a loop or an if must be indented by the same amount. If a print is indented, it runs every time through the loop; if it isn’t, it runs once at the end.
Trusting the code without checking. Always check at least one output by hand. A typo like 3 * n - 1 instead of 3 * n + 1 still runs; it just gives wrong numbers.
Practice
Section titled “Practice”1. (Warm-up) What does this program print?
x = 4print(3 * x - 5)print(x ** 2 + 1)print(x / 8)Solution
, then , then (division in Python gives a decimal):
7170.52. (Warm-up) What does this program print? What algebraic expression is it evaluating?
for n in range(1, 5): print(n, 2 * n + 1)Solution
It evaluates for (the range stops before 5):
1 32 53 74 93. (Warm-up) Write each expression in Python, using the variable n.
- (a) 5 less than twice a number
- (b) the square of a number, plus 3
- (c) three times the sum of a number and 4
Solution
(a) 2 * n - 5
(b) n ** 2 + 3
(c) 3 * (n + 4) (the brackets are needed, just like in algebra)
4. (Core) Change the program in Example 2 so it makes a table of values for , for from to . Show the new code and its output.
Solution
Change the parameters to and , and change the range to range(0, 6) (it stops before 6):
m = -3 # rate of changeb = 4 # initial value
print("x", "y")for x in range(0, 6): y = m * x + b print(x, y)Output:
x y0 41 12 -23 -54 -85 -11Check: when , . ✓
5. (Core) Write a program that solves by testing whole numbers from to . Show its output, and check the answer by hand.
Solution
for x in range(-20, 21): if 4 * x + 9 == -23: print("Solution: x =", x)Output:
Solution: x = -8Check: . ✓ (By algebra: , so .)
6. (Core) Predict the output of this program. Which inequality is it solving?
for x in range(1, 11): if 3 * x - 2 > 20: print(x)Solution
It prints the whole numbers from 1 to 10 that make true. By algebra, , so . The first whole number that works is 8:
8910Check : , which is not greater than 20, so 7 isn’t printed. ✓
7. (Core) A taxi charges a $4 base fare plus $2 per kilometre.
- (a) Write a program that prints the cost, in dollars, of rides from 1 km to 8 km.
- (b) Use your output to find the shortest whole-number distance that costs more than $15.
Solution
(a) The cost of a km ride is dollars:
for d in range(1, 9): cost = 4 + 2 * d print(d, "km costs", cost, "dollars")Output:
1 km costs 6 dollars2 km costs 8 dollars3 km costs 10 dollars4 km costs 12 dollars5 km costs 14 dollars6 km costs 16 dollars7 km costs 18 dollars8 km costs 20 dollars(b) The first cost over $15 is $16, for a 6 km ride. By algebra: gives , so , and the smallest whole number is .
8. (Challenge) Jordan wants to solve by testing values, and writes:
for x in range(1, 10): if 2 * x + 7 == 31: print("Solution: x =", x)When Jordan runs it, nothing is printed at all. Explain why, and fix the code.
Solution
The solution is (because ), but range(1, 10) only tests to . No value in the range makes the condition true, so nothing is printed. Widen the range, for example to range(1, 21):
for x in range(1, 21): if 2 * x + 7 == 31: print("Solution: x =", x)Output:
Solution: x = 12Check: . ✓ This is the big limitation of testing values: the code can only find answers inside the range you give it (and only whole numbers, with range).
9. (Challenge) Plan A costs $30 per month plus $4 per GB. Plan B costs $45 per month plus $1.50 per GB.
- (a) Predict the output of this program, then explain what it tells you.
- (b) Change two lines so the program prints only the number of GB where the plans cost the same.
for g in range(0, 11): plan_a = 30 + 4 * g plan_b = 45 + 1.5 * g if plan_a < plan_b: print(g, plan_a, plan_b)Solution
(a) For each whole number of GB from 0 to 10, it prints the GB and both costs, but only when Plan A is cheaper. Because 1.5 is a decimal, Plan B’s costs print as decimals:
0 30 45.01 34 46.52 38 48.03 42 49.54 46 51.05 50 52.5Plan A is cheaper for 0 to 5 GB. (At 6 GB both cost $54, and after that Plan B is cheaper.)
(b) Change the if condition to test for equal costs, and print a message:
for g in range(0, 11): plan_a = 30 + 4 * g plan_b = 45 + 1.5 * g if plan_a == plan_b: print("Same cost at", g, "GB")Output:
Same cost at 6 GBCheck by algebra: gives , so . Both plans cost dollars. ✓