Learn python assert statement by example

Assert statements are a convenient way to insert debugging assertions into a program.

You can use it by :

assert <condition>


assert <condition>,<error message>

When condition is False, python application will raise an exception, error message will be displayed, True and application will continue. It seems like:

if condition:

How to use assert?

Here is a python example for how to use assert, in some applications, we need inputs and targets array dimensions are the same. So we can do:

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets),"inputs and target is not the same"

Of course, we also can use if statement to do it.