You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
An array (called a list in Python) is a data structure that stores multiple values under a single identifier. Arrays are essential for managing collections of data efficiently and are a key topic in GCSE Computer Science (AQA 3.2 / OCR J277 2.2).
An array is an ordered collection of elements of the same data type, stored in contiguous memory locations. Each element is accessed by its index (position number).
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | "Alice" | "Bob" | "Charlie" | "Diana" | "Eve" |
Key Point: In most programming languages (including Python), array indices start at 0. This is called zero-based indexing. In AQA pseudocode, indices also start at 0.
names ← ["Alice", "Bob", "Charlie", "Diana", "Eve"]
scores ← [85, 72, 91, 64, 78]
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
scores = [85, 72, 91, 64, 78]
Use the index to read or modify a specific element:
OUTPUT names[0] # Outputs: Alice
names[2] ← "Chris" # Changes "Charlie" to "Chris"
print(names[0]) # Output: Alice
names[2] = "Chris" # Changes "Charlie" to "Chris"
Exam Tip: Attempting to access an index that does not exist (e.g.
names[10]when the array only has 5 elements) causes an index out of bounds error.
To find out how many elements are in an array:
LEN(names) returns 5len(names) returns 5for i in range(len(names)):
print(names[i])
for name in names:
print(name)
In pseudocode:
FOR EACH name IN names
OUTPUT name
ENDFOR
flowchart LR
subgraph names["names array (length 5)"]
direction LR
I0["[0]<br/>Alice"]
I1["[1]<br/>Bob"]
I2["[2]<br/>Charlie"]
I3["[3]<br/>Diana"]
I4["[4]<br/>Eve"]
end
I0 --> I1 --> I2 --> I3 --> I4
names.append("Frank") # Adds "Frank" to the end
names.insert(2, "Zara") # Inserts "Zara" at index 2
names.remove("Bob") # Removes the first occurrence of "Bob"
names.pop(0) # Removes and returns the element at index 0
if "Alice" in names:
print("Found Alice")
scores = [85, 72, 91, 64, 78]
highest = scores[0]
for i in range(1, len(scores)):
if scores[i] > highest:
highest = scores[i]
print(f"Highest score: {highest}")
In pseudocode:
highest ← scores[0]
FOR i ← 1 TO LEN(scores) - 1
IF scores[i] > highest THEN
highest ← scores[i]
ENDIF
ENDFOR
OUTPUT "Highest score: ", highest
total = 0
for score in scores:
total += score
average = total / len(scores)
print(f"Average: {average}")
A linear search checks each element in turn until the target is found or the end of the array is reached:
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1 # Not found
A 2D array is an array of arrays — essentially a table with rows and columns.
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(grid[1][2]) # Output: 6 (row 1, column 2)
In pseudocode:
grid[1][2] # Accesses row 1, column 2
for row in range(len(grid)):
for col in range(len(grid[row])):
print(grid[row][col], end=" ")
print()
| Feature | Single Variable | Array |
|---|---|---|
| Stores | One value | Multiple values |
| Access | By name | By name and index |
| Use case | Single piece of data | Collection of related data |
| Efficiency | Fine for 1 value | Much better for many values |
range(len(arr)) goes from 0 to len(arr) - 1.append with assignment — arr[5] = "x" fails if index 5 doesn't exist; use append instead.temperatures = [12, 7, 15, -2, 9, 4, -5, 18]
lowest = temperatures[0]
for i in range(1, len(temperatures)):
if temperatures[i] < lowest:
lowest = temperatures[i]
print(f"Lowest temperature: {lowest}")
Trace:
i | temperatures[i] | lowest |
|---|---|---|
| 0 (init) | — | 12 |
| 1 | 7 | 7 |
| 2 | 15 | 7 |
| 3 | -2 | -2 |
| 4 | 9 | -2 |
| 5 | 4 | -2 |
| 6 | -5 | -5 |
| 7 | 18 | -5 |
Output: Lowest temperature: -5
SUBROUTINE linearSearch(arr, target)
FOR i ← 0 TO LEN(arr) - 1
IF arr[i] = target THEN
RETURN i
ENDIF
ENDFOR
RETURN -1
ENDSUBROUTINE
A linear search inspects each element once, giving a worst-case time complexity proportional to the length of the array. For sorted arrays, a binary search is faster — but binary search is covered in the Algorithms unit.
votes = ["Red", "Blue", "Red", "Green", "Red", "Blue"]
target = "Red"
count = 0
for vote in votes:
if vote == target:
count = count + 1
print(f"{target} received {count} votes")
Output: Red received 3 votes
numbers = [1, 2, 3, 4, 5]
reversed_numbers = []
for i in range(len(numbers) - 1, -1, -1):
reversed_numbers.append(numbers[i])
print(reversed_numbers)
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.