+ 1
What does assertion means ?
Plz give the answer in simple word as you possible
2 Respostas
+ 5
Assertion means that you assume that a statement is true and if it isn't, an exception will be raised (=there will be an error). Example:
n = 5
assert n < 10 # this is true, so nothing will happen
assert n > 10 # this is false, so the program will show an error
It is almost the same as an if statement:
if n < 10:
# do nothing
pass
if n <= 10:
raise ValueError # raise an exception
+ 1
assert is like an if, but where an if returning True will execute a block of code, an assert statement returning True will do nothing.
An if returning False will not execute a block of code, an assert returning False will throw an error.
assert True #code executes normally
assert False #AssertionError raised