The KPI line from format_kpi_line shows spend and CPL beautifully. But your CFO doesn't want pretty text — she wants a list of every campaign that blew its budget. How would you flag those in Excel?
Conditional formatting, probably. Or an IF column that outputs "Over" or "OK".
Exactly that instinct. In Python, the comparison spend > budget already is the answer — it evaluates to True or False without an IF statement. A bool is not a string, not a number. It is the answer to a yes-or-no question. Here's the whole thing:
spend = 14200.0
budget = 12000.0
flagged = spend > budget # TrueWait — so I don't need to write if spend > budget: return True? The comparison just returns a bool on its own?
Right. spend > budget is already a bool expression. Wrapping it in an if to return True is like writing =IF(A1>B1, TRUE, FALSE) in Excel instead of just =A1>B1. The comparison is the value. So a function that flags campaigns is a one-liner:
def is_over_budget(spend: float, budget: float) -> bool:
result = spend > budget
print(f"Spend ${spend:,.2f} vs budget ${budget:,.2f}: flagged={result}")
return resultThat's so clean. The whole flag logic is one line. And the CFO can filter the returned list to only the True rows — like filtering a pivot on a boolean column.
You just described a WHERE clause. Python is flattered. One trap to watch: > compares numbers to numbers. If spend ever arrives as the string "14200" from a CSV export, "14200" > 12000.0 raises a TypeError instead of flagging it. Always convert before comparing.
A string that looks like a number isn't actually a number. I've hit that in Excel too — cells formatted as text that break SUM.
Same root cause, different error message. The fix is always float(spend) before the comparison. That habit will matter when you start reading CSV exports — the pipeline you're building assumes clean floats at the boundary.
A bool is Python's native yes/no type: True or False. Comparison operators produce bools directly:
| Operator | Meaning |
|---|---|
> | greater than |
< | less than |
== | equal |
!= | not equal |
spend > budget evaluates to True or False — no if wrapper needed. Returning a comparison expression is the canonical bool-returning function pattern.
Comparing a string to a float ("14200" > 12000.0) raises TypeError. Always coerce CSV-sourced values with float() before comparing.
The KPI line from format_kpi_line shows spend and CPL beautifully. But your CFO doesn't want pretty text — she wants a list of every campaign that blew its budget. How would you flag those in Excel?
Conditional formatting, probably. Or an IF column that outputs "Over" or "OK".
Exactly that instinct. In Python, the comparison spend > budget already is the answer — it evaluates to True or False without an IF statement. A bool is not a string, not a number. It is the answer to a yes-or-no question. Here's the whole thing:
spend = 14200.0
budget = 12000.0
flagged = spend > budget # TrueWait — so I don't need to write if spend > budget: return True? The comparison just returns a bool on its own?
Right. spend > budget is already a bool expression. Wrapping it in an if to return True is like writing =IF(A1>B1, TRUE, FALSE) in Excel instead of just =A1>B1. The comparison is the value. So a function that flags campaigns is a one-liner:
def is_over_budget(spend: float, budget: float) -> bool:
result = spend > budget
print(f"Spend ${spend:,.2f} vs budget ${budget:,.2f}: flagged={result}")
return resultThat's so clean. The whole flag logic is one line. And the CFO can filter the returned list to only the True rows — like filtering a pivot on a boolean column.
You just described a WHERE clause. Python is flattered. One trap to watch: > compares numbers to numbers. If spend ever arrives as the string "14200" from a CSV export, "14200" > 12000.0 raises a TypeError instead of flagging it. Always convert before comparing.
A string that looks like a number isn't actually a number. I've hit that in Excel too — cells formatted as text that break SUM.
Same root cause, different error message. The fix is always float(spend) before the comparison. That habit will matter when you start reading CSV exports — the pipeline you're building assumes clean floats at the boundary.
A bool is Python's native yes/no type: True or False. Comparison operators produce bools directly:
| Operator | Meaning |
|---|---|
> | greater than |
< | less than |
== | equal |
!= | not equal |
spend > budget evaluates to True or False — no if wrapper needed. Returning a comparison expression is the canonical bool-returning function pattern.
Comparing a string to a float ("14200" > 12000.0) raises TypeError. Always coerce CSV-sourced values with float() before comparing.
Ayaan's marketing team exports campaign spend from HubSpot every Friday. The CFO has asked for a flag on every campaign that exceeded its allocated budget. Write `is_over_budget(spend, budget)` that returns `True` if spend exceeds budget and `False` otherwise — for example, `is_over_budget(14200.0, 12000.0)` should return `True` and `is_over_budget(9500.0, 12000.0)` should return `False`.
Tap each step for scaffolded hints.
No blank-editor panic.