13-1d-arrays.py
python-programming / arrays  ·  Lesson 14
Lesson 14 — Arrays

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.

1d-arrays-predict.py
scores = [12, 18, 15, 20]
print(scores[0])
scores[2] = 17
print(scores)
Predict before running
  • 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.

1d-arrays-investigate.py
temperatures = [14, 16, 15, 18, 17]
total = 0
for i in range(len(temperatures)):
    total = total + temperatures[i]
print(total)
Investigate the code
  • What values will i take?
  • Why does range(len(...)) visit every valid index?
  • Predict the final total.

Indexing an array

array.py
names = ["Ava", "Ben", "Cara", "Dan"]
print(names[2])
names[1] = "Bea"
print(names)
Output
Cara
['Ava', 'Bea', 'Cara', 'Dan']
How it works
  • 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.
Array and Python listPython calls this structure a 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 focusUse 1-D arrays practically, understand zero-based indexing, and treat them as fixed-length/static for OCR purposes.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat 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 indexed0 indexedFirst element is index 0 in both.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Processing every array element with a loop is a core pattern.

lesson-14-worked.py
scores = [68, 74, 91, 55, 83]
total = 0

for score in scores:
    total = total + score

average = total / len(scores)

print(total)
print(average)
Output
371
74.2
Work through it line by line
  • The list contains five fixed positions.
  • The loop visits each stored score exactly once.
  • total accumulates the values.
  • len(scores) returns 5 and is used to calculate the average.

Another example

Use indexes when you need to change stored elements.

lesson-14-example-2.py
levels = [3, 5, 2, 7]

for index in range(len(levels)):
    levels[index] = levels[index] + 1

print(levels)
Output
[4, 6, 3, 8]
Why this example matters
  • range(len(levels)) produces every valid index.
  • Each position is replaced with an updated value.
  • The number of elements stays the same.

Common mistakes

Avoid these errors
  • 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-safe warningPython lists can resize and can mix types, but OCR arrays are fixed/static and are described as storing one data type. For pure array tasks, keep the Python list length unchanged and use a consistent element type.

Exam-ready checklist

You should now be able to
  • 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.