In this topic students learn to store data that has rows and columns in a 2D array, and to work through one with nested loops.
Be able to read, comprehend, trace, adapt and create Python code that:
for or while loopsThis chapter has no slide deck. It was written for Dodona from new material by the course author, after the rest of the course had been converted.
This chapter builds directly on Lists. The exam specification calls the structure a 2D array, and the activities use that term throughout. Python has no separate array type for it: it is a list whose items are lists. The theory page says so once and points back to the Lists chapter, because everything students already know about a list still works on the outer list (which holds rows) and on each row.
The most useful thing to establish early is the reading order of
array[row][column]: Python picks the row first, and then an item from that
row. Students who read scores[1][3] as “row 1, then item 3 of that row”
rarely swap the indices; students who read it as “a coordinate” often do,
especially if they are used to (x, y) from maths, where across comes first.
Nested loops are the second idea. Students first met them in the String manipulation chapter, over a list of words; here they get their natural home. Tracing one on the board, with a column for each counter, is worth the time: the moment students see that the inner counter runs all the way through before the outer one moves on, the rest of the chapter follows.
[["a", "b"], ["c", "d"]]. Laying
it out one row per line makes the code look like the table it stores.array[row][column] picks one item. array[row] on its own is a whole row.len(array) is the number of rows; len(array[row]) is the number of items
on that row.append and index are used on a row:
animals[0].append("Trout"), words[0].index(word).while loops the inner counter has to be reset inside the outer
loop. Nested for loops do that by themselves.seats[c][r] instead of seats[r][c]. It is not an
error Python reports while both numbers are in range, and with a 3 by 4 array
it crashes only for column 3. The first Modify task is built on exactly this
bug, and its test cases include seats where the swap happens to give the
right answer and seats where it does not.str()
before it can be joined onto text with +; without it the line stops with a
TypeError. The Predict task shows the fix in place,
scores[1][0] + " " + str(scores[1][4]), so students do not run into the
error there. The Modify and Make exercises, which print row and column
numbers and averages inside sentences, are where they have to apply str()
themselves.range(0, 4) get a TypeError when the name is added to the total.total = 0 before the outer loop,
the totals run on from one student to the next: 31, 54 and 79 instead of 31,
23 and 25. It is a logical error, not a crash, which is why it is worth
tracing./ instead of //. The averages exercise asks for integer division. With
/, Ali’s average prints as 53.333333333333336 and Bea’s as 75.0..index() on a missing item. It stops the program with a ValueError.
The theory page shows in as the check to make first.