Assertions are statements that assert or state a fact confidently in your program. For example, while writing a division function, you're confident the divisor shouldn't be zero, you assert divisor is not equal to zero.
Assertions are simply boolean expressions that checks if the conditions return true or not. If it is true, the program does nothing and move to the next line of code. However, if it's false, the program stops and throws an error. Check flow here
Python has built-in assert statement to use assertion condition in the program.
assert <condition>
assert <condition>,<error message>
In Python we can use assert statement in two ways as mentioned above.
assert statement has a condition and if the condition is not satisfied the program will stop and give AssertionError.
assert statement can also have a condition and a optional error message. If the condition is not satisfied assert stops the program and gives AssertionError along with the error message.
Key Points to Remember
Assertions are the condition or boolean expression which are always supposed to be true in the code.
assert statement takes an expression and optional message.
assert statement is used to check types, values of argument and the output of the function.
assert statement is used as debugging tool as it halts the program at the point where an error occurs.
def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)
mark1 = []
print("Average of mark1:",avg(mark1))
#Traceback (most recent call last):
# assert len(marks) != 0
# AssertionError