Conditional statements let programs make decisions. Based on whether a condition is True or False, Python executes different code. This is fundamental to all programming — from grading systems to games.
Syntax: if condition: (colon, then indented block). elif (else if) checks another condition. else runs when all above are False. Example: if marks >= 90: grade = "A" elif marks >= 75: grade = "B" else: grade = "C". Python uses INDENTATION (4 spaces) to define blocks — no braces {}. Forgetting indentation causes IndentationError.
Nested if: an if inside another if — for multi-level decisions. Example: if age >= 18: if has_license: print("Can drive"). Logical operators combine conditions: and (both True), or (at least one True), not (flip). Example: if age > 12 and age < 18: print("Teenager"). Short-circuit evaluation: Python stops checking once result is determined.
elif (short for "else if") checks a specific condition — you can have multiple elif blocks. else has NO condition — it catches everything that didn't match the if or any elif. Think of it as a default/catchall. Example: if temp > 30: "Hot" elif temp > 20: "Warm" elif temp > 10: "Cool" else: "Cold" (anything 10 or below).
Book a Trial + Diagnostic session. Get a personalized Learning Path with clear milestones, tutor match, and a plan recommendation — all within 24 hours.
Book Trial + Diagnostic →