September 2026
1051 pages
35h 4m
English
The assert keyword enables you to integrate assertions into a Python program. By writing an assert statement, the programmer specifies a condition that is essential for the execution of the program and that must always hold True when the assert statement is reached. If the condition of an assert statement evaluates to False, then an AssertionError exception is raised. In the following interactive mode session, several assert statements were entered:
>>> lst = [7, 1, 3, 5, -12]>>> assert max(lst) == 7>>> assert min(lst) == -12>>> assert sum(lst) == 0Traceback (most recent call last): File "<python-input-3>", line 1, in <module> assert sum(lst) == 0 ^^^^^^^^^^^^^AssertionError
An error message can also be specified in the ...
Read now
Unlock full access