Goal

You’ll be able to write correct if/elif/else logic, and understand a specific structural mistake that causes conditions to be checked when they shouldn’t need to be.

Learn

Conditionals let a program make decisions, running different code depending on whether a condition is True or False:

age = 20

if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
else:
    print("Adult")

Python checks conditions top to bottom, and stops at the first one that’s True — the elif and else branches are never even evaluated once an earlier condition matches. This “stop at first match” behavior is genuinely important to understand, not just syntax to memorize.

Indentation is not optional styling in Python — it’s how Python knows which code belongs inside a block. Every line inside the if block must be indented consistently (4 spaces is the standard convention):

if age >= 18:
    print("You can vote.")
    print("You can also drive.")  # still inside the if block
print("This runs regardless.")  # NOT indented — always runs

Conditions can be combined using and, or, and not:

if age >= 18 and has_id:
    print("Entry allowed.")

Decision Task

Given age = 15, walk through this code before reading on: if age < 13: print("Child") elif age < 18: print("Teen") elif age < 20: print("Young adult") else: print("Adult") — what gets printed, and why don’t the later conditions matter once a match is found?

Show Answer

“Teen” gets printed. Python checks age < 13 first (False, since 15 is not less than 13), then age < 18 (True, since 15 is less than 18) — and stops there immediately. The remaining elif age < 20 and else branches are never evaluated at all once a match is found, even though 15 would technically also satisfy age < 20.

Common Mistake

Writing a chain of separate if statements instead of if/elif when only one branch should ever run, causing Python to needlessly check every single condition even after an earlier one already matched. Beyond being slightly less efficient, this can cause genuine bugs if multiple conditions could technically be true at once, since separate if statements (unlike elif) don’t stop after the first match — every one gets checked and potentially executed independently.

Practice Questions

1. Write an if/elif/else checking a variable score: print “Pass” if score >= 60, otherwise print “Fail”.

Show Answer

if score >= 60:\n print("Pass")\nelse:\n print("Fail")

2. Given temp = 25, what would this print? if temp > 30: print("Hot") elif temp > 20: print("Warm") elif temp > 10: print("Cool")

Show Answer

“Warm” — temp > 30 is False, temp > 20 is True (25 > 20), so Python prints “Warm” and stops checking further conditions.

3. Write a condition using and that checks whether a variable age is between 13 and 19 inclusive (a teenager).

Show Answer

if age >= 13 and age <= 19: (or equivalently if 13 <= age <= 19:, a valid Python chained comparison)

4. True or False: once an elif condition matches and its block runs, Python still checks any remaining elif or else conditions afterward.

Show Answer

False — Python stops checking entirely once the first True condition is found; remaining elif/else branches are skipped.

5. Why does inconsistent indentation cause an error in Python, unlike in many other languages?

Show Answer

Because indentation is how Python determines which lines belong inside a code block — it’s not just visual styling, it’s structurally meaningful syntax that Python actively parses.

Try It Yourself

Without looking back, write code checking a variable grade (0-100): print “A” for 90+, “B” for 80-89, “C” for 70-79, and “F” otherwise, using proper if/elif/else.

Show Answer

if grade >= 90:\n print("A")\nelif grade >= 80:\n print("B")\nelif grade >= 70:\n print("C")\nelse:\n print("F") — note the conditions only need >= their own threshold, since elif already guarantees the grade didn’t meet any higher threshold checked earlier.

Quick Check

1. What happens once Python finds the first True condition in an if/elif chain?

Show Answer

It runs that block and stops checking any remaining elif/else conditions entirely.

2. Why does indentation matter structurally in Python, not just visually?

Show Answer

It determines which lines of code belong inside a given block, like an if statement’s body.

3. What does the and operator require for a combined condition to be True?

Show Answer

Both individual conditions must be True.

4. What real problem can separate if statements (instead of elif) cause when multiple conditions could be true at once?

Show Answer

Every condition gets checked and potentially executed independently, rather than stopping after the first match, which can cause unintended multiple blocks running.

5. How many spaces is the standard Python indentation convention?

Show Answer

4 spaces.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.