First of all, the priority of ‘and’, ‘or’ and ‘not’ is not>and>or.
Secondly, the logical operators and and or are also called short-circuit logic or lazy evaluation: their parameters are parsed from left to right, and stop once the result can be determined. For example, if A and C are true and B is false, A and B and C will not resolve C. When operating on an ordinary non-logical value, the return value of the short-circuiting operator is usually the last variable. Therefore, the understanding of logical operators is also different from that in C language. For example:
>>> 3 and 4 4 >>> 4 and 3 3 >>> 4 or 3 4 >>> 3 or 4 3
In the above example, according to the C language pair thinking, 3 and 4, that is, 3 and 4 are 3, but because it is a short-circuit operator, the result is 4 because the and operator must Only when all the operands are true will all the operands be parsed and the last variable returned, which is 4; change the order of 4 and 3, and the result will be different, which is 3.
And Or logic (or), that is, as long as one of them is true, it will stop parsing the operands and return the variable that is true most recently, that is, 3 or 4, with a value of 3; change the order to 4 or 3 and it will be 4.
Related tutorial recommendations:Python video tutorial
The above is the detailed content of How to understand python logical operators. For more information, please follow other related articles on the PHP Chinese website!