The assert
keyword is used to perform a runtime assertion check. It allows you
to test whether a certain condition is True, and if the condition is not met, an
AssertionError exception is raised, which can help you identify issues or bugs
in your code during development and testing.
The syntax of the assert
statement is as follows:
assert condition, message
Here, condition
is the expression you want to check, and message
is an
optional string that you can provide to give more context about the assertion
failure. The condition
should evaluate to True for the code to continue
executing normally. If the condition
is False
, Python raises an
AssertionError
with the provided message
(or a default message
if none is
given).
Here's an example of how you might use the assert statement:
def divide(a, b):
assert b != 0, "Division by zero is not allowed"
return a / b
result = divide(10, 0)
print(result)
In this example, the assert
statement checks if b is not equal to zero before
performing the division. Since b is 0, the condition is False
, and an
AssertionError
is raised with the specified message: "Division by zero is not
allowed".