Complete worked example
python
def classify(salary):
if salary is None:
return "unknown"
if salary >= 100:
return "high"
return "lower"- Reasoning
- Missingness is tested before the inclusive numeric threshold.
Typorithm · easy
Step 1 of 4 · intro card
Make missingness and branch priority explicit instead of using a truthiness shortcut.
A condition should reflect the business distinction being made. The worked function labels a missing salary observation as unknown, a known value at least one hundred as high and other known values as lower. The numbers are fictional teaching units, not a salary recommendation.
None and zero represent different facts. None means no value was supplied under this contract; zero is an actual numeric observation. Python truthiness considers both false-like, so a generic if not salary would discard a distinction the report needs to preserve.
The explicit salary is None branch runs before numeric comparison.
The explicit salary is None branch runs before numeric comparison. Identity comparison with None states the missing-value check clearly. The inclusive threshold puts exactly one hundred in high, and the final else covers the remaining known numeric values.
The function assumes a number or None as its input domain. A caller that receives arbitrary JSON must validate that boundary separately. Mixing unknown-input parsing into every later computation makes both the arithmetic contract and its error behavior harder to understand.
python
def classify(salary):
if salary is None:
return "unknown"
if salary >= 100:
return "high"
return "lower"The counterexample uses if not salary and labels a genuine zero as unknown.
The counterexample uses if not salary and labels a genuine zero as unknown. It can still pass tests containing None, eighty and one hundred, so the zero case is essential. A separate common error changes the inclusive threshold to a strict greater-than comparison.
Write the branch table before the code: missing, below threshold, exact threshold and above threshold. If conditions overlap, the first matching branch wins. Branch order should express an intentional priority rather than accidentally compensating for unclear predicates.
python
def classify(salary):
if not salary:
return "unknown"
if salary >= 100:
return "high"
return "lower"Classify None as unknown, zero as zero and every other supplied numeric value as nonzero.
Classify None as unknown, zero as zero and every other supplied numeric value as nonzero. This practice changes the category contract and removes the earlier salary threshold. It should not treat a negative supplied value as missing.
Use two explicit comparisons in the intended priority order. The first small step handles missingness; the second handles a known zero before the fallback. No loop, collection or exception mechanism is required to solve the core task.
Step 1: Handle None first. Step 2: Then classify a known zero before the nonzero fallback.
python
def classify(value):
# Distinguish None, zero and all other supplied numbers.
raise NotImplementedErrorThe solution preserves all three categories and returns nonzero for both positive and negative supplied numbers.
The solution preserves all three categories and returns nonzero for both positive and negative supplied numbers. This is a value-classification exercise, not a domain validation policy that accepts every possible salary. The input domain and the category meaning remain separate.
Acceptance includes None, zero, a positive and a negative number. Comparing only the output for zero would not prove that missingness still has its own category. Avoid replacing the two explicit branches with a compact expression that hides those distinctions from a beginning reader.
python
def classify(value):
if value is None:
return "unknown"
if value == 0:
return "zero"
return "nonzero"In an interview, explain why is None communicates a sentinel check and why truthiness is useful only when all false-like values have the same intended meaning.
In an interview, explain why is None communicates a sentinel check and why truthiness is useful only when all false-like values have the same intended meaning. Empty strings, empty collections, zero and None do not universally mean the same thing.
The next lessons introduce mutable collections and identity. They continue the habit of preserving important distinctions: a new name can reference the same list, and a shallow copy can still share nested objects even when the outer list is different.
Optional stretch — after the core task works: Write a branch table for empty string versus missing text before deciding whether truthiness is appropriate.
Computer practice: https://applydjinn.com/typorithm/python-lab-20261105.zip. Run verify.py with your selected Python interpreter, then complete an exercise and check it with --practice followed by the lesson id. The lab uses only the standard library and disposable local fixtures; it never connects to the production database or company feeds.
Make missingness and branch priority explicit instead of using a truthiness shortcut.