24 total
By the end of this topic, you should be able to:
An array is a special type of variable that can store multiple values of the same type in one place. Think of it like a row of boxes, where each box can hold one piece of data.
Key features of arrays:
Why use arrays?
Instead of creating separate variables like:
score1 = 85
score2 = 90
score3 = 78
score4 = 92
score5 = 88
You can use one array to store all five scores together:
scores = [85, 90, 78, 92, 88]
This makes your code much simpler and easier to manage, especially when you have lots of values to store.
A one-dimensional array (also called a 1D array) is a simple list of values arranged in a single line, like beads on a string.
Imagine an array called letters containing five characters:
Index: 0 1 2 3 4
+---+ +---+ +---+ +---+ +---+
Array: | B | | E | | A | | D | | S |
+---+ +---+ +---+ +---+ +---+
↑ ↑
First element Fifth element
Index means the position number of an element in the array.
There are two ways to count positions in an array:
Zero-indexed (starts at 0) — Most common in programming languages like Python
One-indexed (starts at 1) — Sometimes used in pseudocode
Important: In your exam, the question will tell you whether the array is zero-indexed or one-indexed. Always check this before answering!
In Pseudocode:
DECLARE scores: ARRAY[0:4] OF INTEGER
This means: "Create an array called scores that can hold 5 integers (from index 0 to index 4)."
You can also create an array and fill it with values at the same time:
scores ← [12, 10, 5, 2, 8]
In Python:
scores = [] # Creates an empty array
Or create it with values:
scores = [12, 10, 5, 2, 8] # Creates an array with 5 values
To read a value from an array, use the array name followed by the index in square brackets.
In Pseudocode:
OUTPUT scores[0] // Outputs the first element (12)
OUTPUT scores[3] // Outputs the fourth element (2)
In Python:
print(scores[0]) # Outputs: 12
print(scores[3]) # Outputs: 2
To change a value in an array, use the assignment operator.
In Pseudocode:
colours[4] ← "Red" // Assigns "Red" to index 4 (5th element)
In Python:
colours[4] = "Red" # Assigns "Red" to index 4
Example:
scores = [12, 10, 5, 2, 8]
scores[1] = 15 # Changes the second element from 10 to 15
print(scores) # Outputs: [12, 15, 5, 2, 8]
You can use a variable to specify which element to access. This is very useful when working with loops.
Example:
scores = [12, 10, 5, 2, 8]
position = 2
print(scores[position]) # Outputs: 5 (the element at index 2)
You can also do calculations with the index:
i = 1
print(scores[i + 1]) # Outputs: 5 (element at index 2)
Iteration means going through each element in the array one by one. We use loops to do this.
Method 1: Using a FOR loop with the array directly
In Python:
scores = [12, 10, 5, 2, 8]
for score in scores:
print(score)
Output:
12
10
5
2
8
This loop automatically goes through each element in the array.
Method 2: Using a FOR loop with index numbers
In Pseudocode:
FOR i ← 0 TO 4
OUTPUT scores[i]
NEXT i
In Python:
scores = [12, 10, 5, 2, 8]
for i in range(5): # range(5) gives us 0, 1, 2, 3, 4
print(scores[i])
This method is useful when you need to know the position (index) of each element.
Finding the length of an array:
In Python:
length = len(scores) # Returns 5
You can use this in a loop:
for i in range(len(scores)):
print(scores[i])
This is better because it automatically adjusts if the array size changes.
Sign in to view full notes