6.2.1 – Identify program components
Identify every main program component in context, distinguish definitions from calls, and use complete block boundaries to explain how a program is organised.
Values and assignments
A program needs names for the values it works with and commands that set or change those values.
Variable
A variable is an identifier associated with a value that may change while a program is running.
Constant
A constant is a named value intended not to change while a program runs. Python does not enforce constants, so the Pearson PLS convention is to write constant identifiers in uppercase and not reassign them.
Look at this valid Python:
LIMIT = 6 # line 1
total = 0 # line 2
total = total + 2 # line 3
attempts = 1 # line 4
Line 2 initialises total: it is the first assignment that gives the variable a usable starting value. Line 3 is a later assignment. Python first evaluates the expression on the right, using the old value of total, and then stores the result using the identifier on the left.
Initialisation is therefore a kind of assignment, but not every assignment is an initialisation. The symbol = performs assignment; it does not ask whether two values are equal. Python uses == for an equality comparison.
The four lines also form a command sequence: instructions are encountered in order from top to bottom. A sequence inside a selection, loop or subprogram still follows its local order, even though another construct may skip or repeat the whole block.
To identify initialisation, find the first assignment that establishes a variable's starting value. To identify a later assignment, find where a value is replaced or updated.
Sequence and selection
Sequence gives a program an order. Selection changes which sequence is followed: a condition is evaluated, and one path is selected.
Selection
Selection uses a condition to choose which command or block of commands executes. In PLS-safe Python, it is shown by if, optional elif branches and an optional else branch.
ticket = 4 # line 1
ticket = ticket + 1 # line 2
if ticket >= 5: # line 3
message = "Next level" # line 4
else: # line 5
message = "Keep going" # line 6
print(message) # line 7
Trace the sequence statically. Line 1 makes ticket equal to 4; line 2 updates it to 5. The condition at line 3 is then true, so line 4 executes and line 6 is skipped. After the selection finishes, line 7 is next, giving the predicted output Next level.
The indented commands show the alternatives belonging to the selection. A common mistake is to call only the if header selection; the structural component is the whole decision, lines 3-6 here.
Loops and data structures
A data structure groups related items so a program can treat them as one organised value. In the PLS, a string is a sequence of characters, while Python lists are used to represent arrays and records.
| Python value | PLS role | How to recognise the role |
|---|---|---|
"Final" | string | A sequence of characters |
[4, 7, 6] | array | A list used for items of the same data type |
["Noor", 7] | record | A list used for fields that may have different data types |
Square brackets show that both the array and record use a Python list. Their contents and purpose distinguish the role; not every list should automatically be called an array.
Loops make a command sequence happen more than once. Pearson's PLS gives two useful labels for inspection:
- Condition-controlled repetition: a
whileloop executes its block while a condition is true. - Count-controlled repetition: a
forloop withrangerepeats for a planned sequence of counts. - Iteration over data: a
forloop visits each item of a structure directly.
All are loops. The labels distinguish what controls the next pass; for alone does not prove that the purpose is visiting a stored data structure.
scores = [2, 4, 1] # line 1
index = 0 # line 2
while index < 2: # line 3
print(scores[index]) # line 4
index = index + 1 # line 5
for score in scores: # line 6
print(score) # line 7
The while block is repetition. It prints only the items at index 0 and index 1, so its predicted output is 2 then 4. The for block is iteration over the data structure and visits all three items, so it then produces 2, 4, 1.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account