1-D Arrays
Store a fixed collection of related values using a Python list.
An array stores multiple values under one name. Python lists are used as the equivalent structure in these lessons; when modelling a static array, keep its length fixed.
Predict
Read the code first. Do not run it yet.
scores = [12, 18, 15, 20]
print(scores[0])
scores[2] = 17
print(scores)- What is printed first?
- Which element changes?
- How many elements are in the array?
Investigate
Use this code to explain exactly how the program works.
temperatures = [14, 16, 15, 18, 17]
total = 0
for i in range(len(temperatures)):
total = total + temperatures[i]
print(total)- What values will
itake? - Why does
range(len(...))visit every valid index? - Predict the final total.
Indexing an array
names = ["Ava", "Ben", "Cara", "Dan"]
print(names[2])
names[1] = "Bea"
print(names)Cara ['Ava', 'Bea', 'Cara', 'Dan']
- The first element is at index 0.
- Individual elements can be read or replaced using square brackets.
- For a fixed-length array, do not add or remove elements during the task.
list. It is the normal Python equivalent used for 1-D array tasks.Self-study guide
A one-dimensional array stores several related values under one name, with each item accessed by an index. OCR requires arrays to be treated as fixed-length or static structures. Python lists are more flexible than OCR arrays, so in these lessons we deliberately use Python lists as fixed-length arrays.
That means you should not use append(), remove() or other operations that change the number of elements when practising OCR array problems. Keep the length fixed and replace values by index when necessary. OCR Exam Reference Language also treats an array as storing one data type, so use consistent element types in pure array tasks.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
array colours[5] | colours = ["", "", "", "", ""] | OCR can declare a fixed-size array; Python uses a list of five existing elements. |
array colours = ["Blue", ...] | colours = ["Blue", ...] | Both can be initialised with values. |
names[3] = "Noni" | names[3] = "Noni" | Indexed assignment is conceptually the same. |
0 indexed | 0 indexed | First element is index 0 in both. |
Worked example
Processing every array element with a loop is a core pattern.
scores = [68, 74, 91, 55, 83]
total = 0
for score in scores:
total = total + score
average = total / len(scores)
print(total)
print(average)371 74.2
- The list contains five fixed positions.
- The loop visits each stored score exactly once.
totalaccumulates the values.len(scores)returns 5 and is used to calculate the average.
Another example
Use indexes when you need to change stored elements.
levels = [3, 5, 2, 7]
for index in range(len(levels)):
levels[index] = levels[index] + 1
print(levels)[4, 6, 3, 8]
range(len(levels))produces every valid index.- Each position is replaced with an updated value.
- The number of elements stays the same.
Common mistakes
- Using index 1 for the first element; arrays are zero-indexed.
- Using Python
append()and calling that an OCR fixed-length array solution. - Looping to
len(array)inclusive, which would create an out-of-range index.
Exam-ready checklist
- Access and update an array element by index.
- Process every element with iteration.
- Use
len()safely with indexes. - Explain the fixed/static OCR expectation despite Python list flexibility.
What you have learned
- A 1-D array stores a sequence of related values.
- Python lists are used as the array equivalent.
- Indices start at 0.
- A static array keeps the same number of elements.