Simple 1-D Arrays
1-D Arrays are a vital part of the National 5 course. They allow multiple values of the same data type within a single variable. All the values in an array are stored in a list, and each value has its own position. This position is called an index.
Why use a 1-D Array?
A 1-D array allows for multiple values to be stored in a single variable. The variable and the position of the value can be accessed to retrieve the data for use.
Instead of creating multiple values like the following:
name1 = "John" name2 = "Mary" name3 = "Betty" name4 = "David" name5 = "Sam"A single array can be used that stores each value.
names = ["John", "Mary", "Betty", "David", "Sam"]
Using the Index
The position of an array starts at 0. Each position after it increases by 1.
Using the following array as an example.
#Position 0 1 2 3 4 names = ["John", "Mary", "Betty", "David", "Sam"] print(names[0]) # Prints "John" print(names[1]) # Prints "Mary" print(names[2]) # Prints "Betty" print(names[3]) # Prints "David" print(names[4]) # Prints "Sam"