Python B02 · Lesson 3 of 5

Conditional Statements

if · if-else · if-elif-else · Nested Conditions

Conditional statements let your program make decisions — execute one block of code or another based on whether a condition is True or False. This is where Python starts thinking!

1. The if Statement

Executes a code block only if the condition is True. If the condition is False, the block is skipped entirely.

if condition:
    code block    # runs only when condition is True
Indentation matters!
Python uses indentation (4 spaces or 1 tab) to define code blocks. The code inside if must be indented. If you don't indent, Python will give an IndentationError.
marks = 80

if marks >= 50:
    print("You passed!")

print("Program continues...")   # always runs

From your file (control-flow.py), notice the difference between these two code sets:

# Code set 1 — if without else:
if False:
    print("Statement 1")   # skipped

print("Statement 2")       # always runs — outside if block

# Code set 2 — if with else:
if False:
    print("Statement 1")   # skipped
else:
    print("Statement 2")   # runs because if was False

2. The if-else Statement

Runs one block when the condition is True, and a different block when it's False. One of them always runs.

if condition:
    code block    # runs if True
else:
    code block    # runs if False
age = int(input("Enter your age: "))

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

3. The if-elif-else Statement

Checks multiple conditions in sequence. The first condition that is True wins — its block runs, and the rest are skipped. The else at the end is the fallback when no condition is True.

if condition_1:
    code block
elif condition_2:
    code block
else:
    code block
Remember
elif is short for "else if". You can have as many elif blocks as you need. Only the first matching block runs.
marks = int(input("Enter marks: "))

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
elif marks >= 50:
    print("Grade: D")
else:
    print("Grade: F — Failed")

From your file example:

if True:
    print("Statement 1")   # runs — first True condition
elif True:
    print("Statement 2")   # skipped — first block already ran
else:
    print("Statement 3")   # skipped

4. Nested Conditions

A condition inside another condition. Use this when you need to check a second condition only after the first one passes.

if condition_1:
    if condition_2:       # only checked if condition_1 is True
        code block
    else:
        code block
else:
    code block
# From control-flow.py teacher example:
if True:
    if True:
        print("Statement 1")   # runs
    else:
        print("Statement 2")
else:
    print("Statement 3")

Real-world example:

age = int(input("Enter age: "))
has_ticket = input("Have a ticket? (yes/no): ")

if age >= 18:
    if has_ticket == "yes":
        print("Welcome! Enjoy the event.")
    else:
        print("Adult, but no ticket.")
else:
    print("You must be 18+ to enter.")
Tip
Deeply nested if statements (3+ levels) become hard to read. Often you can simplify using and in a single condition.

🛠 Drills — Practice Time

Drill 1 Pass/Fail Checker
Ask the user for their marks (0–100). Print "Pass" if marks ≥ 50, otherwise print "Fail".
marks = int(input("Enter your marks: "))

if marks >= 50:
    print("Pass")
else:
    print("Fail")
Drill 2 Number Classifier
Ask the user for a number. Print "Positive", "Negative", or "Zero" using if-elif-else.
num = int(input("Enter a number: "))

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")
Drill 3 Grade System
Build a grade calculator: A (90+), B (75–89), C (60–74), D (50–59), F (below 50). Use if-elif-else.
marks = int(input("Enter marks (0-100): "))

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
elif marks >= 50:
    print("Grade: D")
else:
    print("Grade: F")
Drill 4 Nested Eligibility Check
A user can vote if they are (1) 18 or older AND (2) have a voter ID. Ask for both. Use nested if to print the right message.
age = int(input("Enter your age: "))
has_voter_id = input("Do you have a voter ID? (yes/no): ")

if age >= 18:
    if has_voter_id == "yes":
        print("You can vote!")
    else:
        print("You need a voter ID to vote.")
else:
    print("You must be 18+ to vote.")
Drill 5 Predict the Output
Predict what each code block prints. Don't run it — work it out logically first.
x = 15

if x > 20:
    print("A")
elif x > 10:
    print("B")
elif x > 5:
    print("C")
else:
    print("D")
# Output: B
# Why: x=15 → 15>20? No. → 15>10? YES → print "B" → stop.
# Even though 15>5 is also True, elif stops at first match.

📝 Quiz — 7 Questions

Score: 0 / 7
1 How does Python define the code block inside an if statement?
2 In an if-elif-else chain, how many blocks can run?
3 What prints? if False: print("A") \n print("B") (B is not indented)
4 What does elif stand for?
5 What prints? if True: print("1") elif True: print("2") else: print("3")
6 When is a nested if (inside another if) checked?
7 In an if-else, when condition is False, what happens?
← Lesson 2: Operators Next: Lesson 4 — Loops →