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

## Assertion

Assertion means a statement of confidence. In programming, we often make use of assertion statements that can raise an error according to a boolean expression. In python, the assert statement is used for the purpose of raising 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 the 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 Assert Statement

In python, 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 the program halts at that point.

### Syntax

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

#Default Syntax assert <condition-expression> #Syntax with Custom Message assert <condition-expression>, <message>

In the default syntax, if the condition written as the 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.

**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.

numerator = 10 denominator = 0 #Check Condition for AssertionError assert denominator != 0 #Does division only if the denominator is not equal to 0 result = numerator/denominator print(result)

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.

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

numerator = 10 denominator = 0 #Check Condition for AssertionError assert denominator != 0, "Division with 0 Error." #Does division only if the denominator is not equal to 0 result = numerator/denominator print(result)

Now, we’ve also written a message in the assert statement, and as you can see in the console output the condition is not satisfied, therefore the `AsseritionError`

is raised and now the defined message is also displayed.

### 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`

.

#CASE 1 values = [x * x for x in range(1,11)] #List of Squares of Numerical Values 1 To 10 print(values) assert len(values) != 0, "List can not be Empty" mean = sum(values)/len(values) #CASE 2 values = [] #List is empty #List of Squares of Numerical Values 1 To 10 print(values) assert len(values) != 0, "List can not be Empty" mean = sum(values)/len(values)

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.

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.