Python fifth day, two knowledge points three mesh operator and assert throw exception handling.

Source: Internet
Author: User
Tags throw exception

There were no trinocular operators before Python 2.5x, but with the development of language, three mesh operators were added after 2.5

1 " "2 here the main three-mesh operator3 It is also possible to quickly declare variables and assign values to variables by x,y=4,5 this way. 4 " "5x,y=4,56 ifx<y:7Small=x8 Else:9Small=yTen Print(small) One  A  - Print("after the-----is changed to the three mesh operator----")     -Small=xifX<yElsey the Print(small)

Sometimes we in the development process, the program will be running when the possibility of a false pre-judgment, then we use what method to pre-judgment, this time we use the Assert this function, its role is, when the pre-judgment after the establishment, then run, if not, throw an exception.

  

1 " " 2 simple usage of assert 3 " " 4 a,b=3,65assert a>b6print(" performed ")

We judge A>b after the assert, but we can see by the assignment that it is not true, and this time the assert throws an exception:

Traceback (most recent):   " e:/python/day-1/three mesh. PY "  in <module>    b=3.6Typeerror:cannot Unpack non-iterable Float Object

But if the conditions in the assert change to B>a, then the program prints "executed."

 

Python fifth day, two knowledge points three mesh operator and assert throw exception handling.

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.