Year 9 · Y9.3 Python Programming · Lesson 2

Stretch tasks Lists, 2D lists and dictionaries

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each code card to its output card.

  • 12 cards: each code has a output
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Lists, 2D lists and dictionaries

Lists, 2D lists and dictionaries: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What is printed?

    grid = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]
    print(grid[2][0])
    • 3
    • 8
    • 2
    • 7
  2. What is printed?

    grid = [[1, 2, 3],
            [4, 5, 6]]
    print(grid[1][2] + grid[0][1])

  3. What is printed?

    student = {"name": "Aisha", "year": 9}
    student["year"] = 10
    student["house"] = "Blue"
    print(len(student))
    • 2
    • 4
    • 10
    • 3
  4. In grid[1][2], the 1 is the column and the 2 is the row.

    • True
    • False
  5. What is printed?

    scores = [45, 78, 12, 90]
    scores.sort()
    print(scores[0], scores.index(90))
    • 45 3
    • 12 4
    • 45 90
    • 12 3
  6. You need to look up each student's score by their name. Which structure is best?

    • A dictionary
    • A single variable
    • A 2D list of numbers only
    • One long string
  7. What is printed?

    prices = {"pen": 1.5, "pad": 3.0}
    print(prices["pad"] * 2)

  8. Which TWO lines add 7 to the END of a list called nums?

    Tick 2.

    • nums.insert(0, 7)
    • nums.add(7)
    • nums[0] = 7
    • nums.append(7)
    • nums.insert(len(nums), 7)
  9. What is printed?

    count = 0
    for row in [[1, 2], [3, 4], [5, 6]]:
        for cell in row:
            count = count + cell
    print(count)
    • 6
    • 3
    • 12
    • 21
  10. What does 90 in scores give when 90 is in the list?

    • An error
    • True
    • 90
    • The index of 90
  11. Put the lines this program prints in order.

    for r in range(2):
        for c in range(2):
            print(r, c)

    Number these 1 to 4.

    • 1 0
    • 1 1
    • 0 0
    • 0 1
  12. A dictionary uses a key to look up its value, instead of a numbered index.

    • True
    • False

Quiz Challenge, round 2: Quick Fire

  1. Explain why a dictionary can be more readable than a list for storing one student's details. [2 marks]

  2. What is printed, and why? [2 marks]

    board = [["X", "O"],
             ["O", "X"]]
    print(board[0][1])

  3. Explain the difference between sorted(scores) and scores.sort(). [2 marks]

  4. seats is a 2D list of a cinema with 10 rows of 20 seats. What is wrong with seats[15][2]? [2 marks]

Quiz Challenge, round 3: Big Think

  1. Explain what this program prints, and how its nested loops work. [4 marks]

    grid = [[1, 2, 3],
            [4, 5, 6]]
    for row in grid:
        total = 0
        for value in row:
            total = total + value
        print(total)

  2. A teacher wants a program to store every student's marks for five tests. Compare a 2D list with a dictionary of lists, and say which you would use. [6 marks]

This or That: tick the right answer

  1. What is printed?

    grid = [[1, 2], [3, 4], [5, 6]]
    print(grid[2][0])
    • 5
    • 3
  2. What does scores.sort() do?

    • Sorts the list itself
    • Makes a sorted copy
  3. What is printed?

    nums = [3, 1, 2]
    nums.insert(0, 9)
    print(nums)
    • [9, 3, 1, 2]
    • [3, 1, 2, 9]
  4. What is printed?

    pet = {"name": "Rex", "age": 4}
    print(pet["age"])
    • 4
    • age
  5. How do you add a new pair to a dictionary called student?

    • student["house"] = "Blue"
    • student.append("Blue")
  6. What is printed?

    scores = [45, 78, 90]
    print(scores.index(90))
    • 2
    • 3
  7. You swap the row and column by mistake on a grid of 3 rows and 5 columns. What can happen?

    • Nothing: the two are interchangeable
    • An index out of range crash
  8. What is printed?

    student = {"name": "Aisha", "score": 82}
    student["score"] = 90
    print(student["score"])
    • 90
    • 82

Memory Match: match each code to its output

CodeOutput
A [[1, 2], [3, 4]][1][0]1 2
B [[1, 2], [3, 4]][0][1]2 [1, 2, 3]
C {"a": 5, "b": 7}["b"]3 True
D sorted([3, 1, 2])4 1
E [4, 8, 6].index(8)5 7
F 9 in [1, 9, 3]6 3
Answers

