Conditionals in Python

From the Python cheat sheet · Control Flow · verified Jul 2026

Conditionals

if/elif/else statements and ternary expressions.

python
# if / elif / else
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "C"

# Ternary (inline if)
status = "adult" if age >= 18 else "minor"
💡 Python uses and/or/not instead of &&/||/!
⚡ Chained comparisons like 0 < x < 100 are valid and Pythonic
📌 Check empty collections with "if items:" — no need for len(items) > 0
🟢 The ternary syntax is: value_if_true if condition else value_if_false
conditionalsifternary

More Python tasks

Back to the full Python cheat sheet