Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 3
Assertions can be thought of as validators, used to make sure (assert) that the evaluation of an expression meets a condition.
Or to put it simply, make sure that a variable meets a given condition.
for example:
assert x == 2
is the same as doing:
if not (x == 2):
raise AssertionError()
The intended application of assertions is to debug your code without having to use external debugging tools. Just stick an assertion into your code to check a variable for some condition and if the assertion isn't met, you know where your code is falling over.
In this code I use assertions to implement static type checking in python:
https://code.sololearn.com/cI1r0xgbwDbF/?ref=app
In any case, avoid using assertions in production code for things like validating user input because assertions can be turned off entirely in the interpreter, leaving them to be ignored.