← 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 = expressionworks 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 givesTrueorFalse.- 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 givesTrueorFalse. - In an
if,elif,elsechain Python runs the block under the firstTruecondition, or theelseblock if none isTrue, so exactly one block runs. - A loop with an
ifinside 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
4hor2(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, writex == 5, which givesTrueorFalse. (Variables, Expressions and Output in Python) - Expecting a variable to update by itself. If
costwas calculated fromh, changinghlater leavescostunchanged until you run the calculation line again. (Variables, Expressions and Output in Python) - Typing
^for a power. In Python,2 ^ 3is a different operation and prints 1, not 8; powers use a double star, so write2 ** 3. (Variables, Expressions and Output in Python) - Expecting
range(1, 10)to include 10. It stops at 9; to reach 10, userange(1, 11). (Loops: Patterns and Tables of Values) - Using
range(5)when you want 1 to 5. It gives 0, 1, 2, 3, 4; writerange(1, 6). (Loops: Patterns and Tables of Values) - Putting
total = 0inside 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
forline, 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;> 50leaves 50 out. (Conditionals, Inequalities and Altering Code) - Ordering an elif chain badly. In
if x > 0:followed byelif 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.
- Variables, Expressions and Output in Python1In Python, what does the line
x = 7do? - Variables, Expressions and Output in Python2What does
print(20 - 4 * 3 + 2)print? - Variables, Expressions and Output in Python3What does this program print?
x = 4 y = x + 6 x = 20 print(y) - Variables, Expressions and Output in Python4Match each Python operator to what it does.
*///%** - Variables, Expressions and Output in Python5What does
print(5 ** 2 - 4)print? - Variables, Expressions and Output in Python6The variable h already holds 3. Which line stops the program with an error?
- Loops: Patterns and Tables of Values7How many lines does this program print?
for i in range(2, 9): print(i)lines - Loops: Patterns and Tables of Values8What does this program print?
total = 0 for n in range(1, 6): total = total + 2 * n print(total) - Loops: Patterns and Tables of Values9What does this program print?
total = 0 for n in range(1, 4): total = total + n print(total) - Loops: Patterns and Tables of Values10Put these lines in order to make a program that adds 1 + 2 + 3 + 4 + 5 and prints the answer once.
- 1
for n in range(1, 6): - 2
print(total)(not indented) - 3
total = total + n(indented) - 4
total = 0
- 1
- Loops: Patterns and Tables of Values11This 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) - Loops: Patterns and Tables of Values12A 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?
- Conditionals, Inequalities and Altering Code13Which Python condition means ?
- Conditionals, Inequalities and Altering Code14How many numbers does this program print?
for x in range(1, 21): if 4 * x - 3 > 50: print(x)numbers - Conditionals, Inequalities and Altering Code15In 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?
- Conditionals, Inequalities and Altering Code16A program should print "pass" when
scoreis 50 or more. It usesif score > 50:. What is wrong, and what is the fix? - Conditionals, Inequalities and Altering Code17Where 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") - Conditionals, Inequalities and Altering Code18The lesson's phone-plan program starts with the parameters
fee_a = 20,rate_a = 5,fee_b = 40andrate_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…