Conditionals
Intuition
Programs make decisions: if the score is at least 90, the grade is A; otherwise, if it is at least 80, B; and so on. Python checks the conditions from top to bottom and runs the first block whose condition is true.
Precise explanation
if score < 0 or score > 100:
print("Invalid score")
elif score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("F")
- Comparisons produce bools:
==equal,!=not equal,<,<=,>,>=. andis true only if both sides are;orif at least one is;notflips a bool.- Chained comparison:
0 <= score <= 100means0 <= score and score <= 100. - The indented lines (4 spaces) form the block.
elifandelseare optional; at most one branch runs. =assigns,==compares.if score = 90:is aSyntaxError.
Worked example
score = 85: 85 < 0 or 85 > 100 is False → 85 >= 90 False → 85 >= 80 True → prints B;
the remaining branches are skipped.
Common mistakes
- Wrong order: testing
score >= 60beforescore >= 90gives every passing score a D. - Writing
if 0 <= score and <= 100— each side ofandmust be a full comparison. - Forgetting the colon after the condition, or mixing tabs and spaces.
Why this matters
Thresholds turn model scores into decisions (spam / not spam), and branches handle edge cases such as empty data, which you will meet in every later topic.
References
- The Python Tutorial: More Control Flow Tools — Python Software Foundation (
S042) - Built-in Types — Python Software Foundation (
S049)