Python Quest

Track 9 · Lesson 9 of 11

Assertions and invariants

Use assert to state what must be true inside your program, catch your own bugs early, and know why assertions must never validate user input.

About 30 minIntermediate4 exercises

You will be able to

  • Write an assert statement with a helpful message
  • Use assertions for preconditions, postconditions and invariants that only a bug can break
  • Explain why assertions are switched off by python -O and must never validate user input
  • Spot the always-true assertion caused by parentheses

Included with a Formula Quest membership

It has 4 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.

View Formula Quest memberships

Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.