Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

assert in python
python assert

What does assert mean in Python?

Educative Answers Team

The assert statement is a sanity check statement, used when debugging code.

It is an alternative to an if statement. It gives an AssertionError exception if the expression inside the assert evaluates to be false and no error. It is basically an internal self-check within the code.


Assertion statement takes an expression and an optional error message as an input.

The general syntax is:


A custom error message can also be displayed if the assertion condition evaluates to be false.

assert expression, message

The following flow chart explains the concept:

svg viewer

What can assertion do?

  • It can check the type and value of the argument as well as the output of the function.
  • It can be used as a debugging tool since it halts the program where the statements execute to be false.


1. Without a custom error message

The following code explains how to use the assert in python:

x="Python Programming"
assert(x=="Python Programming") # no error
assert(x=="Python Language") # Assertion error 

2. With a custom error message

The following example uses assert with a custom error message defined:

Note: When writing a custom message, do not put a bracket around the assert condition

assert False, "We've got a problem" #custom error message
assert 2 + 2 == 5, "We've got a problem" #custom error message


assert in python
python assert
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring