Python Assert Statement Tutorial

Python Assert

This is a detailed tutorial of Python Assert Statement. Learn to make use of the concept of Assertition for better Exception Handling in Python.

Assertion

Assertion means a statement of confidence. In programming, we often make use of the assertion statements that can raise an error according to a boolean expression. In python, the assert statement is used for the purpose of raise assertion errors. To make use of assertion, you must first need to form an expression about which you’re very confident.

Let’s try to understand the concept of Assertion with the help of an example. Consider the case of dividing two numbers. In the case of division to be valid, the denominator should not be zero. Therefore, if the value of the denominator provided is zero, it should be an invalid case and here we can make use of assertion to raise an error for this very particular case.

So, the expression for the current logic can be formed like If the value of denominator is zero, raise Assertion Error.

Therefore, Python Assert Statement is a kind of debugging tool that can help you manage running your python program efficiently at least avoiding all those possible conditions about which you are very confident that they can raise errors and troubles while executing your program. Find more about Assertion here.

The following diagram explains the concept of Assertion in python.

Python Assertion

Python Assert Statement

In python, the assertion errors can be raised using the assert statement. You must first make an expression for which it can check for an exception. This expression has to return a boolean value i.e. either True or False. If the condition is True, that means there is no exception and the program can continue executing the rest of the program. But if the expression returns False, then the assertion error is raised and program halts at the point.

Syntax

The syntax is simple. All you need to use the keyword assert followed by the boolean expression and you can optionally also define the custom error message followed after a comma.

In the default syntax, if the condition written as expression is not satisfied the AssertionError will be raised while in the syntax with a custom message, the AssertitonError is still raised if the condition is not satisfied but the message defined is also written along with the Exception in the console screen.

You can always make use of the Try Except in Python to check for errors during the runtime of the program. But using the assert statement is sometimes good when you’re very confident about the exception condition and also it helps to reduce the length of the code.

Examples

Let’s try to understand the concept of Assertion in python with the help of different examples. We will begin with a very simple example. The first example illustrates the Zero Division Assertion error.

AsseritionError without Message

Example. The following code does the division of two numerical values but raises an AsseritionError without any message in case the value of the denominator is zero.

As the defined value of the variable denominator is 0, therefore this division will be invalid and hence the condition of the assert statement is False. The program halts and raises the AsseritionError without any additional message.

Python Assert Statement Example

AsseritionError with Message

Example. The same example is re-written here but now we’ve also defined a custom message for the Exception.

Now, we’ve also written a message in the assert statement and as you can see in the console output that as the condition is not satisfied, therefore the AsseritionError is raised and now the defined message is also displayed.

Python Assertion Error With Message

Another Assert Keyword Example

Example. We’re calculating the mean of numerical values of a Python List generated List Comprehension Technique. In case the list has no elements, it raises the AssertionError.

You can clearly see in the first case, the list is not empty and contains 10 elements, therefore, the AsseritionError should not raise and it is obviously not raised as you can see in the output, but in the second case as the list is empty the AsseritionError is raised and Error message is also printed in the console.

Python Assert Statement Example With Python List Mean Calculation

Related Guide. Find Mean, Median & Mode in Python

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *