Syllabus Lesson 10 of 239 · Control Flow
Control Flow

Branching with if / elif / else

if / elif / else let a program choose between paths. Python checks each condition top to bottom and runs the FIRST block whose condition is true, then skips the rest.

if temperature >= 30:
    label = "hot"
elif temperature >= 15:
    label = "mild"
else:
    label = "cold"

Order matters: check the highest / most specific condition first. Because the first true branch wins, an elif only runs when every condition above it was false.

Your turn

Complete grade_for(score) with if / elif / else: return "A" for 90 or above, "B" for 70 or above, "C" for 50 or above, otherwise "F".

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output