6.2.2 – Sequence and selection

6.2.2 – Sequence and selection

Follow ordered statements and choose the right path with if, elif and else. Use indentation, mutually exclusive alternatives and nested decisions to make every input follow its intended route.

Sequence and data dependencies

Sequence means instructions on the chosen path execute in a defined order. Later statements can depend on values produced earlier. Reading a variable before it has a value does not make Python search ahead for its assignment.

Python
message = "Start"
print(message)
message = "Finish"
print(message)
Output
Start
Finish

The first print sees the first value; reassignment happens before the second print. Moving the assignment changes the result. Sequence does not mean every statement in the entire program must run: selection can skip a block, while repetition can revisit one.

An input-processing-output sequence first obtains data, then calculates, then displays the result. For example:

Python
price = float(input("Price: "))
quantity = int(input("Quantity: "))
cost = price * quantity
print("Cost:", cost)

The calculation needs both inputs, and the output needs cost. Those data dependencies determine a valid order even if some independent input statements could be swapped.

Selection and indentation

Selection chooses a path using a Boolean condition. A simple if executes an indented block only when its condition is True. A colon ends the header; indentation identifies the controlled statements.

Python
ready = True
if ready:
    print("Check complete")
    print("Begin")
print("End")

Both indented lines belong to the if block. With ready True, all three messages print. With ready False, only End prints because the final statement is outside the block. Four spaces per indentation level is a clear convention; consistent indentation determines meaning.

Add else for the alternative when the condition is False. Else has no separate condition:

Python
age = 14
if age >= 14:
    message = "Entry allowed"
else:
    message = "Wait"
print(message)

Both paths assign message, so the final print has a value whichever path is selected. If the else assignment were absent, an age below 14 would leave message unassigned. Think through every path, not only the example input.

A structured selection has one entry at its decision and rejoins the surrounding sequence after its branches. Control does not jump into an arbitrary statement in the middle of an indented block.

One exclusive selection chain

Use elif for additional alternatives in the same chain. Python tests from the top, takes the first True branch, and skips the remaining alternatives. If none is True, else handles the remaining case.

Python
score = 67
if score >= 70:
    result = "Distinction"
elif score >= 50:
    result = "Pass"
else:
    result = "Retry"
print(result)
ScoreFirst testNext testResult
75TrueNot testedDistinction
67FalseTruePass
42FalseFalseRetry

At score 70 the first branch applies. At 50 the first is False and the second True. Order matters: testing at least 50 first would absorb scores above 70 unless its interval were narrowed.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account