VertexMath · MTH1W · Free worksheet

Coding with Python Review

Try each problem first. You don’t need to finish everything.

Core path ~55 min Full sheet ~1 h 45 min

Recommended: K1–K5, then one A and one T problem

K = Knowledge & Understanding · A = Application · T = Thinking · Boss = Challenge

vertexmath.ca Hints, answers & more worksheets

Show your work on separate paper. Show your work in the dot-grid space.

Core Skills (K)

Variables · Operators · Data Types · Loops · Conditionals

K1 Trace the Variables

Determine what each program prints.

  1. a)
    a = 7\texttt{a\ =\ 7}
    b = a\texttt{b\ =\ a}
    a = 2\texttt{a\ =\ 2}
    print(a, b)\texttt{print(a,\ b)}
  2. b)
    x = 4\texttt{x\ =\ 4}
    x = x + 3\texttt{x\ =\ x\ +\ 3}
    x = 2 * x\texttt{x\ =\ 2\ *\ x}
    print(x)\texttt{print(x)}
  3. c)
    p = 3\texttt{p\ =\ 3}
    q = 8\texttt{q\ =\ 8}
    p = q\texttt{p\ =\ q}
    q = p\texttt{q\ =\ p}
    print(p, q)\texttt{print(p,\ q)}
Stuck? Peek at a hint

Work line by line: the right side is computed first, then stored in the variable on the left, replacing what was there.

K2 Python Operators

Evaluate each pair of expressions in Python.

  1. a)
    23 // 4\texttt{23\ //\ 4} and 23 % 4\texttt{23\ \%\ 4}
  2. b)
    12 / 4\texttt{12\ /\ 4} and 2 ** 5\texttt{2\ **\ 5}
  3. c)
    1 / 2 * 8\texttt{1\ /\ 2\ *\ 8} and 1 / (2 * 8)\texttt{1\ /\ (2\ *\ 8)}
Stuck? Peek at a hint

Python does *\texttt{*}, /\texttt{/}, //\texttt{//} and %\texttt{\%} from left to right, after brackets and **\texttt{**}.

K3 Numbers or Text?

Determine the value and the data type (int\texttt{int}, float\texttt{float} or str\texttt{str}) of each result.

  1. a)
    "12" + "3"\texttt{"12"\ +\ "3"}
  2. b)
    int("12") + 3\texttt{int("12")\ +\ 3}
  3. c)
    The user types 4\texttt{4} when this program runs.
    n = input("Enter a number: ")\texttt{n\ =\ input("Enter\ a\ number:\ ")}
    print(n * 3)\texttt{print(n\ *\ 3)}
Stuck? Peek at a hint

Quotation marks make a str\texttt{str}. Check whether int()\texttt{int()} or float()\texttt{float()} has turned the text into a number before you calculate.

K4 Loops and range

Determine what each program prints.

  1. a)
    for i in range(4):\texttt{for\ i\ in\ range(4):}
        print(i * 2)\texttt{\ \ \ \ print(i\ *\ 2)}
  2. b)
    for i in range(3, 12, 4):\texttt{for\ i\ in\ range(3,\ 12,\ 4):}
        print(i)\texttt{\ \ \ \ print(i)}
  3. c)
    total = 0\texttt{total\ =\ 0}
    for k in range(1, 5):\texttt{for\ k\ in\ range(1,\ 5):}
        total = total + k * k\texttt{\ \ \ \ total\ =\ total\ +\ k\ *\ k}
    print(total)\texttt{print(total)}
Stuck? Peek at a hint

List the values of the loop variable first: range(a, b)\texttt{range(a,\ b)} starts at a\texttt{a} and stops before b\texttt{b}.

K5 Branching

Determine what this program prints for each input.
t = float(input("Temperature: "))\texttt{t\ =\ float(input("Temperature:\ "))}
if t > 25:\texttt{if\ t\ >\ 25:}
    print("hot")\texttt{\ \ \ \ print("hot")}
elif t > 10:\texttt{elif\ t\ >\ 10:}
    print("mild")\texttt{\ \ \ \ print("mild")}
else:\texttt{else:}
    print("cold")\texttt{\ \ \ \ print("cold")}

  1. a)
    The user types 30\texttt{30}.
  2. b)
    The user types 25\texttt{25}.
  3. c)
    The user types 10\texttt{10}.
