Skip to content
Study Hall
← Unit 4: Coding with Python

MTH1W · Unit 4 test review

Coding with Python: Test Review

Everything for the Unit 4 test on one page: the key facts from all 3 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.

Key facts

Variables, Expressions and Output in Python

  • An assignment name = expression works out the right side using the current values, then stores the result in the variable on the left. The left side must be a variable name, not a calculation.
  • = stores a value; == compares two values and gives True or False.
  • Operators: +, -, * (multiply), / (divide, decimal answer), // (divide and round down), % (remainder), ** (power).
  • Order of operations: brackets, then **, then multiplication and division from left to right, then addition and subtraction from left to right. Python has no implied multiplication.
  • A variable keeps its value until a new assignment changes it. Changing one variable does not recalculate variables that were worked out from it earlier.
  • A parameter is a value that stays fixed while the program runs but can be edited to model a new situation, like and in .

Loops: Patterns and Tables of Values

  • for x in range(a, b): runs the indented body once for each integer x from a up to, but not including, b.
  • range(n) gives 0, 1, …, n − 1 (that is n values, for a positive whole number n). range(a, b, step) counts by step; a negative step counts down.
  • A table of values from a loop has three steps: choose the x-values with range, calculate y with the rule, print both.
  • An accumulator starts at 0 before the loop, has each new amount added inside the loop, and is printed after the loop.
  • When x goes up by 1 on each pass, a constant change in y means the relation is linear, and that change is the coefficient of x (the in ).

Conditionals, Inequalities and Altering Code

  • Comparison operators: <, >, <= (), >= (), == (), != (). Each gives True or False.
  • In an if, elif, else chain Python runs the block under the first True condition, or the else block if none is True, so exactly one block runs.
  • A loop with an if inside tests an inequality for every value in the range; the printed values are the solutions among the values tested, not all solutions.
  • > and < leave out the boundary value; >= and <= include it. "At least 50" is >= 50.
  • To alter a program for a new situation, change its parameters (fees, rates), its range, or its conditions, then predict and check the new output.

Where marks get lost

  • Writing 4h or 2(x + 1) as you would in algebra. Python needs the multiplication sign: type a * between the 4 and the h, and between the 2 and the bracket. (Variables, Expressions and Output in Python)
  • Writing the assignment backwards, as in 15 + 4 * h = cost. The variable being stored must be on the left: cost = 15 + 4 * h. (Variables, Expressions and Output in Python)
  • Using = when you mean to compare. To check whether x equals 5, write x == 5, which gives True or False. (Variables, Expressions and Output in Python)
  • Expecting a variable to update by itself. If cost was calculated from h, changing h later leaves cost unchanged until you run the calculation line again. (Variables, Expressions and Output in Python)
  • Typing ^ for a power. In Python, 2 ^ 3 is a different operation and prints 1, not 8; powers use a double star, so write 2 ** 3. (Variables, Expressions and Output in Python)
  • Expecting range(1, 10) to include 10. It stops at 9; to reach 10, use range(1, 11). (Loops: Patterns and Tables of Values)
  • Using range(5) when you want 1 to 5. It gives 0, 1, 2, 3, 4; write range(1, 6). (Loops: Patterns and Tables of Values)
  • Putting total = 0 inside the loop. It resets the total on every pass, so a print after the loop shows only the last amount added. (Loops: Patterns and Tables of Values)
  • Indenting the wrong lines: a print that should run once ends up inside the loop and prints on every pass, or a calculation that should repeat is left outside the loop and runs only once. (Loops: Patterns and Tables of Values)
  • Forgetting the colon at the end of the for line, or the 4-space indent on the body. Python stops with an error before running anything. (Loops: Patterns and Tables of Values)
  • Typing => or =<. Python's operators are >= and <=, with the equals sign second; the other order is a SyntaxError. (Conditionals, Inequalities and Altering Code)
  • Using one equals sign in a condition, as in if x = 5:. That is a SyntaxError; comparing uses ==. (Conditionals, Inequalities and Altering Code)
  • Using > when the boundary should count. "At least 50" includes 50, so it is >= 50; > 50 leaves 50 out. (Conditionals, Inequalities and Altering Code)
  • Ordering an elif chain badly. In if x > 0: followed by elif x > 10:, the second block can never run, because every x greater than 10 is already caught by the first test. Put the narrower test first. (Conditionals, Inequalities and Altering Code)
  • Treating a loop's output as every solution. A loop over range(0, 11) tests only the whole numbers 0 to 10; the inequality is also true for 4.5, 11 and 1000. (Conditionals, Inequalities and Altering Code)

