Introduction to Python AssertionError
The concept in programming in which the user defines a condition to be true while writing the code using the statement of assert before running the module is called assertion and the control moves to the next line in the code if the condition is true else if the condition is false, the program is stopped from running and Python AssertionError exception is returned and regardless of the programming language the program is implemented in, the function of the assert statement remains the same and it is a concept which is independent of the language but the syntax is different for different programming languages.
Syntax
The syntax of Python AssertionError is as follows:
assert condition, error_message(optional)
How does AssertionError in Python work?
Python language has an in-built assert statement using which we can create simple debug message outputs that are based on logical assertions. An AssertionError exception is raised when the statement of assert fails. BaseException class is the base of all exception classes in Python. Exception class is inherited from BaseException class which in turn is the base class of AssertionError class. Whenever an assert statement is used in the code, there should be proper exception handling code to handle the failure of the assert statement. The error message written by the programmer will be printed by the default handler of exception in python or the error can be handled without any error message as well.
Examples to Implement Python AssertionError
Below are the examples mentioned:
Example #1
Python program to demonstrate Assertion Error in a program which converts the temperature in degrees kelvin to temperature in degrees Fahrenheit:
Code:
#!/usr/bin/python
#a function is defined to convert the temperature in degrees kelvin to degrees Fahrenheit to which the temperature is passed as a parameter
def convertKelToFah(Temp):
#This program demonstrates assertion error while converting the temperature in degrees kelvin to temperature in degrees Farhenheit
#assert statement is used to check if the temperature is below zero and an exception is thrown if a negative parameter is passed to it
assert (Temp >= 0),"The temperature is below zero and it is really cold!"
#the converted value of temperature in degrees kelvin to degrees Fahrenheit is returned
return ((Temp-273)*1.8)+32
#temperature value 300 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit
print convertKelToFah(300)
#temperature value 350.50 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit and the integer value is returned
print int(convertKelToFah(350.50))
#temperature value -10 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit and the exception is thrown
print convertKelToFah(-10)
Output:
Explanation: In the above program, a function is defined to convert the temperature in degrees kelvin to degrees Fahrenheit to which the temperature is passed as a parameter. Then assert statement is used to check if the temperature is below zero and an exception is thrown if a negative parameter is passed to it. Then the converted value of temperature in degrees kelvin to degrees Fahrenheit is returned. Then temperature value 300 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit. Then temperature value 350.50 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit and the integer value is returned. Then temperature value -10 is passed to the function to convert it from degrees kelvin to degrees Fahrenheit and the exception is thrown. The output of the program is shown in the snapshot above.
Example #2
Python program to demonstrate Assertion Error with an error message in the program:
Code:
#This program demonstrates the Assertion Error with error message.
#a variable is defined to store an integer value
x = 1
#a variable is defined to store an integer value
y = 0
#assert statement is used to check if the second integer value is not equal to zero else error message "The operation is invalid because the denominator cannot be zero" is printed
assert y != 0, "The operation is invalid because the denominator cannot be zero"
#Otherwise the result of first integer divided by the second integer is printed
print(x / y)
Output:
Explanation: In the above program, the error message for the assertion error is printed. First, a variable is defined to store an integer value. Then again, a variable is defined to store another integer value. Then assert statement is used to check if the second integer value is not equal to zero else error message “The operation is invalid because the denominator cannot be zero” is printed. Otherwise, the result of the first integer divided by the second integer is printed. The output of the program is shown in the snapshot above.
Steps to avoid Assertion Error in Python
- We can use the -O flag to disable all the assert statements present in the process. By disabling the assert statements in the process, the statements following the assert statements will also be not executed.
- We can make use of the environment variable also to set a flag that disables also the assert statements. By using this, all the processes that inherit or use this environment will be affected.
- We can make sure the flow of control in the program does not reach the assert statements or the assertion error can be caught and from the point, the assertion error is handled, the normal flow of program resumes.
- We can turn on the basic optimizations by using -O which changes the extension of the filename for bytecode files or compiled files to .pyo from .pyc.
- We can also set the PYTHONOPTIMIZE option and if this option is set to a non-empty string which is the same as using -O option. If PYTHONOPTIMIZE is set to an integer, it is the same as using -O multiple times.
Conclusion
In this tutorial, we understand the concept of Assertion Error in Python through definition, the syntax of Assertion Error in Python, working of Assertion Error in Python through programming examples, and their outputs and the steps to avoid Assertion Error in Python.
Recommended Articles
We hope that this EDUCBA information on “Python AssertionError” was beneficial to you. You can view EDUCBA’s recommended articles for more information.