Using arrays
To store a dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions. item in an arrayA set of data values of the same type, stored in a sequence in a computer program. Also known as a list. , the elementAn individual component of an array. that the data will be stored in needs to be referenced.
For example, in the score array score(10), the following would store the score 3000 in the first element:
score[0] = 3000
The following would store the score 2500 in the second element:
score[1] = 2500
Computers start counting at 0, so the first element is [0]
, the second is [1]
, etc.
To access the data stored in an element, the element must be referred to by its number. For example:
print(score[0])
total = score[0] + score[1]
Why use arrays?
A variableA memory location within a computer program where values are stored. holds a single item of data. There may be a situation where lots of variables are needed to hold similar and related data. In this situation, using an array can simplify a program by storing all related data under one name. This means that a program can be written to search through an array of data much more quickly than having to write a new line of code for every variable. This reduces the complexity and length of the program which makes it easier to find and debug errors.
Lists
Lists are data structureThe way that data is stored in a database or program. similar to arrays that allow data of more than one data typeThe format in which a variable or constant holds data, such as ‘integer’ or ‘string’..
Some languages, such as BASICA group of general-purpose, high-level programming languages that are relatively easy to use. BASIC stands for Beginner's All-purpose Symbolic Instruction Code. and JavaA popular high-level computer programming language. allow the use of arrays. Others, such as PythonA high-level programming language., only allow lists.