Python's short-circuit logic

throw bricks and start jade

Earlier in the article on boolean types, I left a question, and now it's time to answer it.

#测试一
>>> 2 and 4
4
>>> 3 or 5
3
>>> "python" and "C"
'C'
>>> "Hello" or 123
'Hello'

Why is it like this? Before answering, let's take a look at the following code:

#测试二
>>> (not 3) or (0 and 5) or (4 and 7) or (2 and 6) or (8 and 9)
7

Do you know why the answer is 7yes? This is because andthese ortwo logical operators follow short-circuit logic , so what is short-circuit logic? Definitely not a short circuit in the brain.

short circuit logic

The so-called short-circuit logic is: from left to right, only when the value of the first operand cannot determine the result of the logical operation, the second operand is evaluated.

Looking at the concept in this way is a bit too abstract, let's analyze the code directly, first look at the code of test 1:

  1. 2 and 4The value is 4, this is because andthe operator requires only Truewhen , the result can be True, at this time the left 2is True, but we still need to know the value on the right to judge the final result, so Python directly returns the right value.
  2. 3 or 5The value of is 3, this is because orthe operator requires as long as there is one of the two operands True, then the result is True, at this time 3, Trueso Python directly returns the value of the first operand.
  3. The same is true for strings or other types.

Let's take a look at two examples:

>>> 0 and 5
0
>>> 0 or 3
3
  1. 0 and 5, 0for False, for and, the result must be False, so return it directly 0.
  2. 0 or 3, 0for False, for or, cannot make a final judgment, the result depends on the following operands, so Python directly returns the following operands 4.

reveal the mask

For Python, it does not need to judge all the results before returning, just return the key values ​​that affect the results. This is short-circuit logic.

Ok, let's go back to the code of test 2 now:

  1. For those with parentheses, we calculate the parentheses first, so the code becomes:

    >>> False or 0 or 7 or 6 or 9
    
  2. Operate according to the short-circuit logic (from left to right). Since the remaining logical operators here are all or, according to our previous analysis, we only need to find a Truevalue of for, and we don’t need to worry about the latter, so the result Just for 7.

operator precedence

Let’s introduce this concept through a question. Let’s change the program of Test 2 and remove all the brackets. Can you guess what the result is?

#测试三
>>> not 3 or 0 and 5 or 4 and 7 or 2 and 6 or 8 and 9

The answer is still 7, are you right? It doesn't matter, you'll see why in a moment.

When we were learning mathematics in elementary school, we have already learned the concept of priority, such as multiplication and division before addition and subtraction, and Python is no exception. Regarding the priority of Python, I also have a special table, click me to send, and for logical operators , It is also prioritized, from high to low:

not > and > or from high to low

For example, calculate0 or 3 and not 6

  1. According to the highest priority not, it becomes 0 or 3 and False.
  2. Second and, so becomes 0 or False.
  3. Finally or, so the result False.

Back to the code of test three:

  1. becomeFalse or 0 and 5 or 4 and 7 or 2 and 6 or 8 and 9

    1. becomeFalse or 0 or 7 or 6 or 9
    2. final result 7.

at last

Well, the above is a summary of this Python learning. If there is anything wrong, please point it out, and let's learn and make progress together.

Guess you like

Origin blog.csdn.net/m0_46376148/article/details/108556994