The role of Python assert

Source: Internet
Author: User
Tags assert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.