Python offers a keyword known as the ‘assert’ keyword and we use it to run conditional checks to handle different errors. If the condition is not satisfied an error is raised and the program halts. Assert also tells us where the error happened.
This condition is known as the ‘Assertion Condition’ and the error is called the ‘Assertion Error’. To put it simply they are Boolean expressions that check a condition.
Conceptually this might seem like repetitious content, in that we can just use an if or try statement to check and handle conditions but the purpose of the assert statement is different. Generally, if and try are generally geared towards handling errors raised by the user, assert is mainly used to handle errors faced by developers. Sometimes, certain errors in Python (such as TypeError) are raised much later on if it is not caught at an earlier time.
The syntax of using ‘Assert’ is:
- assert <condition>, <error message> (optional)
When using the assert statement, it is very similar to Python code for:
if not Assertion_Condition: raise AssertionError
Let’s consider an example for assertion:
zoo_animals =  def add_to_zoo(animals_list): global zoo_animals # animals_list must not be 0 assert len(animals_list) != 0 for animal in animals_list: # adding animal to zoo animals list zoo_animals.append(animal) carnivore_animals = ["Lion", "Cheetah"] add_to_zoo(carnivore_animals) herbivore_animals =  # empty animal list add_to_zoo(herbivore_animals)
If we run this we get:
The program has a global variable to store a list of all the zoo animals. We then created a function that adds these animals to the list of zoo animals. The function takes a list of animals such as carnivore or herbivore animals, and then adds each of these animals one by one. Within the function, we first assert that the list must not be less than 0, then using a for loop we add these animals one at a time.
We created an empty list of herbivore animals to see how our assert statement reacts to it. When we call our “add_to_zoo” function with our herbivore list, the assert function checks if the list is empty or not. If the list is not empty, i.e., the Assertion Condition is True, it goes on to execute the rest of our code and if it is not, Python raises the ‘AssertionError’ stops our program.
We can also add an optional error message to our assert, as shown below:
def add_to_zoo(animals_list): global zoo_animals # animals_list must not be 0 assert len(animals_list) != 0, "Animal list must not be empty" for animal in animals_list: # adding animal to zoo animals list zoo_animals.append(animal)
When we run this, we get:
We see that the error message that we added is displayed the error message after the ‘AssertionError’.
If we have a Python script that uses a lot of Assert statements, we can disable all of those in the command line by adding ‘-O’, when we are running the Python script. Let’s add a print statement to see our list of zoo animals at the end of the program and then run the script as:
- python -O <your_script_name>.py
Now when we run it, we get the output as:
As we can see, the code ran without running into the Assert statement.
What have we learned?
- What is the use of the ‘assert’ keyword?
- How does Assert differ from if and try statements?
- What is the optional part of an Assert statement?
- What are Assertion Condition and Assertion Error?
- How do we disable Assert statements in our code?