Back to course

Conditional Statements 3: The `elif` (Else If)

Python Programming: The 0 to Hero Bootcamp

The if...elif...else Chain

When you have more than two possible outcomes, you use elif (short for 'else if'). This allows you to check multiple conditions sequentially.

Python evaluates the conditions from top to bottom. As soon as one condition evaluates to True, the corresponding block is executed, and the rest of the chain is skipped entirely.

Syntax

python if condition_1: # Runs if condition 1 is True elif condition_2: # Runs if condition 1 was False AND condition 2 is True elif condition_3: # ...and so on else: # Runs only if ALL preceding conditions were False

Example: Letter Grading System

python student_score = 85

if student_score >= 90: grade = 'A' elif student_score >= 80: # Only checked if score < 90 grade = 'B' elif student_score >= 70: # Only checked if score < 80 grade = 'C' else: grade = 'F'

print(f"The student's grade is: {grade}")

Output: The student's grade is: B