Make decisions with if

Use comparisons and conditional branches to choose which instructions run.

An if statement runs a block when its condition is true. elif checks another condition, and else handles the remaining case. Indentation marks which statements belong to each branch.

Choose a message by score

python · static example

score = 72

if score >= 80:
    result = "Great work"
elif score >= 50:
    result = "Passed"
else:
    result = "Try again"

print(result)

The comparisons are checked from top to bottom. Only the first matching branch runs.

Sample output
Passed

Labeled sample only — nothing was run on this page.

Knowledge check

Which branch runs when `score` is 72?

Pick an answer to check.