The law of Python3 and and or operations

Source: Internet
Author: User
Tags logical operators

One, contains a logical operator

First, starting with the basic concept, which objects in Python will be treated as False? And which is True?

In Python, none, 0 in any numeric type, empty string "", Empty tuple (), empty list [], empty dictionary {} are treated as false, and custom types, if the __ nonzero __ () or __ len __ () method is implemented and the method returns 0 or FALSE, The instance is also treated as false, and the other objects are true.

Here is the simplest logical operation: here are the ture; 0, "both are false

    1and2 ==>2 1 Or2 ==> 1 1 and 0 ==> 0 1 or 0 ==> 1 Span class= "Hljs-keyword" >0 and 1 ==> 0 0 or 1 ==> Span class= "Hljs-keyword" >1 0 and "  ==> 0 0 or  "==> "    

Summary: Condition of an operator

    1. or left to right, returns the first true value, all false returns the latter value
    2. And from left to right, if all values are true, returns the latter value, with a false value, returns the first false value
Ii. logical operators with two or more

Logical operator and/or once more than one, the core idea of its operational rules is short-circuit logic. Okay, so let's get to the short-circuit idea:

The expression is left-to-right, and if the left logical value of OR is True, then all expressions (whether and or or) that are short-circuited or are directly output or left-hand expressions.

The expression is left-to-right, and if the left logical value of and is False, then all the and expressions are shorted, until an or appears, and the output and left expression to the left of or is involved in the next logical operation.

If the left of the or is False, or if the left side of and is True, the short-circuit logic cannot be used.

Let's start with a simple, hypothetical full and statement or all or statement:

Example 1
defA ():Print('A')    return[]defb ():Print('B')    return1defC ():Print('C')    return1defd ():Print('D')    return []defe ():Print('E')    return1ifA () andB () andC () andD () ande ():Print('OK')

A () is false, followed by an and statement, all short-circuited, and finally only the expression of a () is returned. Remember that all short-circuited expressions are not output. So, just print A here.

Example 2
defA ():Print('A')    return1defb ():Print('B')    return1defC ():Print('C')    return[]defd ():Print('D')    return1defe ():Print('E')    return1ifA () andB () andC () andD () ande ():Print('OK')

Python executes a () from left to right, a () returns a logical value of true, followed by an and statement, so it cannot be shorted thereafter, continues with the logical operation of B (), the logical value of a () and B () output B () is true, and then logical operation with C (), B () an The logical value of D C () output C () is False, and thereafter all is an and statement, all short-circuited, and finally only a B C is printed.

Example 3
defA ():Print('A')    return1defb ():Print('B')    return1defC ():Print('C')    return []defd ():Print('D')    return1defe ():Print('E')    return[]ifA ()orB ()orC ()orD ()ore ():Print('OK')

The logical value of a () is true, followed by an OR statement, all short-circuited, eventually only a, and if statement is true, so you also print an OK.

Example 4
defA ():Print('A')    return []defb ():Print('B')    return []defC ():Print('C')    return1defd ():Print('D')    return []defe ():Print('E')    return1ifA ()orB ()orC ()orD ()ore ():Print('OK')

Python executes a () from left to right, a () returns a logical value of false, followed by an or statement, so it cannot be shorted thereafter, continues with a logical operation with B (), the logical value of a () or B () output B () is false, followed by a logical operation with C (), B () or The logical value of C () output C () is True, followed by an OR statement, all short-circuited, and finally only a B-c OK is printed.

Let's take a look at the same situation where the and and OR statements exist:

Example 5
defA ():Print('A')    return []defb ():Print('B')    return []defC ():Print('C')    return1defd ():Print('D')    return []defe ():Print('E')    return1deff ():Print('F')    return1defg ():Print('G')    return []defh ():Print('H')    return1ifA () andB () andC () andD ()orE () andF ()orG () andh ():Print('OK')

Don't think the statement is very long is difficult, we analyze, from left to right, first a () the logical value is False, followed by the or statement there are three and statements: a () and B () and C () and D (), are short-circuited. Just output a (), get a () or E () to True, Output e (), get E () and F () to true, output F (), followed by an or statement, then the short circuit is followed by all. Finally, only a E F OK is printed. (combined with the above three points, slowly analyzed)

3. Ternary operator

Before python2.5, Python had no ternary operator, Guido Van Rossum thought it would not help python to be more concise, but programmers who used C, C + + and Java programming tried to simulate ternary operations with and OR OR. Using Python's short-circuit logic.

Ternary operator bool? A:B, if BOOL is true, a, otherwise B.

Translate to Python language as:

        bool and A or B

How to understand it? First A, B are true, this is the default. If BOOL is true, then bool and a are true, output a, short-circuit B. If bool is false, short circuit a, direct bool or B, output B.

In a simpler way:

        Return a if bool else b

The law of Python3 and and or operations

Related Article

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.