1.2.2 Variables, constants and data structures

Variables and Constants

A variable is a named location in memory that stores a value which can change during program execution. A constant is a named location whose value is set once and does not change.

VariableConstant
Value changes?Yes — can be updated at any timeNo — set once, never altered
PLS conventionlowercase or camelCase: score, playerNameALL_UPPERCASE: MAX_SCORE, TAX_RATE
Typical useAccumulating totals, loop counters, user inputPi, tax rates, maximum array size, fixed thresholds
MAX_LIVES = 3         # constant — value never changes
TAX_RATE = 0.2        # constant

score = 0             # variable — starts at 0
lives = MAX_LIVES     # variable — initialised from constant

score = score + 10    # variable updated during play
lives = lives - 1     # variable updated when player dies

Using constants instead of "magic numbers" (unexplained numeric literals scattered through code) makes programs easier to understand and maintain. If the tax rate changes, you update one constant, not every calculation.

Data Structures

A data structure stores multiple values in an organised way. The PLS supports three structured data types, all implemented using Python's list type.

A string is a sequence of characters. It is indexed from zero. Strings are immutable in Python — you cannot change an individual character; you create a new string.

name = "Python"
print(name[0])      # P  (index 0)
print(name[3])      # h  (index 3)
print(name[0:3])    # Pyt  (slice: indices 0, 1, 2)
print(len(name))    # 6
Remember: the first character is at index 0, not 1. The last character is at index len(name) - 1.

A 1D array (list in Python) stores a sequence of items of the same type (homogeneous), indexed from zero.

scores = [85, 72, 91, 68, 77]

print(scores[0])    # 85  (first element)
print(scores[4])    # 77  (last element, index 4)

scores[2] = 95      # update element at index 2
print(scores)       # [85, 72, 95, 68, 77]

scores.append(80)   # add element to end
print(len(scores))  # 6

A 2D array is a list of lists — useful for grids, tables or matrices. The first index selects the row; the second selects the column.

grid = [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]

print(grid[0][0])   # 1  (row 0, col 0)
print(grid[1][2])   # 6  (row 1, col 2)
print(grid[2][1])   # 8  (row 2, col 1)

# Access all elements using nested loops
for row in grid:
    for item in row:
        print(item, end=" ")

A record is a list of items usually of mixed (heterogeneous) types — for example, a student's name, age and score stored together. In the PLS, records are implemented as lists.

# A single record: [name, age, score]
student = ["Alice", 16, 88]

print(student[0])   # Alice
print(student[1])   # 16
print(student[2])   # 88

# A list of records (2D structure)
students = [
    ["Alice", 16, 88],
    ["Bob",   17, 75],
    ["Carol", 16, 92]
]

print(students[1][0])   # Bob
print(students[2][2])   # 92

 Key Takeaways

  • Variables store values that change; constants store values that do not — named in ALL_UPPERCASE.
  • All sequences are zero-indexed — the first element is at index 0.
  • Strings = sequence of characters; 1D array = homogeneous list; 2D array = list of lists; record = heterogeneous list.
  • In the PLS, arrays and records are both implemented as Python list.
  • Slice syntax [start:stop] extracts a sub-sequence — stop index is excluded.