Python assert keyword
Instance
Test whether the condition returns True:
x = "hello" # If the condition returns True, nothing will happen: assert x == "hello" # If the condition returns False, it will raise AssertionError: assert x == "goodbye"
Definition and Usage
The assert keyword is used when debugging code.
The assert keyword allows you to test whether the conditions in the code return True, otherwise, the program will raise AssertionError.
You can write a message that will be output if the code returns False, see the following examples.
More Instances
Instance
If the condition is False, write a message:
x = "hello" # How to conditionally return False, raise AssertionError: assert x == "goodbye", "x should be 'hello'"