Two-dimensional arrays

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Two-dimensional arrays

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A 2D array stores data in...
A A single line of values only
B Rows and columns, like a table
2. Which gets the element in row 2, column 3 of a seating plan?
A seats[3][2]
B seats[2][3]
3. In grid[1][4], what does the 4 represent?
A The column index
B The row index
4. Which Python code creates a 2D array with 2 rows and 3 columns?
A grid = [[0, 0, 0], [0, 0, 0]]
B grid = [0, 0, 0, 0, 0, 0]
5. A game board has 3 rows and 3 columns. How many elements does it hold?
A 9
B 6

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1seats[2][0] ABoth indexes are put in one set of square brackets. Each index needs its own brackets, so use seats[0][1].
2board = [["X", "O"], ["O", "X"]] Brows = len(timetable)
3Each row of a timetable is an inner list, so len() of the outer list gives the number of rows CThe row index 2 is out of range because there are only 2 rows, numbered 0 and 1. Use timetable[1][0] to get Art.
4Change one cell of a game board by giving its row index first, then its column index Dboard[1][2] = "X"
5seats = [["Ava", "Liam"], ["Mia", "Noah"]] print(seats[0, 1]) EGets the element in row 2, column 0 of the seating plan: row index first, then column index
6timetable = [["Maths", "English"], ["Art", "PE"]] print(timetable[2][0]) FCreates a 2D array for a game board with 2 rows and 2 columns, as a list of row lists

Definitions

grid[row][col]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
grid = [[1, 2, 3], [4, 5, 6]] print(grid[_][2]) # second row, third column
2.
board = [["X", "O"], ["O", "X"]] print(board[0][_ ]) # first row, second column
3.
board = [["-", "-"], ["-", "-"]] _____[1][0] = "X" print(board)
4.
for r in range(___(grid)): for c in range(len(grid[r])): print(grid[r][c])
5.
board = [["X", "O"], ["O", "X"]] if board[0][0] __ "X": print("Top left is X")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A grid = [[1, 2, 3], [4, 5, 6]]
B row = 1
C col = 3
D print(grid[row][col])
Question 2
A board = [["X","O"],["O","X"]
B row = 0
C col = 1
D print(board[row][col])
Question 3
A plan = [[0, 0, 0], [0, 0, 0]]
B plan[1, 2] = 5
C print(plan[1][2])
Question 4
A grid = [[1, 2], [3, 4]]
B total = 0
C for r in range(2):
D for c in range(3):
E total += grid[r][c]
Question 5
A board = [["X","O"],["O","X"]]
B board[0][1] = X
C print(board[0][1])

Answer Key

Two-dimensional arrays
Section A: Quick Questions
Q1
B — Rows and columns, like a table
Q2
B — seats[2][3]
Q3
A — The column index
Q4
A — grid = [[0, 0, 0], [0, 0, 0]]
Q5
A — 9
Section B: Match the Pairs
Q1
seats[2][0] → Gets the element in row 2, column 0 of the seating plan: row index first, then column index
Q2
board = [["X", "O"], ["O", "X"]] → Creates a 2D array for a game board with 2 rows and 2 columns, as a list of row lists
Q3
Each row of a timetable is an inner list, so len() of the outer list gives the number of rows → rows = len(timetable)
Q4
Change one cell of a game board by giving its row index first, then its column index → board[1][2] = "X"
Q5
seats = [["Ava", "Liam"], ["Mia", "Noah"]] print(seats[0, 1]) → Both indexes are put in one set of square brackets. Each index needs its own brackets, so use seats[0][1].
Q6
timetable = [["Maths", "English"], ["Art", "PE"]] print(timetable[2][0]) → The row index 2 is out of range because there are only 2 rows, numbered 0 and 1. Use timetable[1][0] to get Art.
Section C: Definitions
Q1
grid[row][col] — A 2D array stores data in rows and columns like a table. Use the row index first, then the column index, to reach one element, such as a seat in a seating plan.
Section D: Fill in the Blanks
Q1
grid = [[1, 2, 3], [4, 5, 6]] print(grid[1][2]) # second row, third column
Q2
board = [["X", "O"], ["O", "X"]] print(board[0][1 ]) # first row, second column
Q3
board = [["-", "-"], ["-", "-"]] board[1][0] = "X" print(board)
Q4
for r in range(len(grid)): for c in range(len(grid[r])): print(grid[r][c])
Q5
board = [["X", "O"], ["O", "X"]] if board[0][0] == "X": print("Top left is X")
Section E: Spot the Error
Q1
C — Each row has 3 columns, so valid column indexes are 0, 1 and 2. Column 3 gives an IndexError. It should be col = 2.
Q2
A — The outer list is missing its closing bracket, so the board is a syntax error. The line should end with ]] so both rows are inside the outer list.
Q3
B — Python lists need one pair of brackets per index: the row first, then the column. plan[1, 2] causes a TypeError. It should be plan[1][2] = 5.
Q4
D — Each row has only 2 columns, so the inner loop must use range(2). range(3) makes c reach 2 and causes an IndexError.
Q5
B — The text X needs quotes, otherwise Python looks for a variable called X and raises a NameError. It should be board[0][1] = "X".