A1. Using the grid below, what is grid[0][3]? What is grid[2][0]?
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
Solution
grid[0][3]→ 9grid[2][0]→ 6
Year 9 · Python
In Year 8 you used 1D lists with counting, totaling, max and min. This year you store data in a grid (a 2D array) and wrap useful code in functions. The techniques stay the same — you just apply them across rows and columns.
returnA one-dimensional array is a single row of values. A two-dimensional array is a grid: rows and columns. In Python we build this as a list of lists.
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
This grid has 3 rows and 4 columns:
| col 0 | col 1 | col 2 | col 3 | |
|---|---|---|---|---|
| row 0 | 4 | 7 | 2 | 9 |
| row 1 | 1 | 5 | 8 | 3 |
| row 2 | 6 | 0 | 4 | 2 |
grid[row][col]. First index is the row, second is the column.grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
print(grid[0][0]) # 4 top-left
print(grid[1][2]) # 8 row 1, column 2
grid[2][1] = 10
print(grid[2][1]) # 10
rows = len(grid) # 3
cols = len(grid[0]) # 4 (length of the first row)
print(rows, cols)
Use an outer loop for rows and an inner loop for columns:
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
for r in range(len(grid)):
for c in range(len(grid[r])):
print(grid[r][c], end=" ")
print() # new line after each row
Output:
You can also loop through rows as whole lists:
for row in grid:
for value in row:
print(value, end=" ")
print()
A function is a named block of code you can reuse. You define one with def.
def greet(name):
print("Hello,", name)
greet("Aisha")
greet("Ben")
Output:
name is a parameter — a placeholder for a value you pass in.greet("Aisha") runs the function with that argument.Use return when the function should give back an answer:
def double(n):
return n * 2
answer = double(7)
print(answer) # 14
print(double(10)) # 20
return in that function does not run. return ends the function straight away.Instead of copying the same nested loops everywhere, write a function once:
def print_grid(grid):
for row in grid:
for value in row:
print(value, end=" ")
print()
print_grid([[1, 2], [3, 4]])
Same pattern as Year 8: start a counter at 0, visit every cell, add 1 when the rule matches.
Try the interactive simulation: Counting in a 2D array on CompSimu.
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
count = 0
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] >= 5:
count = count + 1
print(count, "cells are 5 or more")
Output:
As a function:
def count_at_least(grid, limit):
count = 0
for row in grid:
for value in row:
if value >= limit:
count = count + 1
return count
scores = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
print(count_at_least(scores, 5))
Start total at 0 and add every cell.
Try the interactive simulation: Totalling a 2D array on CompSimu.
def total_grid(grid):
total = 0
for row in grid:
for value in row:
total = total + value
return total
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
print(total_grid(grid))
Output:
def row_total(grid, row_index):
total = 0
for value in grid[row_index]:
total = total + value
return total
print(row_total(grid, 0)) # 4+7+2+9 = 22
def column_total(grid, col_index):
total = 0
for r in range(len(grid)):
total = total + grid[r][col_index]
return total
print(column_total(grid, 2)) # 2+8+4 = 14
def average_grid(grid):
total = total_grid(grid)
cells = len(grid) * len(grid[0])
return total / cells
print(average_grid(grid))
Start with the first cell, then update when you find a better value.
Try the interactive simulation: Max & min in a 2D array on CompSimu.
def grid_max(grid):
biggest = grid[0][0]
for row in grid:
for value in row:
if value > biggest:
biggest = value
return biggest
def grid_min(grid):
smallest = grid[0][0]
for row in grid:
for value in row:
if value < smallest:
smallest = value
return smallest
grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
print("Max:", grid_max(grid))
print("Min:", grid_min(grid))
Output:
def max_position(grid):
best_r = 0
best_c = 0
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] > grid[best_r][best_c]:
best_r = r
best_c = c
return best_r, best_c
r, c = max_position(grid)
print("Biggest value at row", r, "column", c)
Output:
Analyse a classroom mark book (rows = students, columns = tests):
marks = [
[67, 72, 80],
[55, 61, 58],
[90, 88, 95],
[40, 47, 52]
]
def student_average(marks, student):
total = 0
for score in marks[student]:
total = total + score
return total / len(marks[student])
def count_fails(marks, pass_mark):
count = 0
for row in marks:
for score in row:
if score < pass_mark:
count = count + 1
return count
print("Student 2 average:", student_average(marks, 2))
print("Fails below 50:", count_fails(marks, 50))
print("Class max:", grid_max(marks))
print("Class min:", grid_min(marks))
| Mistake | Example | Fix |
|---|---|---|
| Swapping row and column | grid[col][row] | Remember grid[row][col] |
| Only looping one level | Visiting rows but not cells | Use a nested loop for every cell |
| Hard-coding sizes | Always using range(3) | Use len(grid) and len(grid[0]) |
Forgetting return | Function prints but caller gets None | Return the answer you need |
| Starting max at 0 always | All values negative | Start with grid[0][0] |
| Uneven rows assumed square | Different row lengths | Loop with len(grid[r]) per row |
Work through these in order. Solutions are hidden — try each yourself, then click Show solution.
grid[0][3]? What is grid[2][0]?grid = [
[4, 7, 2, 9],
[1, 5, 8, 3],
[6, 0, 4, 2]
]
Solution
grid[0][3] → 9grid[2][0] → 6len.Solution
Rows: len(grid) → 3
Columns: len(grid[0]) → 4
grid = [[2, 4], [6, 8]]
for r in range(len(grid)):
for c in range(len(grid[r])):
print(grid[r][c] * 2)
Solution
Output:
1.grid = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
Solution
for r in range(len(grid)):
for c in range(len(grid[r])):
grid[r][c] = 1
def square(n):
return n * n
print(square(5))
print(square(3) + 1)
Solution
Output:
add(a, b) that returns the sum of two numbers. Then use it to print add(10, 4).Solution
def add(a, b):
return a + b
print(add(10, 4))
def larger(a, b):
if a > b:
print(a)
else:
print(b)
Solution
It prints instead of returning. Fixed version:
def larger(a, b):
if a > b:
return a
else:
return b
row_length(grid, r) that returns how many cells are in row r.Solution
def row_length(grid, r):
return len(grid[r])
data = [
[0, 2, 0],
[4, 0, 5],
[1, 1, 0]
]
Solution
def count_zeros(grid):
count = 0
for row in grid:
for value in row:
if value == 0:
count = count + 1
return count
print(count_zeros(data)) # 4
total_grid(grid) and use it to find the total of:temps = [
[12, 14, 11],
[15, 13, 16]
]
Solution
def total_grid(grid):
total = 0
for row in grid:
for value in row:
total = total + value
return total
print(total_grid(temps)) # 81
grid_max(grid) and grid_min(grid) for:heights = [
[150, 162, 148],
[171, 155, 160],
[149, 168, 158]
]
Solution
def grid_max(grid):
biggest = grid[0][0]
for row in grid:
for value in row:
if value > biggest:
biggest = value
return biggest
def grid_min(grid):
smallest = grid[0][0]
for row in grid:
for value in row:
if value < smallest:
smallest = value
return smallest
print(grid_max(heights)) # 171
print(grid_min(heights)) # 148
c in a grid.Solution
def column_total(grid, c):
total = 0
for r in range(len(grid)):
total = total + grid[r][c]
return total
rain = [
[2, 0, 1, 0, 3],
[0, 0, 5, 1, 0],
[4, 2, 0, 0, 1]
]
Solution
def total_grid(grid):
total = 0
for row in grid:
for value in row:
total = total + value
return total
def count_value(grid, target):
count = 0
for row in grid:
for value in row:
if value == target:
count = count + 1
return count
def grid_max(grid):
biggest = grid[0][0]
for row in grid:
for value in row:
if value > biggest:
biggest = value
return biggest
days = len(rain) * len(rain[0])
print("Total:", total_grid(rain))
print("Average:", total_grid(rain) / days)
print("Dry days:", count_value(rain, 0))
print("Max:", grid_max(rain))
row_averages(grid) that returns a 1D list of the average of each row.marks = [
[10, 8, 9],
[6, 7, 5],
[9, 9, 10]
]
Solution
def row_averages(grid):
averages = []
for row in grid:
total = 0
for value in row:
total = total + value
averages.append(total / len(row))
return averages
print(row_averages(marks)) # [9.0, 6.0, 9.333...]
best_student(marks) that returns the row index of the student with the highest total across all tests.marks = [
[67, 72, 80],
[55, 61, 58],
[90, 88, 95],
[40, 47, 52]
]
Solution
def row_total(grid, r):
total = 0
for value in grid[r]:
total = total + value
return total
def best_student(marks):
best = 0
for r in range(len(marks)):
if row_total(marks, r) > row_total(marks, best):
best = r
return best
print(best_student(marks)) # 2