Python skill practice
How do you approach this?
Find Python Logic Flaws Before Shipping
Published · Updated
Published by Unrust. Scoring and editorial method
Quick answer
Find a Python flaw by stating what each input category should mean, then walking that meaning through conditions, loops, and defaults. Python truthiness makes empty lists, zero, empty strings, and None easy to group together even when the product contract treats them differently. Test the exact boundaries and read a clean-looking expression for its behavior, not just its elegance.
Original example
Do not treat position zero as a missing position
def pick(items, index):
if index:
return items[index]
return NoneThe contract returns None for an omitted index and the item for a valid non-negative integer index. Here pick(['first', 'second'], 0) incorrectly returns None instead of 'first'. The repair on this page uses an explicit None check and rejects negative, boolean, fractional and out-of-range indexes. This policy intentionally differs from Python's ordinary support for negative list indexes.
Separate absence from an empty or zero value
A condition such as if index or if records can be correct only when every falsy value has the same domain meaning. List the valid values explicitly and decide which are accepted, which cause a fallback, and which require an error. That small table often reveals the missing condition faster than a broad rewrite.
Check iteration and default behavior together
A loop may quietly do nothing for an empty iterable, while a default return value makes that no-op look successful. Read the loop and its result as one contract: should no records produce an empty collection, a sentinel, a warning, or a failure for the caller?
Check a concrete outcome
Use the original example with this input. These are local teaching fixtures, checked against the displayed snippet; they do not call external services.
Contract: None returns None; valid non-negative integer indexes return an item. The zero input should return first but the proposal returns None. The repair below separates the two cases and rejects invalid indexes.
| Expression after the example | Observed result |
|---|---|
| [pick(["first", "second"], 0), pick(["first", "second"], None)] | [None, None] |
Repair absence without discarding zero
The repaired helper returns 'first' for zero, 'second' for one, and None for None. Negative, fractional, boolean and out-of-range inputs raise IndexError under this explicitly restricted contract.
def pick(items, index):
if index is None:
return None
if type(index) is not int or not 0 <= index < len(items):
raise IndexError("index outside the supported range")
return items[index]Checklist
- Which valid Python values are falsy but not absent?
- What should an empty iterable communicate to the caller?
- Does this default distinguish invalid input from a normal boundary case?
Practice loop
- 01Write a Python input table containing None, zero, an empty container, and one ordinary value when relevant.
- 02Trace the branch and return value for each entry instead of relying on a happy-path example.
- 03Add a focused test for the first value whose expected domain meaning differs from the code.
Limits of this page
A small Python condition does not represent every issue in a service, including serialization, concurrency, numerical precision, or library behavior. One exercise is a snapshot of one task and cannot determine why a particular edge was overlooked or whether it reflects a lasting skill gap.
Source context
Published by Unrust as practical teaching material using public language documentation and original examples. See the scoring and editorial method for scope, checking and limitations.
Check your own starting point
One diagnostic session is a snapshot, not causal proof of why a result occurred. It can help you choose a focused next practice step without making a broader claim about your ability.
Take the free diagnosticTake the read in Python →Same six skills, side by side on your dashboard.