Engineering Python · Data & Control
Conditionals
In Engineering Python because labs branch on thresholds and flags — if/elif/else is how a script chooses a path.
`if` / `elif` / `else` run different blocks depending on boolean conditions. Only one branch's body runs for a chained if/elif/else.
- Engineering Python
- Easy level
- 4 concepts
1if and else
The `if` condition is any expression that yields True/False. Indentation defines the body.
Figure. The if condition is any expression that yields True/False. Indentation defines the body. temp is 85, so hot runs and ok does not.
Branch
temp = 85
if temp > 80:
print("hot")
else:
print("ok")What selects which branch runs?
- The boolean condition on the if/elif line
- The length of the file
- Random choice
Conditions decide the path.
2elif chains
`elif` adds another tested path. The first True condition wins; later ones are skipped. `else` catches the rest.
Order matters: put the most specific test first, or an earlier broad condition will steal the case.
Figure. elif adds another tested path. The first True condition wins. score is 72, so pass runs and A and fail are skipped.
elif
score = 72
if score >= 90:
print("A")
elif score >= 50:
print("pass")
else:
print("fail")In an if/elif/else chain, which body runs when two conditions are True?
- Only the first True branch
- Both True branches
- Only else
First match wins; later branches are skipped.
3Combining conditions
`and` / `or` / `not` combine booleans. Keep conditions readable; nest only when needed.
Figure. and / or / not combine booleans. if ok and n > 0 prints go.
Takeaway
- Idea`and` / `or` / `not` combine booleans. Keep conditions readable; nest only when needed.
and
ok = True
n = 3
if ok and n > 0:
print("go")In an if / elif / else chain in Python, when does the elif branch execute?
- When all preceding if/elif conditions evaluated to False and its own condition is True
- Always after the if branch completes execution
- Simultaneously with all other matching branches in parallel
- Only if the trailing else block is omitted from the chain
In an if/elif chain, conditions are checked in order. The first branch with a True condition executes and the rest are skipped.
4Lab: branch on a value
Classify a score and print the label the check expects.
Figure. If score >= 50 print pass, else print fail. With score=72 stdout contains pass.
Takeaway
- IdeaClassify a score and print the label the check expects.
Coding lab. Branch on a score runs in the app, with checks on your output.
Notes
- In Engineering Python because labs branch on thresholds and flags — if/elif/else is how a script chooses a path.
- The `if` condition is any expression that yields True/False. Indentation defines the body.
- `elif` adds another tested path. The first True condition wins; later ones are skipped. `else` catches the rest.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Conditions choose a path; indentation marks the body; first True elif wins.
- if and else
- The `if` condition is any expression that yields True/False. Indentation defines the body.
- elif chains
- `elif` adds another tested path. The first True condition wins; later ones are skipped. `else` catches the rest.
- Combining conditions
- `and` / `or` / `not` combine booleans. Keep conditions readable; nest only when needed.
- Lab: branch on a value
- Classify a score and print the label the check expects.
Practise Conditionals
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 3 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device