Python and-or skills

Source: Internet
Author: User

Python and-or skills

Similar expressions in C language:Bool?A:BIf the expression value is true, the value is a, otherwise it is B.

Let's look at an example:

>>> A = "first" >>> B = "second" >>> 1 and a or B 'first '>>> 0 and a or B 'second'

This example is well understood. 1 indicates true, the value is a, otherwise it is B.

This is not the case during use. If expression a is false, the expression value is B.

For example:

>>> A = "" >>> B = "second" >>> 1 and a or B 'second'

There is a skill to avoid the above problems, as shown below:

>>> A = "" >>> B = "second" >>> (1 and [a] or [B]) [0]
Because [A]Is a non-empty list, it will never be false. Even AYes 0Or ''Or other false values, list [A]True because it has an element.





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.