Arrays
Arrays
Page 1 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Arrays
Your notes
1-Dimensional Arrays
What is an array?
An array is an ordered, static set of elements in a fixed-size memory location
An array can only store 1 data type
A 1D array is a linear array
Indexes start at generally start at 0, known as zero-indexed
Page 2 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Access the individual elements of the array by using the following syntax:
array[index]
Modify the individual elements by assigning new values to specific indexes using the following syntax:
array[index] = newValue
Use the len function to determine the length of the array by using the following syntax:
len(array)
In the example the array has been iterated through to output each element within the array. A for loop
has been used for this
Python
# Output:
#1
# 10
#3
#4
#5
Page 3 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
2-Dimensional Arrays
What is a 2-dimensional array?
A 2D array extends the concept on a 1D array by adding another dimension
A 2D array can be visualised as a table with rows and columns
When navigating through a 2D array you first have to go down the rows and then across the columns to
find a position within the array
Page 4 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
NamesAndNumbers[2, 1] ← "25"
NamesAndNumbers[3, 1] ← "Sarah"
NamesAndNumbers[3, 2] ← "12"
NamesAndNumbers[3, 3] ← "8"
Assigns the name "Holly" to index 0, 1 (1st row, 2nd column) - replaces "Paul"
Example in Python
Python
# Initialising a 2D array with 3 rows and 3 columns, with the specified values
array_2d = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Python
Page 5 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Worked Example
A parent records the length of time being spent watching TV by 4 children
Data for one week (Monday to Friday) is stored in a 2D array with the identifier minsWatched.
The following table shows the array
0 1 2 3
Monday 0 34 67 89 78
Tuesday 1 56 43 45 56
Wednesday 2 122 23 34 45
Thursday 3 13 109 23 90
Write a line of code to output the number of minutes that Lyla watched TV on Tuesday [1]
Write a line of code to output the number of minutes that Harry watched TV on Friday [1]
Write a line of code to output the number of minutes that Quinn watched TV on Wednesday [1]
Answers
Page 6 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
print(minsWatched[1,1] or print(minsWatched[1][1]
print(minsWatched[2,4] or print(minsWatched[2][4]
print(minsWatched[0,2] or print(minsWatched[0][2] Your notes
Page 7 of 7
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers