Python logical operators

Source: Internet
Author: User

And Or not

Priority: () > and > or > Not

1.or

In Python, the logical operator or,x or y, if X is true, returns X if X is false to return the Y value. Because if X is true then the or operation exits to see L, one is true, so the value of x is returned. If the value of X is false, then the result of the or operation depends on Y, so the value of Y is returned.

1 Print or 2)   #  12printor 2)   #  33  printor 2)   #  24printor #  - 5 Print or 0)

2.and

In Python, the logical operator and,x and y, and if x is true, returns the Y value. If x is false, the Y value is returned. If the value of X is True,and the operation does not end, the value of Y continues to be seen, so true and false depends on the value of Y, so if X is true, then the value of Y is returned. If x is false, then the and operation ends the operation because there is a false and false, so the value of x is returned.

Print  and 2)  #  2print and 0)  #  0Print  and 2)  #  0print and 2)  #  2 Print  and 0)  #  0

3. Mixing examples and parsing

Print  and or  and 3 < 2)   #  false or False

In the case of a left-hand, high-priority rule, first, because the comparison operator takes precedence over the logical operator, it is simple to do so if the operator is lower than the logical operator precedence. and priority is greater than or

1 > 2 is False

3 < 2 is False

Flase and 3, because false is false so and does not return the operation directly false

4 and False, because 4 is true so the AND operator continues the operation, with false as the primary, so returns false.

False or False because false is false, so the OR operator will continue after the operation, with false as the primary, so return the false value after

Python logical operators

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.