Python theorytheory 0/50 · 0%
Control flow · easy

9. Control flow: if/elif/else

Branching based on truthy conditions.

Conditionals use `if`, `elif`, and `else`, with truthiness rules: `0`, `""`, `None`, empty collections, and `False` are all falsy.

python
def fee_tier(gwei):
    if gwei < 10:
        return "low"
    elif gwei < 50:
        return "medium"
    else:
        return "high"

Python has no ternary keyword like `?:`, but supports a conditional expression:

python
status = "active" if balance > 0 else "empty"

Chained comparisons are also valid: `0 <= x < 100`.

Check your understanding

  1. 1. Which of these is falsy in Python?

  2. 2. What is Python's conditional expression syntax?

  3. 3. Is `0 <= x < 100` valid Python?