How to understand python logical operators

步履不停
Release: 2019-07-03 11:27:57
Original
6841 people have browsed it

How to understand python logical operators

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!