Dev Helpers

Python / Errors and testing

Check expected values with assertions

Dev Helpers · July 11, 2026

Assertions make assumptions visible in tests. Python can disable them in optimized execution, so required input validation deserves an explicit exception.

Worked example

def add(a, b):
    return a + b
assert add(2, 3) == 5
print('Passed')

Result

Passed

Implementation notes

A useful assertion documents the invariant that broke. Include enough test context to distinguish incorrect inputs from an incorrect transformation.

Try it yourself

Run an assertion-based sample normally and under python -O.

Open the Python example →

Editorial date July 11, 2026 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.