Python is a versatile programming language that allows you to perform almost any kind of computational task. The built-in modules, functions, and keywords in Python can be extremely helpful to programmers for performing various tasks.
An assert statement is a built-in statement or keyword in Python used for debugging code. It is a type of check that tests the functionality of your code. The Assert statement works like a boolean expression, checking whether the given condition is true or false. If the given condition is true, then nothing happens, and the next line of code is executed. If the given condition is false, then the assert statement stops the execution of the program and outputs an error. The assert statement works like the raise keyword and outputs an exception when the given condition is false. The exception raised by an assert statement is also called an AssertionError. This article briefly covers the assertion statement in Python with some examples.
Syntax
The syntax for using the assert statement is as follows:
Printing the message with the assert statement is optional. With the message statement, the syntax for the assert statement is as follows:
Examples
Now, we will discuss some examples to understand the usage of the assert statement. While performing the division operation, we should keep in mind that the divisor is not equal to zero. If the divisor is zero, then the program will raise the “ZeroDivisionError.” We can insert an assert statement to check that the divisor is not equal to zero. We will implement this in Python script.
num1= 10
#declaring second number variable
num2 =0
# using assert statement
assert num2!=0, "The divisor is zero"
Output
With the assert statement, we have applied the condition that the num2 (divisor) should not be equal to zero. The given condition is false because the num2 value is equal to zero. The Python interpreter raises the assertion error that “The divisor is zero.”
Next, we will change the value of num2 and execute our program. In this example, the assert statement is now true. So, in this case, nothing will happen. The assert statement will be neglected, and the next line will be executed.
num1= 10
#declaring second number variable
num2 =10
# using assert statement
assert num2!=0, "The divisor is zero"
print("The result is:",num1/num2)
Output
In the output, it can be seen that the assert statement is neglected, and the next line of code is executed.
val ="LinuxHint"
#inserting an assert statement
# The condition is false, assert statement will be executed
assert val!="LinuxHint", "The condition is false"
Output
The above example could be done in another way, as well. Let us try this other way.
val ="hello"
#inserting an assert statement
# The condition is false, assert statement will be executed
assert val=="LinuxHint", "The variable value is not equal to LinuxHint"
The val variable value is “hello.” This means that the condition is not true, and the program will execute the assert statement.
Output
Conclusion
The assert statement is a built-in statement in Python that allows you to debug your code. The assert statement takes a condition and an optional message. It checks the condition of a given statement. If the condition is true, then nothing happens, and the next line of code is executed. If the condition is false, the assert statement outputs an AssertionError. This article explained the assert statement in detail with some simple examples.