Python Tutorial 0/52 lessons ~6 min read Lesson 9

    Conditionals (if / elif / else)

    Python conditionals use if, elif, else with colons and indented blocks.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Python conditionals use if, elif, else with colons and indented blocks. Python 3.10+ adds match-case for structural pattern matching.

    Understanding the topic

    Core concepts to understand:

    • elif chains avoid nested ifs.
    • Ternary: x if cond else y.
    • Truthy/falsy: empty collections, 0, None, '' are falsy.
    • match-case (3.10+) for pattern matching.

    Syntax reference

    Visual flow / code:

    python
    score = 85
    if score >= 90:
    grade = "A"
    elif score >= 80:
    grade = "B"
    else:
    grade = "C"
    # Ternary
    status = "pass" if score >= 60 else "fail"
    # match-case (3.10+)
    match status:
    case "pass": print("🎉")
    case "fail": print("😢")
    case _: print("?")

    Execution workflow

    1Conditionals (if / elif / else) Workflow
    1 / 4

    Step 1

    elif chains avoid nested ifs.

    Apply this step while implementing conditionals (if / elif / else) in real code.

    Real-world use

    Pattern matching with match is now standard in modern Python codebases — especially for handling API responses, state machines, and parsing.

    Best practices

    • Use elif over nested ifs.
    • Exploit truthy/falsy for cleaner checks.
    • Prefer match-case for multi-branch dispatch.

    Common mistakes

    • if x == True: is redundant — just if x:.

    Hands-on exercise

    Interview preparation — practice these questions:

    • What's truthy/falsy?
    • What's match-case?
    • How does ternary work?

    Summary

    In summary: elif chains > nested ifs. match-case for modern dispatch.

    Ready to mark this lesson complete?Track your journey across the entire course.