Python Assert Statement and AssertionError Explained with Example
Have you heard about ‘assert’ in Python? Or you may have got ‘AssertionError’ while executing any project code.
In this Python tutorial, you will learn
assert statement with examples and how to use it in your code.
What is Assert Statement in Python?
The assert statement is used in Python to verify the condition.
- If the condition is
False, it terminates the program by throwing an exception.
- If the condition is
True, it does not make any changes to the execution of your program.
It has a very simple syntax.
You can use any condition statement which returns either
Condition in the Python code is executed first. Based on the result of condition, assert statement either don’t make any changes (just like nothing happened) or terminates the program.
Python Assert Example
Let’s understand this concept with simple examples…
When the condition is True…
>>> assert True >>> >>>
If you run above the line of code on your Python console, nothing happens as the condition is
When the condition is False…
>>> assert False Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError >>>
Try running the above statement on your Python console. For any False condition, it throws an
You can capture
AssertionError error as a part of exception handling in Python.
Python Assert Exception Message
To distinguish multiple assert failure messages, you can also customize the message.
Customizing the message to display for the False condition:
>>> assert False, "Assertion failed!" Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError: Assertion failed! >>>
Many times this assert statement is very useful when you don’t want to execute your program for the invalid conditions or parameter values.
Let’s take a real and practical programming example. You are asking the users to enter their birthday month, so the accepted values are 1 to 12.
Here you don’t want your program continues running for an invalid (erroneous) month.
When the user provides a valid month number:
>>> month=int(input('birthday month: ')) birthday month: 11 >>> assert month in range(1,13) >>>
When the user provides an invalid month number:
>>> month=int(input('birthday month: ')) birthday month: 14 >>> assert month in range(1,13) Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError >>>
In the above code, we have used:
You can also disable assert statement globally.
How to disable Assert Statement Globally?
Assert statement can be used for debugging code and for the unit testing.
If your script already have
assert statement and you don’t want to execute it, you can use
-O command-line switch while starting Python.
Use the following command to open Python console.
This will disable the debugging mode of the Python.
Now, even if you provide False condition to the assert, it will not throw an error.
>python -O Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) Type "help", "copyright", "credits" or "license" for more information. >>> >>> assert False >>> >>>
This way it will disable assert statement globally.
Great! With this Python assert tutorial, you have learned some of the interesting concepts in Python. If you have any doubt, share it with me in the comment.