Use of assert statement in Python
- Assert statement are used to check whether the given logical or condition is True or False.
- if the condition is True, the program will continue to execute next line of code..
- However, if the condition is False then the program will stop the execution and raise an AssertionError.
- Assert statement is defined using assert keyword in Python. Assertions are always supposed to be True.
Assert condition [Error Message]
assert statement without error message
assert statement with error message
assert statement with try & except
assert statement with function
Above, square(10) will return 100, whereas square(-10) will raise an AssertionError because we passed -10.