1.2.2 - Variables, constants and data structures
A single name can keep track of one value, while a data structure can organise a whole collection without a tangle of separate names. In this lesson, you will follow and write short algorithms that use variables, constants, strings, records, and one- and two-dimensional arrays precisely.
Changing and Fixed Values
An algorithm often needs to remember what is true now. A booking algorithm might need
the number of visitors waiting; a game might need the current player name. Giving each value
a meaningful name lets later instructions refer to it.
Variable
A variable is a named storage location used to hold a value that may change while an algorithm is
followed.
An assignment sets or changes the value held under a name. In Python, the value on the
right of = is assigned to the name on the left. Here, = means "assign" rather than
"is permanently equal to". A variable may change; it does not have to change in every
algorithm.
Some values are rules or settings that should remain fixed throughout one use of an
algorithm.
Constant
A constant is a named value that is intended not to change while an algorithm is followed.
Pearson's Python notation conventionally uses uppercase names for constants, such as
MAX_TEAM_SIZE. Python does not make an uppercase name read-only: the capital letters
communicate the programmer's intention. A constant gives one clear name to a fixed value,
helps keep repeated uses consistent, and gives one place to update the setting before a
future use of the algorithm.
Worked example - follow the names
The numbers on the left are line labels, not part of the Python code.
1 MAX_TEAM_SIZE = 5
2 players_waiting = 2
3 players_waiting = 4
4 display_limit = MAX_TEAM_SIZE
Follow assignments from top to bottom:
| After line | MAX_TEAM_SIZE | players_waiting | display_limit |
|---|---|---|---|
| 1 | 5 | not yet assigned | not yet assigned |
| 2 | 5 | 2 | not yet assigned |
| 3 | 5 | 4 | not yet assigned |
| 4 | 5 | 4 | 5 |
Line 3 reassigns players_waiting, so its earlier value 2 is replaced. Line 4 reads
the constant's value and assigns a copy of that value to display_limit; it does not change
MAX_TEAM_SIZE.
Use a variable for changing state and a constant for a value that should remain fixed. In
Python, uppercase signals constant intent but does not enforce it.
Strings as Character Sequences
Storing a word as separate variables such as letter_1, letter_2, and letter_3 would
make it awkward to keep the characters together. A string stores the complete text under one
name while still allowing an algorithm to select a character by its position.
String
A string is an ordered sequence of characters, such as letters, digits, punctuation, or
spaces.
A string is a one-dimensional structure. Pearson's PLS sequences start at index 0, so the
index is one less than the everyday position number:
| Character | N | O | V | A |
|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 |
| Position | 1st | 2nd | 3rd | 4th |
Worked example - read characters by index
1 badge = "NOVA"
2 first_character = badge[0]
3 final_character = badge[3]
At line 2, badge[0] selects "N", so first_character holds "N". At line 3,
badge[3] selects "A", so final_character holds "A". The square brackets select
one item; they are not part of the string itself.
Python strings can be read by index, but an individual character cannot be replaced with an
assignment such as badge[0] = "S". If different text is needed, assign a new whole string
value to the variable. This keeps string use distinct from updating an array element.
Count string indices from zero: the third character is at index 2, not index 3.
One-Dimensional Arrays
Suppose an algorithm stores four level numbers. Four separate names would make the values
harder to handle as one collection. An array gives the related values one structure name and
uses an index to identify each item.
One-dimensional array
A one-dimensional array is an ordered sequence of items of the same data type, accessed
using one index.
Pearson's PLS represents an array with a Python list. The word homogeneous means that
the array items have the same data type and the same role: for example, all four items below
are integer level numbers. The Python list notation is the representation; the computer
science idea being modelled is an array.
Worked example - select and update items
1 levels = [2, 4, 3, 1]
2 selected_level = levels[1]
3 levels[2] = 5
4 final_level = levels[2]
- Line 2 uses index
1, the second position, soselected_levelbecomes4. - Line 3 replaces the item at index
2, solevelsbecomes[2, 4, 5, 1]. - Line 4 then reads the updated item, so
final_levelbecomes5.
The variable levels refers to the whole array. levels[2] refers to just one element in
that array. Confusing the array name with one indexed element changes the meaning of an
instruction.
Use a one-dimensional array when several ordered items have the same type and role. One pair
of square brackets selects one element.
Records and Fields
An array works well when every item has the same role. Information about one entity is
different: a library book might have a shelf code, a title, and an availability status. These
values belong together but do not all have the same type or meaning.
Record
A record is a collection of fields about one entity, usually containing values of different
data types.
Pearson's PLS represents a record with a Python list. The field order must be known and
used consistently. For this book record, the design is:
| Index | Field | Example value | Kind of data |
|---|---|---|---|
| 0 | shelf code | "R4" | text |
| 1 | title | "Orbit" | text |
| 2 | available | True | Boolean |
Worked example - follow a record update
1 book = ["R4", "Orbit", True]
2 selected_title = book[1]
3 book[2] = False
Line 2 reads the title field, so selected_title holds "Orbit". Line 3 updates the
availability field, producing the record ["R4", "Orbit", False]. The shelf code and title
remain unchanged because the assignment targets only field index 2.
Square brackets alone do not tell you whether a list represents an array or a record. Ask
what the items mean:
- Array: same-type items with the same role, such as four temperatures.
- Record: different fields about one entity, such as one book's code, title, and status.
A record keeps the different attributes of one entity together. Its field positions have
fixed meanings defined by the algorithm's design.
Two-Dimensional Structures
Some information naturally has two positions. A seating plan has a row and a column; a list
of records has a record number and a field number. A two-dimensional structure keeps this
table-like relationship instead of flattening every value into unrelated names.
Two-dimensional structure
A two-dimensional structure is a sequence of sequences in which an item is accessed with
two indices: the first selects an outer row or record, and the second selects an item or
field inside it.
In Python list notation, structure[1][2] means:
- select outer item/row at index
1; - from that selected row, select inner item/column at index
2.
This is row first, then column. Pearson's PLS supports up to two dimensions and expects a
rectangular structure: every inner row, or every record in a list of records, has the same
number of items or fields.
Worked example 1 - a homogeneous two-dimensional array
1 seats = [["free", "taken", "free"],
2 ["taken", "free", "free"]]
3 selected_status = seats[0][1]
4 seats[1][2] = "taken"
Each item is a text seat status, so this is a two-dimensional array. Line 3 selects row 0,
column 1, giving "taken". Line 4 changes row 1, column 2, so the structure becomes:
[["free", "taken", "free"],
["taken", "free", "taken"]]
Worked example 2 - a two-dimensional list of records
1 devices = [["D12", True],
2 ["D18", False]]
3 first_status = devices[0][1]
4 devices[1][1] = True
The outer structure contains two device records. Within each record, field 0 is the asset
code and field 1 is the ready status. Line 3 selects the first record and then its status,
so first_status holds True. Line 4 changes the second device's status; both records still
have the same two-field layout.
Use the information's shape and meaning to choose a representation:
| Information to store | Suitable representation | Reason |
|---|---|---|
| one current value | variable | one named value may change |
| one fixed setting | constant | the named value should stay unchanged |
| ordered characters | string | text is a character sequence |
| ordered same-type values | one-dimensional array | one index selects one item |
| different fields about one entity | record | field positions describe that entity |
| same-type values in rows and columns | two-dimensional array | two indices select a grid item |
| several entities with the same field layout | two-dimensional list of records | first index selects the entity; second selects its field |
One index selects an item from a one-dimensional structure. Two indices select an outer
row/record first and then an inner column/field.