Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

assert

assert performs a debugging check and raises AssertionError when its condition is false. An optional expression supplies the failure value, but optimized execution can disable assertions.

How it works

An assert statement evaluates a condition only when debugging is enabled. If the condition is false, Python raises AssertionError; the optional expression supplies the exception's value. Because assertions can be disabled with optimized execution, use them for internal debugging checks rather than required runtime validation.

Syntax

assert expression ["," expression]

Parameters

NameTypeDescription
expression / expression1expressionBoolean-like condition to test; the assertion fails when it evaluates as false.
expression2expressionOptional value used to construct AssertionError when the condition fails.

Key points

  • Use assert for concise internal consistency checks and debugging assumptions.
  • The one-expression form raises AssertionError when its condition evaluates as false.
  • The comma form evaluates a second expression that becomes the value passed to AssertionError on failure.
  • Assertion behavior is guarded by the built-in __debug__ flag.
  • The failure message expression is evaluated as the argument to the raised AssertionError.

Common mistakes

  • The documented equivalence depends on the normal built-in meanings of __debug__ and AssertionError; rebinding those names changes the assumption.

Example: Checking a precondition

def average(total, count):
    assert count > 0, "count must be positive"
    return total / count

print(average(10, 2))

The assertion checks a function precondition and raises AssertionError with a message when the condition is false.

When to use it

Invariant checking

Check an internal invariant after transforming data.

Development-time preconditions

Verify assumptions at function boundaries inside trusted code.

Control-flow diagnostics

Catch impossible branches or unexpected states while debugging.

Interview questions

QWhat forms can an assert statement take?

The syntax is assert expression or assert expression1, expression2. A false condition raises AssertionError; in the second form, the second expression becomes the exception value.

Follow-up: What happens under optimized execution? The assertion is skipped when __debug__ is false, so mandatory validation should use explicit conditionals and exceptions instead.

QWhat exception does assert raise, and how is its message supplied?

A simple assertion behaves like a conditional check guarded by __debug__; failure raises AssertionError. The message form raises AssertionError with the supplied expression as its value.

Follow-up: Why can a message expression still matter even when the condition is true? It is part of the assertion syntax, but it is only used to construct the exception when the condition fails.

QWhen should assert not be used?

Assertions are intended as convenient debugging checks. They should not replace validation of external input or any condition that must be enforced regardless of execution mode, because assertions may be disabled.

Follow-up: How would you enforce a required condition? Test it explicitly and raise an appropriate exception, such as ValueError, so the check remains active in optimized mode.

  • AssertionError
  • __debug__
  • raise
  • if

Source: 7. Simple statements. Checked 2026-10-16.