Using assert assertions is a very good habit of learning python, and Python assert asserts that the sentence format and usage are simple. Before we can complete a program, we don't know where the program is going to go wrong, and instead of letting it crash at the top of the run, it's better to crash when the error condition occurs, and then we need the help of the assert assertion. This article is mainly about the basic knowledge of assert assertions.
The role of the Python assert assertion
A python assert assertion is a decision that declares that its Boolean value must be true, and if an exception occurs, it indicates that the expression is false. You can understand that the Assert assertion statement is Raise-if-not, which is used to test the expression, and its return value is false, triggering an exception.
The syntax format of an ASSERT assertion statement
How does assert python work?
Expression assert expressions
The following statements for the Assert usage are available for reference:
assert 1==1assert 2+2==2*2assert len (['myboy',]) <10 assert Range (4) ==[0,1,2,3]
How to add an exception parameter for an ASSERT assertion statement
The exception argument to assert is to add string information after the assertion expression to explain the assertion and better know where the problem is. The format is as follows:
assert expression [, arguments]
assert expression [, parameter]
assert len (lists) >=5,' list element number less than 5'assert 2==1,' 2 is not equal to 1 '
The role of Python assert