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 0, known as zero indexed
Concept | Pseudocode | Python |
Create | array scores[5] |
scores = [] |
Creates a blank array with 5 elements (0-4) | Creates a blank array | |
array scores ← [12, 10, 5, 2, 8] |
scores = [12, 10, 5, 2, 8] |
|
Creates an array called scores with values assigned | ||
Assignment | colours[4] ← "Red" |
colours[4] = "Red" |
Assigns the colour "Red" to index 4 (5th element) |
Example in Python
Creating a one-dimensional array called ‘array’ which contains 5 integers.
- Create the array with the following syntax:
array = [1, 2, 3, 4, 5]
- 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 |
|