Stuck? Peek at a hint

Test the conditions from the top. Python runs the first branch whose condition is True\texttt{True} and skips the rest.

Real World (A)

Math Library · While Loops · Branching in Context

A1 Ladder Check

A hardware store's app uses this program to find how high a ladder reaches up a wall.
import math\texttt{import\ math}
ladder = float(input("Ladder: "))\texttt{ladder\ =\ float(input("Ladder:\ "))}
base = float(input("Base: "))\texttt{base\ =\ float(input("Base:\ "))}
h = math.sqrt(ladder**2 - base**2)\texttt{h\ =\ math.sqrt(ladder**2\ -\ base**2)}
print("Reaches", round(h, 2), "m")\texttt{print("Reaches",\ round(h,\ 2),\ "m")}

  1. a)
    A 5 m ladder stands with its base 1.4 m from a wall. Determine the output. How high up the wall does the ladder reach?
  2. b)
    A painter always sets the base one quarter of the ladder's length from the wall. Rewrite the base\texttt{base} line so the program calculates it instead of asking. Then determine the output for a 6 m ladder.
  3. c)
    Someone types a ladder length of 3 and a base of 4. What happens when the program runs, and what does it mean for the ladder?
Stuck? Peek at a hint

The ladder, the wall and the ground form a right triangle, and the ladder is the hypotenuse.

A2 Saving for a Bike

Maya has $50 and saves $35 every week for a $480 bike. Her program counts the weeks.
savings = 50\texttt{savings\ =\ 50}
weeks = 0\texttt{weeks\ =\ 0}
while savings < 480:\texttt{while\ savings\ <\ 480:}
    savings = savings + 35\texttt{\ \ \ \ savings\ =\ savings\ +\ 35}
    weeks = weeks + 1\texttt{\ \ \ \ weeks\ =\ weeks\ +\ 1}
print(weeks, savings)\texttt{print(weeks,\ savings)}

  1. a)
    Determine the output. What does it tell Maya?
  2. b)
    Maya already has $500 when she starts, so the first line becomes savings = 500\texttt{savings\ =\ 500}. Determine the output and what it means.
  3. c)
    A friend calculates 480−5035≈12.29\dfrac{480-50}{35}\approx 12.29 and says Maya needs 12.29 weeks. Who is right, Maya's program or her friend?
Stuck? Peek at a hint

Track savings\texttt{savings} and weeks\texttt{weeks} each time the loop runs, and test the condition before every pass.

A3 Electricity Bill

An Ontario utility charges 10 cents per kWh for the first 600 kWh in a month, and 12 cents per kWh for each kWh above 600. This program prints the bill in dollars.
kwh = float(input("Energy used (kWh): "))\texttt{kwh\ =\ float(input("Energy\ used\ (kWh):\ "))}
if kwh <= 600:\texttt{if\ kwh\ <=\ 600:}
    cost = 0.10 * kwh\texttt{\ \ \ \ cost\ =\ 0.10\ *\ kwh}
else:\texttt{else:}
    cost = 60 + 0.12 * (kwh - 600)\texttt{\ \ \ \ cost\ =\ 60\ +\ 0.12\ *\ (kwh\ -\ 600)}
print(round(cost, 2))\texttt{print(round(cost,\ 2))}

  1. a)
    Determine the output for 450 kWh and for 900 kWh. What do the outputs mean?
  2. b)
    A family's bill was $120. How much energy did they use?
  3. c)
    The meter reader types −50-50 by mistake. Determine the output. Does it make sense for the bill? Write an if\texttt{if} condition that would catch this kind of mistake.
Stuck? Peek at a hint

In (b), $120 is an output of the program, so work backwards from the branch that gives bills over $60.

Think It Through (T)

Error Analysis · Building Code from a Graph

T1 Spot the Flaw

A student wants to add 1+2+3+41+2+3+4:
for n in range(1, 5):\texttt{for\ n\ in\ range(1,\ 5):}
    total = 0\texttt{\ \ \ \ total\ =\ 0}
    total = total + n\texttt{\ \ \ \ total\ =\ total\ +\ n}
print(total)\texttt{print(total)}
"My trace table shows that the total ends at 10, so the program prints 10."

nn 11223344
total\texttt{total} 1133661010
  1. a)
    Determine what the program actually prints.
  2. b)
    Identify the line that causes the problem, explain why, and fix the program.
  3. c)
    To add the whole numbers from 1 to 100, the student starts the fixed loop with for n in range(1, 100):\texttt{for\ n\ in\ range(1,\ 100):} instead. Determine what it prints, and correct the loop.
Stuck? Peek at a hint

Trace the program one pass at a time, writing down total\texttt{total} after every line inside the loop.

T2 Build It from the Graph

The graph shows the cost CC, in dollars, to ship a parcel of mass mm kilograms, for 0<m≤60<m\le 6.

Step graph of shipping cost C in dollars against mass m in kilograms. C = 8 from an open dot at (0, 8) to a closed dot at (1, 8). C = 12 from an open dot at (1, 12) to a closed dot at (3, 12). C = 20 from an open dot at (3, 20) to a closed dot at (6, 20).mC12345674812162024
  1. a)
    Write a program that reads m\texttt{m} and prints C\texttt{C}, using if\texttt{if}, elif\texttt{elif} and else\texttt{else}.
  2. b)
    Determine the cost of a 3 kg parcel and of a 3.2 kg parcel from the graph.
  3. c)
    A student's program uses if m < 1:\texttt{if\ m\ <\ 1:} as its first condition and elif m <= 3:\texttt{elif\ m\ <=\ 3:} after it. Find the mass for which it disagrees with the graph.
Stuck? Peek at a hint

A closed dot means that endpoint belongs to the piece, which matches <=\texttt{<=}; an open dot matches <\texttt{<}.

Boss Level

Optional challenge

Count Without Running

count = 0\texttt{count\ =\ 0}
for n in range(1, 1001):\texttt{for\ n\ in\ range(1,\ 1001):}
    if n % 3 == 0 or n % 5 == 0:\texttt{\ \ \ \ if\ n\ \%\ 3\ ==\ 0\ or\ n\ \%\ 5\ ==\ 0:}
        count = count + 1\texttt{\ \ \ \ \ \ \ \ count\ =\ count\ +\ 1}
print(count)\texttt{print(count)}

  1. a)
    Determine the output without running the program.
  2. b)
    The or\texttt{or} is changed to and\texttt{and}. Determine the new output.
  3. c)
    With or\texttt{or} again, 1001\texttt{1001} is replaced by a whole number N\texttt{N}. Find the smallest N\texttt{N} for which the program prints 100\texttt{100}.
Stuck? Peek at a hint

Count the multiples of 3 and the multiples of 5 separately, then look for the numbers you counted twice.

vertexmath.ca Hints, answers & more worksheets

Free to print, copy, adapt and share for classroom use · CC BY-NC 4.0 · © VertexMath

Answer key

Final answers only. Try first, then check.

Core Skills

K1
  • a) 2 7\texttt{2\ 7}
  • b) 14\texttt{14}
  • c) 8 8\texttt{8\ 8}
K2
  • a) 5\texttt{5} and 3\texttt{3}
  • b) 3.0\texttt{3.0} and 32\texttt{32}
  • c) 4.0\texttt{4.0} and 0.0625\texttt{0.0625}
K3
  • a) "123"\texttt{"123"}, a str\texttt{str}
  • b) 15\texttt{15}, an int\texttt{int}
  • c) 444\texttt{444} (n\texttt{n} is the str\texttt{str} "4"\texttt{"4"})
K4
  • a) 0\texttt{0}, 2\texttt{2}, 4\texttt{4}, 6\texttt{6} (one per line)
  • b) 3\texttt{3}, 7\texttt{7}, 11\texttt{11} (one per line)
  • c) 30\texttt{30}
K5
  • a) hot\texttt{hot}
  • b) mild\texttt{mild}
  • c) cold\texttt{cold}

Real World

A1
  • a) Reaches 4.8 m\texttt{Reaches\ 4.8\ m}: the ladder reaches 4.8 m up the wall.
  • b) base = ladder / 4\texttt{base\ =\ ladder\ /\ 4}; Reaches 5.81 m\texttt{Reaches\ 5.81\ m}
  • c) Python stops with a ValueError\texttt{ValueError}: 32−42=−73^2-4^2=-7, and math.sqrt\texttt{math.sqrt} cannot take the square root of a negative number. A 3 m ladder cannot reach a wall 4 m away.
