Making decisions — if, elif, else
A big apple with scab on it is not an A. One rule, two conditions — and both must be true.
- Ask True/False questions in Python
- Choose one branch with if, elif, else
- Join two conditions with and
Making decisions — if, elif, else
A big apple with scab on it is not an A. One rule, two conditions — and both must be true.
- Ask True/False questions in Python
- Choose one branch with if, elif, else
- Join two conditions with and
Story
IF in it. Both worked.Learn it
A program becomes useful when it can decide. A condition is a question Python answers True or
False, and if / elif / else choose one path based on it. You have written this logic before,
as a decision tree in Class 6 and an IF formula in Class 7; now you write it in Python, where
indentation decides which lines belong to which choice, and and lets one rule need two things
at once.
Words to know
a question with a True or False answer, like size > 8.
> < >= <= == (equal to) != (not equal to).
= stores a value; == compares two values. Mixing them up is the commonest mistake in Python.
if asks the first question; elif ("else if") asks the next; else catches everything left. Only the first true branch runs.
the four spaces that tell Python which lines are inside a choice.
and needs both sides true; or needs one; not flips True and False.
a value right on the border, like exactly 8 when the rule says > 8.
Step by step: grading exam marks
marks = int(input("Marks out of 100: "))
present = input("Attended the practical? y or n: ")
if marks >= 75 and present == "y":
print("Distinction")
elif marks >= 40:
print("Pass")
else:
print("Try again")
- 82 and
y: both sides ofandare true → Distinction. The other branches are skipped. - 82 and
n: theandfails, so Python checkselif marks >= 40→ Pass. - 30: neither is true →
else→ Try again. - Edge case, exactly 75 and
y:>=includes 75 → Distinction. With>it would not, so choose carefully. - Two errors to expect: a missing
:givesSyntaxError: expected ':'; missing spaces giveIndentationError.
Careful: indentation is not style in Python; it is grammar. The indented lines are the ones
that belong to the choice above them. And text comparisons are exact: "N" is not "n", so
tell the user which letter to type.
Your missions
04-grader.pyTrue or False?
5 min- 1
Guess True or False for each first. Then type them into the shell. The last two catch most people.
8 > 6 8 >= 8 8 != 6 "Ambri" == "Ambri" "ambri" == "Ambri"=stores.==compares.
One question, three answers
7 min- 2
In the editor:
size = float(input("Apple size in cm: ")) if size > 8: print("A grade") elif size > 6: print("B grade") else: print("C grade") - 3
Predict the grade for 9, 7, 5 and 8. Run all four. Is 8 what the uncle would want? A value on the border is an edge case.
Only you can see this
Two conditions
4 min- 4
Add
spots = input("Any scab spots? y or n: ")at the top, and change the first test toif size > 8 and spots == "n":. Test 9 withy, then withn.
Break it
2 min- 5
Delete the
:afterif→SyntaxError: expected ':'. Put it back, delete the four spaces beforeprint("A grade")→IndentationError. Fix both and save.
Stuck?
Always C grade? Text is exact: type a lowercase n. IndentationError that won't go? Delete
the line's leading space and press the space bar four times. SyntaxError on the if line?
You wrote = for ==.
Add a Reject grade for under 4 cm, then make scab or under 4 cm send it to Reject, using
or. Test every outcome.
No laptop today?
Trace the two-condition grader for (9, "n"), (9, "y") and (5, "n"): which branch runs,
what prints? Then: why can a 9 cm apple with spots never reach else?
Debate it
Your program decides an apple's grade, and the grade decides what the farmer is paid.
The rule in the code — size > 8 and spots == "n" — was written by someone. Who should
decide where the line sits: the trader who buys, the grower who sells, or the person who
writes the program? What should happen to an apple at exactly 8 cm, and who should be
told that the rule works that way?
Check yourself · practice, not a test
In the app, students answer these one at a time and each answer is checked straight away. It's practice, never a mark — anything they miss comes back later for another go.
1.What does == do?
- APuts a value into a variable
- BAsks whether two values are equal
- CAdds two numbers
- DEnds a line
2.What does this print? python marks = 7 if marks > 8: print("High") elif marks > 5: print("Middle") else: print("Low")
- AHigh
- BMiddle
- CLow
- DMiddle and Low
3.Find the bug. python if size > 8 print("A grade")
- Aprint should be indented further
- BThe colon is missing after the condition
- Csize must be in quotes
- Dif should be elif
4.If the if condition is True, Python also checks the elif below it.
TrueFalse5.A and B is True only when ______ sides are True.
6.Match each operator to its meaning:
- !=
- >=
- ==
- not equal to
- equal to
- greater than or equal to
7.Put these lines in the order they must appear, so an apple is graded correctly:
- 1print("A grade")
- 2if size > 8:
- 3else:
- 4print("B grade")
- 5print("C grade")
- 6size = float(input("Apple size in cm: "))
- 7elif size > 6:
= stores, == compares. Colon at the end, four spaces underneath.
Typing practice
Today's symbols are the colon : and the comparison signs > < =.
:is Shift + ; — the key beside L. Everyifline in Python ends with it.==is two equals signs typed together, with no space between them.- typing.com: continue the symbols lessons.
- Then type these two lines twice, with no mistakes:
if size > 8:elif grade == "B":
The colon is the single most forgotten character in Python. Your fingers should learn it today.
- :
- >
- <
- =
- :
- if
- ==
- if size > 8:
- elif grade == "B":
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.