On this page
  1. Welcome
  2. Part 1 · 2D arrays
  3. Part 2 · Nested loops
  4. Part 3 · Functions
  5. Part 4 · Counting
  6. Part 5 · Totaling
  7. Part 6 · Max and min
  8. Part 7 · Combined
  9. Common mistakes
  10. Practice
  11. Quizzes
  12. Challenges

Year 9 · Python

Two-dimensional arrays and functions

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.

  • Create and index a 2D array (list of lists)
  • Visit every cell with nested loops
  • Write functions with parameters and return
  • Use counting, totaling, max and min on 2D data

Part 1: What is a 2D array?

A 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]
]
Each value sits at grid[row][column]. Indexes start at 0. The highlighted cell is row 1, column 2.

This grid has 3 rows and 4 columns:

col 0col 1col 2col 3
row 04729
row 11583
row 26042
⚠️ Order matters: grid[row][col]. First index is the row, second is the column.

Part 2: Indexing and nested loops

Reading and changing a cell

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

Number of rows and columns

rows = len(grid)          # 3
cols = len(grid[0])       # 4  (length of the first row)
print(rows, cols)
💡 This assumes every row has the same length — the usual case for a rectangular 2D array in school work.

Visiting every cell with nested loops

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:

4 7 2 9 1 5 8 3 6 0 4 2

You can also loop through rows as whole lists:

for row in grid:
    for value in row:
        print(value, end=" ")
    print()

Part 3: Function definitions

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:

Hello, Aisha Hello, Ben
  • name is a parameter — a placeholder for a value you pass in.
  • Calling greet("Aisha") runs the function with that argument.

Returning a value

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
⚠️ Code after a return in that function does not run. return ends the function straight away.

Why functions help with 2D arrays

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]])

Part 4: Technique — counting in 2D

Same pattern as Year 8: start a counter at 0, visit every cell, add 1 when the rule matches.

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:

5 cells are 5 or more

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))

Part 5: Technique — totaling in 2D

Start total at 0 and add every cell.

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:

51

Total of one row

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

Total of one column

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

Average of the whole grid

def average_grid(grid):
    total = total_grid(grid)
    cells = len(grid) * len(grid[0])
    return total / cells

print(average_grid(grid))

Part 6: Technique — maximum and minimum in 2D

Start with the first cell, then update when you find a better value.

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:

Max: 9 Min: 0

Finding where the maximum is

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:

Biggest value at row 0 column 3

Part 7: Putting it together

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))

Common mistakes checklist

MistakeExampleFix
Swapping row and columngrid[col][row]Remember grid[row][col]
Only looping one levelVisiting rows but not cellsUse a nested loop for every cell
Hard-coding sizesAlways using range(3)Use len(grid) and len(grid[0])
Forgetting returnFunction prints but caller gets NoneReturn the answer you need
Starting max at 0 alwaysAll values negativeStart with grid[0][0]
Uneven rows assumed squareDifferent row lengthsLoop with len(grid[r]) per row

Practice questions

Work through these in order. Solutions are hidden — try each yourself, then click Show solution.

Section A: 2D arrays

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]
]

A2. How many rows and columns does that grid have? Write Python expressions using len.

A3. What will this code print?

grid = [[2, 4], [6, 8]]
for r in range(len(grid)):
    for c in range(len(grid[r])):
        print(grid[r][c] * 2)

A4. Write nested loops that set every cell in a 3×3 grid of zeros to the value 1.

grid = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
]

Section B: Functions

B1. What will this code print?

def square(n):
    return n * n

print(square(5))
print(square(3) + 1)

B2. Write a function add(a, b) that returns the sum of two numbers. Then use it to print add(10, 4).

B3. This function is supposed to return the larger of two values, but it is wrong. Fix it.

def larger(a, b):
    if a > b:
        print(a)
    else:
        print(b)

B4. Write a function row_length(grid, r) that returns how many cells are in row r.

Section C: Counting, totaling, max and min in 2D

C1. Write a function that counts how many zeros are in a 2D grid.

data = [
    [0, 2, 0],
    [4, 0, 5],
    [1, 1, 0]
]

C2. Write a function total_grid(grid) and use it to find the total of:

temps = [
    [12, 14, 11],
    [15, 13, 16]
]

C3. Write functions grid_max(grid) and grid_min(grid) for:

heights = [
    [150, 162, 148],
    [171, 155, 160],
    [149, 168, 158]
]

C4. Write a function that returns the total of column c in a grid.

Section D: Challenge questions

D1. Rainfall grid. Rows are weeks, columns are days Mon–Fri. Write a program using functions that prints: total rainfall, average per day, number of dry days (0), and the maximum value.

rain = [
    [2, 0, 1, 0, 3],
    [0, 0, 5, 1, 0],
    [4, 2, 0, 0, 1]
]

D2. Write a function row_averages(grid) that returns a 1D list of the average of each row.

marks = [
    [10, 8, 9],
    [6, 7, 5],
    [9, 9, 10]
]

D3. Write a function 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]
]

Well done! You can now work with 2D arrays and functions, and apply counting, totaling, max and min across a grid. Next try the Year 9 quizzes, then the harder practice challenges (one challenge per page, with a hidden solution), then keep practising by inventing your own tables — scores, temperatures, game maps — and writing functions to analyse them.