Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1if mark >= 50: passed.append(row) | AThe append is outside the if, so every record is added and nothing is filtered out. The append must be inside the if so only matches are kept. | |
| 2if age >= 16 and house == 'Red': | BA compound condition: a record is kept only if both tests are true | |
| 3Build a result list by starting with an empty list, looping over every record, and adding only the matches | Cif year == 11 or year == 12: | |
| 4A filter that keeps students who are in Year 11 or Year 12 using a compound condition | DThe result list is created inside the if, so it is reset to empty for each match and only the last match survives. It must be created once before the loop. | |
| 5for row in rows: if row[2] > 80: result = [] result.append(row) | Eresult = [] for row in rows: if row[2] > 80: result.append(row) | |
| 6for row in rows: result.append(row) if row[2] > 80: print(row) | FKeeps the record only when its mark meets the condition, by adding it to the passed list |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
filter — Keep only the records in a stream that meet a condition and discard the rest, usually with an if inside a loop that adds matches to a result list.adults = []
for p in people:
if p["age"] >= 18: adults.append(p)high = []
for r in records:
if r["mark"] >= 80: high.append(r)for s in rows:
if s["yr"] == 11 and s["m"] > 70:
out.append(s)matches = []
for t in tasks:
if t["done"]: matches.append(t)cheap = []
for p in items:
if p["price"] < 10: cheap.append(p)