ASSERTIONS IN PYTHON

An assertion is a sanity-check that you turn on or turn off when you have finished testing the program.

An expression is tested, and if the result comes up false, an exception is raised.

Assertions are carried out through use of the assert statement.

For example:

print(1)

assert 2 + 2 ==4

print(2)

assert 1 + 1 == 3

print(3)

Output: >>> 1

2

AssertionError

The assert can take a second argument that is passed to the AssertionError raised if the assertion fails

temp = -10

assert (temp >= 0), “Colder than absolute zero!”

Output: >> AssertionError : Colder than absolute zero!

Leave a comment

Design a site like this with WordPress.com
Get started