Skip to content
Related Articles

Related Articles

Python assert keyword

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 26 Aug, 2022
Improve Article
Save Article

Assertions in any programming language are the debugging tools that help in the smooth flow of code. Assertions are mainly assumptions that a programmer knows or always wants to be true and hence puts them in code so that failure of these doesn’t allow the code to execute further. 

assert in Python

In simpler terms, we can say that assertion is the boolean expression that checks if the statement is True or False. If the statement is true then it does nothing and continues the execution, but if the statement is False then it stops the execution of the program and throws an error.

Flowchart of Python Assert Statement

assert Keyword in Python

assert in Python

assert Keyword in Python

In python, assert keyword helps in achieving this task. This statement takes as input a boolean condition, which when returns true doesn’t do anything and continues the normal flow of execution, but if it is computed to be false, then it raises an AssertionError along with the optional message provided. 

Syntax : assert condition, error_message(optional) 

Parameters : 
condition : The boolean condition returning true or false. 
error_message : The optional argument to be printed in console in case of AssertionError

Returns : 
Returns AssertionError, in case the condition evaluates to false along with the error message which when provided. 

Example 1: Python assert keyword without error message

Python3




# Python 3 code to demonstrate
# working of assert
 
# initializing number
a = 4
b = 0
 
# using assert to check for 0
print("The value of a / b is : ")
assert b != 0
print(a / b)

Output : 

The value of a / b is : 
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [19], in <cell line: 10>()
      8 # using assert to check for 0
      9 print("The value of a / b is : ")
---> 10 assert b != 0
     11 print(a / b)

AssertionError: 

Example 2: Python assert keyword with error message

Python3




# Python 3 code to demonstrate
# working of assert
 
# initializing number
a = 4
b = 0
 
# using assert to check for 0
print("The value of a / b is : ")
assert b != 0, "Zero Division Error"
print(a / b)

Output:

AssertionError: Zero Division Error

Practical Application

This has a much greater utility in Testing and Quality Assurance role in any development domain. Different types of assertions are used depending upon the application. Below is the simpler demonstration of a program that only allows only the batch with all hot food to be dispatched, else rejects the whole batch.

Python3




# Python 3 code to demonstrate
# working of assert
# Application
 
# initializing list of foods temperatures
batch = [ 40, 26, 39, 30, 25, 21]
 
# initializing cut temperature
cut = 26
 
# using assert to check for temperature greater than cut
for i in batch:
    assert i >= 26, "Batch is Rejected"
    print (str(i) + " is O.K" )

Output : 

40 is O.K
26 is O.K
39 is O.K
30 is O.K

Runtime Exception : 

AssertionError: Batch is Rejected

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!