Register Login

assert Keyword in Python

Assertions in programming play a significant role in debugging and handling errors based on True or False conditions. Assertions help in smoothly running a code. Assertions are essentially assumptions that programmers assume or plan and when they want it to be true; they put them in code so that, they can check the failure of those code block that does not allow the code to execute further.

Assert Keyword:

Python also allows programmers to set an assertion for debugging purposes. They achieve this using the assert keyword. This keyword will allow programmers to check whether a condition in the code returns True or not. If it does not return true, the program raises an AssertionError. This statement will accept input as a Boolean condition and when it is true, does not return anything.


assert condition, error_message (optional)


x = 6
y = 0
# implementing assert keyword for checking for 0 in the divisor part
print(" The result when x divides y is : ")
assert y != 0, "Divide by 0 error"
print(x / y)

You can see the program outputs:

Assert Python

The result when x divides y is :

Also, it throws a runtime exception as shown in the figure.

Assert in Software testing & Quality Assurance:

Assert in Python plays a significant role when it comes to quality assurance in software testing. Developers use various forms of assertions depending upon the project or the situation in which they are working. Here is a program showing that the groups who will get rejected will not submit their assignments on or before the 16th.


dateOfSub= [10, 16, 3, 9, 25, 21]
# initializing the last date
lastdate = 16
# implement the assert statement for checking whether submission day is less than or equals last date
for i in dateOfSub:
assert i <= lastdate, "Batch is Rejected"
print(str(i) + " date is OK or submission")


An asset is a popular tool and keyword in Python for debuggers and quality assurance testers within a development team. Hence, if you are planning to become a full-stack developer, learning the debugging aspects of Python programming will give you an upper hand in the development experience.