Coding/Learn/python/if elif else
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnIf-Elif-Else (Multi-Path Decisions)
17 / 32 0%
Lesson 17 of 32
7 mins read

If-Elif-Else (Multi-Path Decisions)

PreviousNext Lesson
In Plain English

What if you have more than two outcomes? Grade scores can be A, B, C, D, or F. Traffic lights can be Green, Yellow, or Red. The elif (short for "else if") keyword allows you to chain as many sequential checks as you need.

The Mental Model

"Think of a multiple-choice quiz. Python checks Option A (if). If False, it checks Option B (elif). If False, it checks Option C (elif). As soon as one matches, it takes that answer and skips the rest of the test."

Deep Dive: How It Works

Execution Order: Python checks conditions from top to bottom. The VERY FIRST condition that evaluates to True executes its block, and Python immediately skips the rest of the entire ladder!

You can have as many elif blocks as you want between the initial if and the optional closing else.

Order Matters: Place your most specific conditions at the top. If you check score > 50 before score > 90, a score of 95 will trigger the > 50 branch first and miss the > 90 branch.

Syntax Blueprint

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

Python tests each branch in sequence and exits the ladder at the first match.

PointCore Rules to Remember

Point star
First Match Wins: Python executes the first True branch and skips all remaining elif/else blocks.
Point star
Unlimited elifs: You can chain 2, 5, or 20 elif blocks to handle complex categories.
Point star
Optional else: The else block at the end is optional but recommended as a catch-all safety net.

Common Beginner Traps & How to Fix Them

Mistake:Using separate if statements instead of elif

Why it happens: Writing multiple ifs causes EVERY check to run independently, potentially triggering multiple outputs.

How to fix: Use elif to link checks together into a single mutually exclusive chain.

Pro Tip: In Python 3.10+, you can also use the match-case statement for structural pattern matching on values.

Live Interactive Example

Hit Run Code to see it live
Traffic Light State Machine
Python 3.12
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: Python tested green (False), tested yellow (True), ran the caution message, and skipped red and else.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Categorize age groups with if-elif-else

  • PointGiven age = 15, write an if-elif-else ladder:
  • PointIf age < 13, print "Child".
  • PointElif age < 20, print "Teenager".
  • PointElse, print "Adult".
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates