teach-ict.com logo

THE education site for computer science and ICT

2. One dimensional array

An alternative to storing related data as separate variables is to use an array.

The array is given a name and is associated with each of the entries in it. For example an array called 'football_team' is declared :

                 football_team = ("John Salisbury", "Harold Cooper", "James March", ... )

In this case it is the names of a football squad. It will have 11 items in it.

Each item has an index (position in the array). The first item (or element) is at position zero. Arrays usually begin with position zero, not one.

An array is a list of related data. Each piece of data within the array is an element. The position of each element within the array is its index. Arrays usually begin at index 0, not index 1.

In order to access an element of an array, you use its index

Like this

                football_team = ("John Salisbury", "Harold Cooper", "James March")
                print football_team[1]

This will print out 'Harold Cooper' because that is the element at index 1.

An array that can be accessed by a single index is called a one dimensional array.

Updating an array

You can also change the value of an element by using its index, like this

                   football_team = ("John Salisbury", "Harold Cooper", "James March")
                   football_team[2] = "Greg Jones"

For this array, this command will replace whatever was at index 2 with the specified input. So we are replacing "James March" with "Greg Jones".

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is a one dimensional array