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 7
yes? This is because and
these or
two 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:
2 and 4
The value is4
, this is becauseand
the operator requires onlyTrue
when , the result can beTrue
, at this time the left2
isTrue
, but we still need to know the value on the right to judge the final result, so Python directly returns the right value.3 or 5
The value of is3
, this is becauseor
the operator requires as long as there is one of the two operandsTrue
, then the result isTrue
, at this time3
,True
so Python directly returns the value of the first operand.- 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
0 and 5
,0
forFalse
, forand
, the result must beFalse
, so return it directly0
.0 or 3
,0
forFalse
, foror
, cannot make a final judgment, the result depends on the following operands, so Python directly returns the following operands4
.
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:
-
For those with parentheses, we calculate the parentheses first, so the code becomes:
>>> False or 0 or 7 or 6 or 9
-
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
True
value of for, and we don’t need to worry about the latter, so the result Just for7
.
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
- According to the highest priority
not
, it becomes0 or 3 and False
. - Second
and
, so becomes0 or False
. - Finally
or
, so the resultFalse
.
Back to the code of test three:
-
become
False or 0 and 5 or 4 and 7 or 2 and 6 or 8 and 9
- become
False or 0 or 7 or 6 or 9
- final result
7
.
- become
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.