Round 1: Quiz Blitz

  1. 7 Row first, then column: row 2 is [7, 8, 9], and column 0 of it is 7.
  2. 8 grid[1][2] is 6 and grid[0][1] is 2, so 6 + 2 = 8.
  3. 3 Changing year does not add a pair, but adding house does: name, year and house.
  4. False Row first, then column. Getting them the wrong way round crashes on a grid that is not square.
  5. 12 3 sort changes the list to [12, 45, 78, 90]. The first item is 12, and 90 is now at index 3.
  6. A dictionary A dictionary maps a key, the name, to a value, the score.
  7. 6.0 The value for pad is 3.0, and 3.0 x 2 = 6.0.
  8. nums.append(7); nums.insert(len(nums), 7) append always adds at the end, and inserting at position len(nums) does the same. Lists have no add method.
  9. 21 The inner loop visits every number in every row: 1 + 2 + 3 + 4 + 5 + 6 = 21.
  10. True in checks membership and gives True or False.
  11. 0 0 → 0 1 → 1 0 → 1 1 The inner loop runs all the way through for each pass of the outer loop.
  12. True student["score"] is far easier to read than student[3].

Round 2: Quick Fire

    • Values are looked up by a meaningful key, such as student["score"]
    • With a list you have to remember which index holds what
    Model answer

    In a dictionary each value has a meaningful key, so student["score"] says exactly what it is. With a list you have to remember that, say, index 3 holds the score.

    • O
    • It is row 0, then column 1: row first, then column
    Model answer

    O is printed. board[0][1] means row 0, the first row, then column 1, the second item in it.

    • sorted gives back a new sorted copy and leaves the original unchanged
    • sort changes the list itself, in place
    Model answer

    sorted(scores) returns a new sorted copy and leaves scores unchanged. scores.sort() sorts the list itself, in place.

    • There is no row 15, so it causes an IndexError
    • The indexes are the wrong way round: it should be seats[2][15]
    Model answer

    There are only 10 rows, numbered 0 to 9, so row 15 does not exist and Python stops with an IndexError. The indexes are the wrong way round: the row comes first, so it should be seats[2][15].

Round 3: Big Think

    • The outer loop goes through each row in turn
    • total is reset to 0 at the start of each row
    • The inner loop adds every value in that row
    • It prints 6, then 15
    Model answer

    The outer loop goes through each row of the grid in turn. At the start of each row, total is reset to 0. The inner loop then goes through every value in that row and adds it to total, and after the inner loop finishes the row's total is printed. So it prints 6, for 1 + 2 + 3, then 15, for 4 + 5 + 6.

    • 2D list: each row is a student and each column a test
    • With a 2D list you must remember which row belongs to which student
    • Dictionary: each key is a student's name and its value is a list of marks
    • A dictionary makes it easy to look a student up by name
    • Both can be looped through to work out totals or averages
    • A 2D list makes it easy to work down a column, such as the average for one test
    • A justified choice
    Model answer

    In a 2D list each row would be a student and each column a test, which makes it easy to work down a column, for example to find the average for one test. But you have to remember which row number belongs to which student. In a dictionary of lists each key is a student's name and its value is a list of their five marks, so you can look a student up by name, which is far more readable. Both can be looped through to work out totals and averages. I would use the dictionary, because the teacher will mostly be looking up individual students, and marks["Aisha"] is much clearer than marks[7].

This or That

  1. 5 Row 2 is [5, 6], and item 0 of that row is 5.
  2. Sorts the list itself sort() changes the list in place. sorted(scores) gives a sorted copy and leaves the list alone.
  3. [9, 3, 1, 2] insert(0, 9) puts 9 at index 0, which is the start of the list.
  4. 4 A dictionary uses the key, here age, to look up its value.
  5. student["house"] = "Blue" Assigning to a new key adds the pair. Dictionaries have no append.
  6. 2 index gives the position of 90, counting from 0.
  7. An index out of range crash On a grid that is not square, swapping them can ask for a row or column that does not exist.
  8. 90 Assigning to a key that already exists replaces its value.

Memory Match

  • A 6 [[1, 2], [3, 4]][1][0]: 3
  • B 1 [[1, 2], [3, 4]][0][1]: 2
  • C 5 {"a": 5, "b": 7}["b"]: 7
  • D 2 sorted([3, 1, 2]): [1, 2, 3]
  • E 4 [4, 8, 6].index(8): 1
  • F 3 9 in [1, 9, 3]: True