GCSE · Computer Science · OCR · Spec J277
1D arrays
Four friends stored under one name: players. Ask for players[1] and you get Ben, not Ana. If that feels wrong, good. That's exactly the bit this lesson sorts out.
Computer Science · Arrays
Two loops, one array
Four names stored under one name, players. Step through two different loops over the same array and watch what each one actually gets hold of.
Before you press Next: Cal is the third name. What index do you think Cal is at?
1 players = ["Ana", "Ben", "Cal", "Dev"]2 for player in players:3 print(player)4 for i in range(len(players)):5 print(i, players[i])
| player | i | players[i] |
|---|---|---|
Output
Step 1: One name, four items. Here a Python list is being used as an array: four slots, every item a string, and that length and data type stay fixed.
Each row is one pass of a loop. The player column fills during loop 1; i and players[i] fill during loop 2.
Computer Science · Choosing a data structure
Would an array do the job?
Three checks: Do you know how many items there are at the start? Will items need adding or removing? Is all the data one type?
Still to sort
An array suits this (0)
Passes all three checks.
Where the line is: Changing a value later doesn't rule out an array. Only adding or removing items does.
An array doesn't suit this (0)
Fails at least one check.
Where the line is: One failed check is enough: an unknown number of items, items added or removed, or mixed data types.
Six jobs. Run the three checks on each one before you place it.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
A row of numbered slots under one name. You can change what's in a slot, but you can't add a new one. And the numbering starts at 0.
What you need to know
- An array stores lots of items under one name, keeping your data organised and easy to get at.
- Each item sits at an index, which is simply its position.
- Indexing is zero-based: the first item is at index 0, not 1.
Have a gocolours = ["red", "green", "blue"]. Your mate swears colours[1] is "red" and is willing to bet their lunch on it. Before you look: what's really at index 1?
"green". Your mate owes you lunch.
"red" is at index 0, because counting starts at 0, so index 1 is the second item. Your mate counted from 1.
- You access an item by giving its index, like players[2]; an expression such as i + 1 works too, if it gives an integer.
Have a goSame colours array, and i holds 1. Which item does colours[i + 1] give you?
"blue"
i + 1 works out to 2, an integer, so it's used as the index, and index 2 is the third item. "green" comes from using i and forgetting the + 1.
- An array is static: it reserves memory for a set amount of data, so its size can't change.
- Every item in an array has to be the same data type.
- You can change what an item holds, but you can't add items or delete them.
Have a goAn array holds [5, 8, 3]. Which of these is allowed: changing the 8 into a 9, or putting a 4 on the end?
Changing the 8 into a 9.
An array's contents can be changed, but its size is fixed, so a fourth item can't be added.
- A list is different: it's dynamic, can grow or shrink, and can hold mixed data types.
- In Python, a list can act as an array, provided its length and data type stay fixed.
- Visit every item with for item in array, or with for i in range over its length, using i as the index.
The big picture
An array holds multiple items of one data type under one name. It's static: its size is fixed, so you can change items but not add or delete them. Each item has an index, starting at 0, and an index can be any expression that works out to an integer. You can visit every item with for item in array, or with for i in range over the array's length, using i as the index. A list, by contrast, is dynamic and can hold mixed data types.
Key points
Worked example
Problem
speeds = [30, 40, 50, 60, 70] is a list being used as an array. A program runs i = 2, then speeds[i + 1] = 45, then print(speeds[i - 2]) and print(speeds[4]). What gets printed, and what does speeds hold at the end?
⚠ Watch out
Counting from 1. In an array of 5 items the first is at index 0 and the last at index 4, so array[1] is the second item, and array[5] isn't one of its items at all.
Memory hook
A fixed row of numbered boxes: you can swap what's in a box, but you can't add a box. And the first box is box 0.
Check yourself
Cover the page. Give three ways an array differs from a list. Then, for nums = [4, 8, 15, 16], say which item nums[2] gives you and which index holds 16.
Flashcards
(14)What is an array?
Why is an array called a static data structure?
What rule do all the items in an array follow about data type?
In an array, can you change an item? Can you add or delete one?
What is an item's index?
At which index is the first item of an array?
An array has 6 items. What are its first and last indexes?
Can an expression be used as an index?
How does a list differ from an array in size?
How does a list differ from an array in data types?
When is an array a suitable choice?
In for item in array, what does the variable hold each time round?
In for i in range over an array's length, what values does i take, and what is it for?
When can a Python list be used as an array?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
- 2D arrays
- Basic string manipulation (concatenation, slicing)
- Boolean operators AND, OR, NOT
- Casting
- Constants
- Data type: Boolean
- Data type: Character and string
- Data type: Integer
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.