- Home
- Lessons
- GCSE & IGCSE Computer Science
- Arrays, 2D arrays and records
Arrays, 2D arrays and records
🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.
When a program has lots of related values, store them together in a data structure instead of in dozens of separate variables.
One-dimensional arrays (lists)
scores = [12, 7, 19, 4]
Items are numbered from 0: scores[0] is 12 and scores[2] is 19.
len(scores) gives 4. scores.append(10) adds 10 to the end.
scores[1] = 8 changes the second item.
In Python, arrays are written as lists.
Items are numbered from 0: scores[0] is 12 and scores[2] is 19.
len(scores) gives 4. scores.append(10) adds 10 to the end.
scores[1] = 8 changes the second item.
In Python, arrays are written as lists.
Two-dimensional arrays
A 2D array is a list of lists, like a table of rows and columns.
grid = [[3, 8, 1], [6, 2, 9]]
grid[row][column]: grid[1][2] is row 1, column 2, which is 9.
grid = [[3, 8, 1], [6, 2, 9]]
grid[row][column]: grid[1][2] is row 1, column 2, which is 9.
Records
A record groups related fields that can be of different data types, such as one student's details.
student = ["Zara", 15, 62.5, True] (name, age, height, member)
A table of students can be stored as a list of records.
student = ["Zara", 15, 62.5, True] (name, age, height, member)
A table of students can be stored as a list of records.
temps = [14, 17, 12, 20]. Find the average with a loop.
- Add the items: 14 + 17 + 12 + 20 = 63
- Divide by len(temps) = 4
Answer: 15.75
Lists (arrays) store many values under one name, indexed from 0. A 2D array is a list of lists: grid[row][column]. A record groups related fields of different types.
The interactive lesson includes the diagrams for this topic.
Check you have got it
Answer 6 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.