Python keyword and and or usage instances

Source: Internet
Author: User
And the expression is evaluated from left to right in Python, and if all values are true, the last value is returned, and if there is a false, the first false value is returned.

The or is also left-to-right with a computed expression, returning the first true value.
Copy the Code code as follows:


IDLE 1.2.4
>>> ' A ' and ' B '
' B '
>>> ' and ' B '
''
>>> ' A ' or ' B '
A
>>> ' or ' B '
' B '


Similar to the use of a trinocular expression: bool? A:b
Copy the Code code as follows:


>>> a = ' first '
>>> B = ' second '
>>>1and A or B # is equivalent to the case of bool = True
' First '
>>>0and A or B # is equivalent to the case of bool = False
' Second '
>>> a = ' '
The problem occurs when >>>1and A or B # A is False
' Second '
>>> (1and[a]or[b]) [0]# safe usage, because [a] cannot be false, at least one 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.