A2
  • a) 13 505\texttt{13\ 505}: Maya can buy the bike after 13 weeks, with $505 saved ($25 left over).
  • b) 0 500\texttt{0\ 500}: the loop never runs, so she can buy the bike today.
  • c) The program: Maya adds $35 once a week, so after 12 weeks she has $470, which is not enough. She needs 13 whole weeks.
A3
  • a) 45.0\texttt{45.0} and 96.0\texttt{96.0}: bills of $45.00 and $96.00
  • b) 1100 kWh (from 60+0.12(k−600)=12060+0.12(k-600)=120)
  • c) -5.0\texttt{-5.0}: the family would be paid $5, which is impossible because energy used cannot be negative. e.g. if kwh < 0:\texttt{if\ kwh\ <\ 0:} then print a warning instead of a bill.

Think It Through

T1
  • a) 4\texttt{4}
  • b) total = 0\texttt{total\ =\ 0} runs on every pass, so the running total restarts each time. Move total = 0\texttt{total\ =\ 0} above the for\texttt{for} line.
  • c) 4950\texttt{4950} (it stops at 99); use range(1, 101)\texttt{range(1,\ 101)} to get 5050\texttt{5050}
T2
  • a) e.g.
    m = float(input("Mass (kg): "))\texttt{m\ =\ float(input("Mass\ (kg):\ "))}
    if m <= 1:\texttt{if\ m\ <=\ 1:}
        C = 8\texttt{\ \ \ \ C\ =\ 8}
    elif m <= 3:\texttt{elif\ m\ <=\ 3:}
        C = 12\texttt{\ \ \ \ C\ =\ 12}
    else:\texttt{else:}
        C = 20\texttt{\ \ \ \ C\ =\ 20}
    print(C)\texttt{print(C)}
  • b) $12 and $20
  • c) m=1m=1: the graph gives $8, the student's program gives $12

Boss Level

Boss
  • a) 467\texttt{467}
  • b) 66\texttt{66}
  • c) N=216N=216

Free worksheet · vertexmath.ca · Free to print and share for classroom use

Free to print, copy, adapt and share for classroom use · CC BY-NC 4.0 · © VertexMath

vertexmath.ca Hints, answers & more worksheets

Free cheat sheet

Coding with Python Cheat Sheet

The whole unit on one page: every rule, a worked example for each idea, and the traps that cost marks.

Unit Summary

One printable page with every rule.

Build your own

The same page with blanks: fill it in from memory, then check.

Phone card

The essentials on one screen. Save it to your photos.

Most testedWhat does this program print?
total = 0\texttt{total\ =\ 0}
for k in range(1, 6):\texttt{for\ k\ in\ range(1,\ 6):}
    total = total + k\texttt{\ \ \ \ total\ =\ total\ +\ k}
print(total)\texttt{print(total)} (card 7)

1Variables and print

score = score + 5\texttt{score\ =\ score\ +\ 5}

Use=\texttt{=} stores the right side's value on the left; ==\texttt{==} tests if two values are equal.

Tipprint("x")\texttt{print("x")} shows x, not its value.

Trapa = 4\texttt{a\ =\ 4}, b = a\texttt{b\ =\ a}, a = 9\texttt{a\ =\ 9}: b\texttt{b} is still 4\texttt{4}, not 9\texttt{9}.

2Operators

Order: brackets, **\texttt{**}, then * / // %\texttt{*\ /\ //\ \%}, then + -\texttt{+\ -}.

7 / 2\texttt{7\ /\ 2} 3.5\texttt{3.5} (float)
7 // 2\texttt{7\ //\ 2} 3\texttt{3} (quotient)
7 % 2\texttt{7\ \%\ 2} 1\texttt{1} (remainder)
7 ** 2\texttt{7\ **\ 2} 49\texttt{49} (power)

Tipround(x, 2)\texttt{round(x,\ 2)}: 2 decimals. #\texttt{\#} starts a comment.

Trap1/(2k)1/(2k) is 1 / (2 * k)\texttt{1\ /\ (2\ *\ k)}; 1 / 2 * k\texttt{1\ /\ 2\ *\ k} is half of kk.

3Data types and input

age = int(input("Age: "))\texttt{age\ =\ int(input("Age:\ "))}

Useinput()\texttt{input()} gives a str\texttt{str}: convert it before you calculate.

