Control flow: conditions and conditional expressions

Make decisions using if/elif/else, comparisons, and Python conditional expressions.

Choose which block runs

An if statement evaluates a condition and runs its indented body when that condition is true. elif adds another branch, and else handles the remaining case:

score = 82

if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "Keep practicing"

Comparisons include ==, !=, <, <=, >, and >=. Combine conditions with and, or, and not. Python uses indentation to mark blocks, so keep the indentation consistent.

A decision diagram showing if, elif, and else branches

Conditional expression

For a short choice that produces one value, Python has a conditional expression:

status = "adult" if age >= 18 else "minor"

Read this as “use adult if the condition is true, otherwise use minor.” Prefer a normal if block when the branches are long or nested; clarity matters more than shortening a line.

Exercise: Set a temperature value and assign "cold", "mild", or "warm" using if/elif/else. Try a conditional expression for a separate yes/no label.

Sources: Python Tutorial: if Statements, LearnPython.org: Conditions