08-sequence.py
python-programming / programming-constructs  ·  Lesson 09
Lesson 09 — Programming constructs

Sequence

Follow instructions in the order they are written.

Sequence means carrying out instructions one after another in order. The result of an earlier statement can affect every statement that follows it.

Predict

Read the code first. Do not run it yet.

sequence-predict.py
x = 4
y = x + 3
x = y * 2
print(x, y)
Predict before running
  • Trace the values after each line.
  • What are the final values of x and y?
  • Why does changing the order of the assignments change the result?

Investigate

Use this code to explain exactly how the program works.

sequence-investigate.py
length = 8
width = 5
area = length * width
print("Area:", area)
Investigate the code
  • Identify the input values already stored in variables.
  • Identify the processing statement.
  • Identify the output statement.

Input, process, output in sequence

A simple program often follows an input → process → output sequence.

rectangle.py
length = 8
width = 5
area = length * width
print("Area:", area)
Output
Area: 40
How it works
  • The first two statements establish the values to use.
  • The third statement processes those values.
  • The final statement outputs the result.
Order mattersIn a sequence, each line is completed before the next line begins.

Tracing a program

A trace table records how variable values change as an algorithm runs. Work through the statements in order and update the table whenever a tracked value changes.

trace.py
total = 0
for number in range(1, 4):
    total = total + number
print(total)
numbertotal after the assignment
11
23
36
Trace carefullyA trace table follows the actual order of execution. Only change a value in the table when the corresponding assignment has executed.

Self-study guide

Sequence means instructions are carried out in order, one after another. This sounds simple, but many programming errors come from using a value before it has been created or calculating things in the wrong order.

A useful way to think about sequence is data flow: input values are obtained first, calculations produce intermediate values next, and results are output only after the required values exist.

OCR focusSequence is one of the three basic programming constructs. You should be able to trace a sequence line by line and explain why changing the order can change the result or cause an error.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
Sequencetop-to-bottom statementsThere is no special OCR or Python keyword for sequence.
x = 4 y = x + 3x = 4 y = x + 3The assignment form is the same.
Input → Process → Outputinput() → calculations → print()A common sequence structure.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Every statement here depends on values created earlier.

lesson-09-worked.py
price = 12.50
quantity = 3
subtotal = price * quantity
delivery = 2.00
total = subtotal + delivery

print(subtotal)
print(total)
Output
37.5
39.5
Work through it line by line
  • price and quantity must exist before subtotal can be calculated.
  • subtotal must exist before it is used in the final total.
  • The outputs are last because they depend on the completed calculations.
  • Moving total = subtotal + delivery above the creation of subtotal would make the program invalid.

Another example

Tracing sequence means recording changes at exactly the line where they happen.

lesson-09-example-2.py
x = 4
y = x + 3
x = y * 2
print(x, y)
Output
14 7
Why this example matters
  • After line 1: x = 4.
  • After line 2: y = 7 while x is still 4.
  • After line 3: x changes to 14; y stays 7.

Common mistakes

Avoid these errors
  • Using a variable before it has been assigned.
  • Mentally applying all assignments at once instead of one line at a time.
  • Assuming a later change to x automatically changes a value already stored in y.
Exam-safe warningWhen tracing, update only the variable changed by the current instruction. Previously calculated values do not automatically recalculate.

Exam-ready checklist

You should now be able to
  • Identify sequence in an algorithm.
  • Trace assignments in order.
  • Rearrange statements into a valid sequence.
  • Explain why one calculation must happen before another.

What you have learned

  • Sequence runs instructions in order.
  • Earlier assignments can affect later calculations.
  • Simple programs often follow input, process, output.