Skip to main content
Albero Academy

Conditional Statements

Control program flow with if, elif, else, ternary expressions, match-case, guard clauses, and clean conditionals.

Chapter 8Python14 min readBeginner friendly
  • #Python
  • #Control Flow
  • #Conditionals

Conditionals are how programs make decisions. Python's if family is short and readable, but the way you structure conditionals separates clean code from spaghetti. This chapter covers both.

if / elif / else

Python
score = 78
if score >= 90:
grade = "A+"
elif score >= 75:
grade = "A"
elif score >= 60:
grade = "B"
else:
grade = "C"
print(grade) # A

Python uses indentation to mark blocks — no curly braces, no then.

Truthy & falsy values

Any value can be tested in an if — Python evaluates "truthiness". The following are all falsy:

Falsy values
False
None
0, 0.0, 0j (any zero)
"" (empty string)
[], {}, (), set() (empty containers)
custom objects with __bool__ → False
Python
name = ""
# Pythonic — relies on truthiness
if not name:
print("name is missing")
# Less pythonic — explicit comparison
if name == "":
print("name is missing")
# Common pattern — provide default
display_name = name or "Anonymous"

Ternary expressions

Python
age = 22
status = "adult" if age >= 18 else "minor"
print(status) # adult
# Compose with f-strings
print(f"You are {'eligible' if age >= 18 else 'too young'} to vote.")

match-case (Python 3.10+)

Python's pattern matching — like a smarter switch. Best for branching on the shape of data.

Python
def describe(point):
match point:
case (0, 0):
return "origin"
case (x, 0):
return f"on x-axis at {x}"
case (0, y):
return f"on y-axis at {y}"
case (x, y):
return f"point at ({x}, {y})"
case _:
return "not a 2-tuple"
print(describe((0, 0))) # origin
print(describe((5, 0))) # on x-axis at 5
print(describe((3, 4))) # point at (3, 4)

Guard clauses

One of the most underused patterns. Instead of nesting your "happy path" deep inside conditionals, return early:

Python
# ❌ Nested
def process(user):
if user is not None:
if user.is_active:
if user.has_subscription:
return charge(user)
else:
return None
else:
return None
else:
return None
# ✅ Guard clauses — return early, indent shallowly
def process(user):
if user is None: return None
if not user.is_active: return None
if not user.has_subscription: return None
return charge(user)

The second version is faster to read, easier to debug, and gives each failure a clear exit point.

Writing clean conditionals

  • Prefer guard clauses over nested if trees.
  • Use in for membership: if status in {"open", "pending"}:, not a chain of ors.
  • Extract complex conditions into named variables: is_eligible = age >= 18 and has_id and not is_banned.
  • Reach for match-case when matching the shape of data, not for simple value checks.
  • Avoid double negatives: if not is_invalid: is harder to read than if is_valid:.

Key takeaways

  1. 01Python uses indentation, not braces — and any value can be truthy or falsy.
  2. 02Always use is None for None checks; never == None.
  3. 03Ternary expressions are one-liners — break to regular if/else if they wrap.
  4. 04match-case is for matching the shape of data; if/elif is fine for simple values.
  5. 05Guard clauses (return early) beat deeply nested ifs every time.