Practice test

18 questions from across the unit. Each one shows the lesson it comes from.

  1. Variables, Expressions and Output in Python
    1
    In Python, what does the line x = 7 do?
  2. Variables, Expressions and Output in Python
    2
    What does print(20 - 4 * 3 + 2) print?
  3. Variables, Expressions and Output in Python
    3
    What does this program print?x = 4 y = x + 6 x = 20 print(y)
  4. Variables, Expressions and Output in Python
    4
    Match each Python operator to what it does.
    *
    /
    //
    %
    **
  5. Variables, Expressions and Output in Python
    5
    What does print(5 ** 2 - 4) print?
  6. Variables, Expressions and Output in Python
    6
    The variable h already holds 3. Which line stops the program with an error?
  7. Loops: Patterns and Tables of Values
    7
    How many lines does this program print?for i in range(2, 9): print(i)
    lines
  8. Loops: Patterns and Tables of Values
    8
    What does this program print?total = 0 for n in range(1, 6): total = total + 2 * n print(total)
  9. Loops: Patterns and Tables of Values
    9
    What does this program print?total = 0 for n in range(1, 4): total = total + n print(total)
  10. Loops: Patterns and Tables of Values
    10
    Put these lines in order to make a program that adds 1 + 2 + 3 + 4 + 5 and prints the answer once.
    1. 1for n in range(1, 6):
    2. 2print(total) (not indented)
    3. 3 total = total + n (indented)
    4. 4total = 0
  11. Loops: Patterns and Tables of Values
    11
    This program prints x and y for 4 values of x. By how much does y increase from each line to the next?for x in range(1, 5): print(x, 4 * x + 1)
  12. Loops: Patterns and Tables of Values
    12
    A pattern has 4 squares in stage 1 and gains 3 squares each stage. Which one-line loop prints the stage number and the number of squares for stages 1 to 10?
  13. Conditionals, Inequalities and Altering Code
    13
    Which Python condition means ?
  14. Conditionals, Inequalities and Altering Code
    14
    How many numbers does this program print?for x in range(1, 21): if 4 * x - 3 > 50: print(x)
    numbers
  15. Conditionals, Inequalities and Altering Code
    15
    In the lesson's phone-plan program (Plan A: 20 dollars plus 5 dollars per GB; Plan B: 40 dollars plus 1 dollar per GB), what line is printed when gb is 5?
  16. Conditionals, Inequalities and Altering Code
    16
    A program should print "pass" when score is 50 or more. It uses if score > 50:. What is wrong, and what is the fix?
  17. Conditionals, Inequalities and Altering Code
    17
    Where is the point (2, 9) compared with the line ? This program decides.x = 2 y = 9 if y == 3 * x + 2: print("on the line") elif y > 3 * x + 2: print("above the line") else: print("below the line")
  18. Conditionals, Inequalities and Altering Code
    18
    The lesson's phone-plan program starts with the parameters fee_a = 20, rate_a = 5, fee_b = 40 and rate_b = 1. To model Plan A raising its rate to 6 dollars per GB, which single line should you change?

Endless drill

Fresh numbers every time, for the calculation questions in this unit.

Endless drillnew numbers every time

Generating a problem…

Keep going