int\texttt{int} whole: 42\texttt{42}
float\texttt{float} decimal: 4.0\texttt{4.0}
str\texttt{str} text: "42"\texttt{"42"}

TrapType 6\texttt{6} into n = input()\texttt{n\ =\ input()}: n * 2\texttt{n\ *\ 2} is "66"\texttt{"66"}, not 12\texttt{12}.

Trapint(4.9)\texttt{int(4.9)} is 4\texttt{4}, not 5\texttt{5}; int("4.9")\texttt{int("4.9")} is a ValueError.

4The math library

import math\texttt{import\ math}

math.sqrt(x)  →  x\texttt{math.sqrt(x)}\;\to\;\sqrt{x}

math.pi  →  3.14159…\texttt{math.pi}\;\to\;3.14159\ldots

ExampleLegs 55 and 1212:
math.sqrt(5**2 + 12**2)\texttt{math.sqrt(5**2\ +\ 12**2)}
gives 13.0\texttt{13.0}.

Trap3 ^ 2\texttt{3\ \textasciicircum{}\ 2} is 1\texttt{1}, not 9\texttt{9}. A power is 3 ** 2\texttt{3\ **\ 2}.

5for\texttt{for} loops

for i in range(...):\texttt{for\ i\ in\ range(...):} runs once per value.

range(5)\texttt{range(5)} 0,1,2,3,40,1,2,3,4
range(2, 6)\texttt{range(2,\ 6)} 2,3,4,52,3,4,5
range(1, 10, 3)\texttt{range(1,\ 10,\ 3)} 1,4,71,4,7

Traprange(1, 8)\texttt{range(1,\ 8)} stops at 7\texttt{7}. For 8\texttt{8}, use range(1, 9)\texttt{range(1,\ 9)}.

6while\texttt{while} loops

UseTests before each pass; stops once False\texttt{False}.

ExamplePrints 81\texttt{81}:
n = 1\texttt{n\ =\ 1}
while n < 50:\texttt{while\ n\ <\ 50:}
    n = n * 3\texttt{\ \ \ \ n\ =\ n\ *\ 3}
print(n)\texttt{print(n)}

TrapSaving $40 a week for $300, the loop counts 8 weeks, not 300÷40=7.5300\div40=7.5.

7Running total

ExamplePrints 15\texttt{15} (1+2+3+4+51+2+3+4+5):
total = 0\texttt{total\ =\ 0}
for k in range(1, 6):\texttt{for\ k\ in\ range(1,\ 6):}
    total = total + k\texttt{\ \ \ \ total\ =\ total\ +\ k}
print(total)\texttt{print(total)}

TipProduct? Start at 1\texttt{1}.

Traptotal = 0\texttt{total\ =\ 0} inside the loop resets it each pass: it prints 5\texttt{5}, not 15\texttt{15}.

8Conditional statements

UseOnly the first true branch runs.

if mark >= 80:\texttt{if\ mark\ >=\ 80:} "A"\texttt{"A"}
elif mark >= 70:\texttt{elif\ mark\ >=\ 70:} "B"\texttt{"B"}
else:\texttt{else:} "C"\texttt{"C"}

TipA nested if\texttt{if} runs only if the outer test is True\texttt{True}.

TrapFor mark = 80\texttt{mark\ =\ 80}, mark > 80\texttt{mark\ >\ 80} is False\texttt{False}. Use >=\texttt{>=} when the endpoint counts.

9Loops with branches

UseIn a loop, n % 4 == 0\texttt{n\ \%\ 4\ ==\ 0} finds multiples of 44.

Example$500 at 4% for 3 years: m = m * 1.04\texttt{m\ =\ m\ *\ 1.04}, 3 times, gives 562.43\texttt{562.43}.

TrapAt 10¢/kWh, −80-80 kWh prints a bill of -8.0\texttt{-8.0}. Check inputs with if\texttt{if} first.

Which tool?

You know how many times to repeat for loop
Repeat until a goal is reached while loop
Pick one action from several if / elif / else

More free MTH1W worksheets

Other courses

All MTH1W resources → · All free resources →

What is VertexMath?

VertexMath is daily math practice for Ontario high school students, with a tutor that asks questions instead of giving answers. These worksheets are free, whether or not you use it.

Visit vertexmath.ca →

Free to print, copy, adapt and share for classroom use · CC BY-NC 4.0 · © VertexMath