# Find Python Logic Flaws Before Shipping

> Find Python logic flaws by comparing conditions, defaults, and iteration behavior with the actual contract, especially around zero, empty containers, and optional values.

- Canonical HTML: [https://unrust.dev/languages/python/find-the-flaw](https://unrust.dev/languages/python/find-the-flaw)
- Markdown representation: [https://unrust.dev/languages/python/find-the-flaw/index.md](https://unrust.dev/languages/python/find-the-flaw/index.md)

Published: 2026-07-14

## Direct 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.

## 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?

## Original example

### Do not treat position zero as a missing position

```python
def pick(items, index):
    if index:
        return items[index]
    return items[0]
```

In this original Python example, zero is a valid list position but the truthiness check sends it to the fallback. The review work is to define what omitted, negative, and out-of-range indexes should mean to callers, then express that policy explicitly and test it at the boundary rather than relying on a generic truthy check.

## 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

1. Write a Python input table containing None, zero, an empty container, and one ordinary value when relevant.
2. Trace the branch and return value for each entry instead of relying on a happy-path example.
3. Add 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

Editorially reviewed for Unrust using public language documentation, common code-review practice, and small original examples written for this resource system.

## Related resources

- [How to Validate Copilot Suggestions](https://unrust.dev/use-cases/validate-copilot-suggestions/index.md): Validate autocomplete and Copilot suggestions by checking their contract, assumptions, and tests before a small completion becomes a hidden behavior change.
- [How to Prepare for a Focused Code Review](https://unrust.dev/use-cases/prepare-for-code-review/index.md): Prepare a small, evidence-rich code review by clarifying the contract, reducing unrelated changes, and showing tests that cover the risky behavior.

Next step: [Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
