Without selection a program can only do exactly one thing. Selection lets it choose a path based on a condition — a test that evaluates to True or False. That single capability is what separates a program from a calculator, and it is the point at which programs start being able to be wrong in interesting ways.
| Python | What it does |
|---|---|
if mark >= 50: | Tests the condition; runs the indented block if True |
print("Pass") | The indented body — runs only when the condition holds |
elif mark >= 40: | Checked ONLY if every condition above was False |
print("Borderline") | Body of the elif |
else: | Runs when no condition above was True. No condition of its own |
print("Fail") | Body of the else |
if runs only when the condition is true; a line at the outer level always runs. Mixing tabs and spaces produces confusing errors, so let your editor insert four spaces.Comparison and logical operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
< > | Less / greater than | 3 > 7 | False |
<= >= | Less / greater than or equal | 5 >= 5 | True |
and | Both conditions must be True | x > 0 and x < 10 | True only inside the range |
or | At least one must be True | day == "Sat" or day == "Sun" | True on either day |
not | Reverses a condition | not (x > 5) | True when x ≤ 5 |
x < 0 or x > 100. Writing and there gives a condition that can never be true — no number is both below 0 and above 100. Read the condition aloud: "less than zero AND greater than a hundred" is obviously impossible, and hearing it is faster than debugging it.Order matters in an if-elif chain
Python checks the branches in order and stops at the first one that is True. That makes ordering part of the logic, not a style choice. Testing mark >= 50 before mark >= 80 means an 85 matches the first branch and never reaches the second — so every strong student is reported as a bare pass. Write the chain from the most specific condition to the least, which for numeric ranges means from the highest threshold down.
| Correct order | Why |
|---|---|
if mark >= 80: # Level 4 | Highest threshold first, so 85 is caught here |
elif mark >= 70: # Level 3 | Only reached when the mark is below 80 |
elif mark >= 60: # Level 2 | Only reached when below 70 |
elif mark >= 50: # Level 1 | Only reached when below 60 |
else: # Below 50 | Everything remaining |
if statements are all evaluated, so several branches can run. An if-elif chain runs at most one branch. When the cases are mutually exclusive — a mark is exactly one level — use elif: it is faster, and more importantly it makes the exclusivity explicit so a later reader cannot get it wrong.Boundaries are where bugs live
Almost every selection bug is an off-by-one at a boundary. > and >= differ on exactly one value, and that value is usually the one that matters — the pass mark, the maximum, the zero case. When you test a program, do not test the middle of each range. Test exactly on the boundary, one below it, and one above it. A test with a mark of 50, 49 and 51 finds bugs that a test with 75 never will.
- An if inside another if's body
- Use when the second test only makes sense if the first passed
if logged_in:thenif is_admin:- Gets unreadable past two or three levels
- One if with
and - Use when the tests are independent
if logged_in and is_admin:- Flatter and usually easier to read