Learn Python – Python Assert Keyword- Basic and advance

Python assert keyword is described as a debugging device that tests a condition. The Assertions are normally the assumption that asserts or state a fact confidently in the program. For example, while writing a division function, the divisor have to not be zero, and you assert that the divisor is no longer equal to zero.

It is simply a Boolean expression that has a circumstance or expression checks if the circumstance returns true or false. If it is true, the software does no longer do anything, and it moves to the subsequent line of code. But if it is false, it raises an AssertionError exception with an optionally available error message.

The foremost assignment of assertions is to inform the builders about unrecoverable blunders in the application like “file now not found”, and it is proper to say that assertions are inside self-checks for the program. It is the most critical for the testing or best assurance in any application development area. The syntax of the assert keyword is given below.

Syntax

assert condition, error_message(optional)    

Why Assertion is used

It is a debugging tool, and its most important venture is to check the condition. If it finds that the circumstance is true, it moves to the subsequent line of code, and If not, then stops all its operations and throws an error. It points out the error in the code.

Where Assertion in Python used

Checking the outputs of the functions.

Used for testing the code.

In checking the values of arguments.Checking the valid input.

Example1

This instance shows the working of assert with the error message.

def avg(scores):    
    assert len(scores) != 0,"The List is empty."    
    return sum(scores)/len(scores)    
    
scores2 = [67,59,86,75,92]    
print("The Average of scores2:",avg(scores2))    
    
scores1 = []    
print("The Average of scores1:",avg(scores1))    

Output:

The Average of scores2: 75.8
AssertionError: The List is empty.

Explanation: In the above example, we have exceeded a non-empty list scores2 and an empty listing scores1 to the avg() function. We acquired an output for scores2 listing successfully, but after that, we got an error AssertionError: List is empty. The assert circumstance is cosy via the scores2 list and lets the software proceed to run. However, scores1 does not fulfill the situation and offers an AssertionError.

Example2:

This instance shows the “Divide by 0 error” in the console.

# initializing number     
x = 7    
y = 0    
# It uses assert to check for 0     
print ("x / y value is : ")     
assert y != 0, "Divide by 0 error"    
print (x / y)     

Output:

x / y value is :

Runtime Exception :

Traceback (most recent call last):  
  File "main.py", line 6, in <module>  
    assert y != 0, "Divide by 0 error"  
AssertionError: Divide by 0 error  

Explanation:

In the above example, we have initialized an integer variable, i.e., x=7, y=0, and strive to print the value of x/y as an output. The Python interpreter generated a Runtime Exception because of the assert keyword found the divisor as zero then displayed “Divide by zero error” in the console.