Courses/Testing & Debugging

🧪 Testing & Debugging

Assertions

Sanity-check assumptions while developing.

assert raises AssertionError when the condition is false. Use it for invariants you *know* must hold — not for user input validation.

def average(xs):
    assert len(xs) > 0, "need at least one number"
    return sum(xs) / len(xs)
main.py
Output
Press Run to execute.
Expected output
4.0

Sign in to track your progress across lessons.