if, elif, else Statements in Python
An if statement runs a block of code only when a condition is true. Adding elif ("else if") lets you check additional conditions in sequence, and a final else catches everything that didn't match any prior condition. Python checks conditions top to bottom and runs only the first block whose condition is true, skipping the rest entirely.
if condition:
Introduces the first check; its block runs only if the condition is truthy.
elif condition: (optional, repeatable)
Checks another condition, but only if every earlier condition was false.
else: (optional, at most one)
Runs when none of the above conditions were true.
Python stops at the first match
Later elif and else blocks are skipped entirely once one condition succeeds.
Any number of elif blocks are allowed
Between the opening if and the optional else.
score = 75 if score >= 90: print("Grade: A")elif score >= 80: print("Grade: B")elif score >= 70: print("Grade: C")else: print("Grade: F")Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.