Skip to content
Family Table Math
Auto

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.

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.

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:

AlgebraPythonNotes
3x+13x + 13 * x + 1always write * for multiplication
x2x^2x ** 2two stars mean “to the power of”
x4\dfrac{x}{4}x / 4division gives a decimal, like 0.5
x=9x = 9? (test)x == 9two equals signs compare
x<5x \lt 5, x≥5x \ge 5x < 5, x >= 5inequalities

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 1,2,3,4,51, 2, 3, 4, 5. It stops before the second number, so 66 is not included.

A while loop keeps repeating as long as a condition is true.

A parameter is a value you set once, at the top of a program, that controls how the rest of it behaves. In y=mx+by = mx + b, the slope mm and the initial value bb are parameters: change them, and you get a different line. Putting parameters at the top makes them easy to find and change.

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.

Example 1: Evaluating an expression many times

Section titled “Example 1: Evaluating an expression many times”

The toothpick pattern from writing algebraic expressions uses 3n+13n + 1 toothpicks in Figure nn. 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 4
2 7
3 10
4 13
5 16

How it works. The loop sets n to 11, then 22, and so on up to 55 (not 66). 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 y=mx+by = mx + b with m=2m = 2 and b=−3b = -3, for xx from −2-2 to 33:

m = 2 # rate of change
b = -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 -5
0 -3
1 -1
2 1
3 3

The text after # is a comment. Python ignores it; it’s a note for people reading the code.

Check one row by hand: when x=−2x = -2, y=2(−2)−3=−7y = 2(-2) - 3 = -7. ✓ Notice that yy goes up by 22 (the rate of change mm) each time xx goes up by 11, and y=−3y = -3 (the initial value bb) when x=0x = 0.

Changing the parameters. Change the first two lines to m = -1 and b = 5, and the same program makes a table for y=−x+5y = -x + 5:

x y
-2 7
-1 6
0 5
1 4
2 3
3 2

(a) Solve 5x−7=385x - 7 = 38 by testing every whole number from −20-20 to 2020.

for x in range(-20, 21):
if 5 * x - 7 == 38:
print("Solution: x =", x)

Output:

Solution: x = 9

Only one value made the condition true. Check by hand: 5(9)−7=45−7=385(9) - 7 = 45 - 7 = 38. ✓

(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 20+5g≤5020 + 5g \le 50:

for g in range(0, 11):
cost = 20 + 5 * g
if cost <= 50:
print(g, "GB costs", cost, "dollars")

Output:

0 GB costs 20 dollars
1 GB costs 25 dollars
2 GB costs 30 dollars
3 GB costs 35 dollars
4 GB costs 40 dollars
5 GB costs 45 dollars
6 GB costs 50 dollars

You can afford up to 6 GB. Algebra agrees: 20+5g≤5020 + 5g \le 50 gives 5g≤305g \le 30, so g≤6g \le 6.

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 = 40
weeks = 0
while savings < 200:
savings = savings + 15
weeks = weeks + 1
print(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 ww weeks you have 40+15w40 + 15w dollars.

  • After 10 weeks: 40+150=19040 + 150 = 190. That’s still less than 200, so the loop runs again.
  • After 11 weeks: 40+165=20540 + 165 = 205. Now savings < 200 is false, so the loop stops.

The print line isn’t indented, so it runs once, after the loop. Prediction:

11 205

Running the program confirms it. This solves the inequality 40+15w≥20040 + 15w \ge 200: the smallest whole number that works is w=11w = 11.

Leaving out the multiplication sign. In algebra 3x3x means 3×x3 \times x, 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 11 to 99, not 11 to 1010. 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 x2x^2. 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.

1. (Warm-up) What does this program print?

x = 4
print(3 * x - 5)
print(x ** 2 + 1)
print(x / 8)
Solution

3(4)−5=73(4) - 5 = 7, then 42+1=174^2 + 1 = 17, then 4÷8=0.54 \div 8 = 0.5 (division in Python gives a decimal):

7
17
0.5

2. (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 2n+12n + 1 for n=1,2,3,4n = 1, 2, 3, 4 (the range stops before 5):

1 3
2 5
3 7
4 9

3. (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 y=−3x+4y = -3x + 4, for xx from 00 to 55. Show the new code and its output.

Solution

Change the parameters to m=−3m = -3 and b=4b = 4, and change the range to range(0, 6) (it stops before 6):

m = -3 # rate of change
b = 4 # initial value
print("x", "y")
for x in range(0, 6):
y = m * x + b
print(x, y)

Output:

x y
0 4
1 1
2 -2
3 -5
4 -8
5 -11

Check: when x=5x = 5, y=−3(5)+4=−11y = -3(5) + 4 = -11. ✓

5. (Core) Write a program that solves 4x+9=−234x + 9 = -23 by testing whole numbers from −20-20 to 2020. 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 = -8

Check: 4(−8)+9=−32+9=−234(-8) + 9 = -32 + 9 = -23. ✓ (By algebra: 4x=−324x = -32, so x=−8x = -8.)

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 3x−2>203x - 2 \gt 20 true. By algebra, 3x>223x \gt 22, so x>7.3‾x \gt 7.\overline{3}. The first whole number that works is 8:

8
9
10

Check x=7x = 7: 3(7)−2=193(7) - 2 = 19, 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 dd km ride is 4+2d4 + 2d dollars:

for d in range(1, 9):
cost = 4 + 2 * d
print(d, "km costs", cost, "dollars")

Output:

1 km costs 6 dollars
2 km costs 8 dollars
3 km costs 10 dollars
4 km costs 12 dollars
5 km costs 14 dollars
6 km costs 16 dollars
7 km costs 18 dollars
8 km costs 20 dollars

(b) The first cost over $15 is $16, for a 6 km ride. By algebra: 4+2d>154 + 2d \gt 15 gives 2d>112d \gt 11, so d>5.5d \gt 5.5, and the smallest whole number is 66.

8. (Challenge) Jordan wants to solve 2x+7=312x + 7 = 31 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 x=12x = 12 (because 2x=242x = 24), but range(1, 10) only tests 11 to 99. 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 = 12

Check: 2(12)+7=312(12) + 7 = 31. ✓ 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.0
1 34 46.5
2 38 48.0
3 42 49.5
4 46 51.0
5 50 52.5

Plan 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 GB

Check by algebra: 30+4g=45+1.5g30 + 4g = 45 + 1.5g gives 2.5g=152.5g = 15, so g=6g = 6. Both plans cost 30+24=5430 + 24 = 54 dollars. ✓