Home > Backend Development > Python Tutorial > Analyze the usage of and and or in python

Analyze the usage of and and or in python

零下一度
Release: 2017-05-26 11:54:42
Original
11407 people have browsed it

In Python, and and or perform Boolean logic, as you would expect, but they do not return a Boolean value; instead, they return one of the values ​​they actually compare.

>>> 1 and 2 and 3
3
>>> False and 1 and 2
False
>>> 1 and 2 and 3 and 4
4
>>> 1 and 2 and 3 and False
False
>>> 1 or 2 or 3
1
>>> False or 1 or 2
1
>>> 1 and 2 and 3 or False and 1
3
Copy after login

In python and and or perform Boolean logical operations, but return actual values.

1. All are and, if both are true, return the last variable value; if false, return the first false value

2. All For or, if both are false, return the last value; if true, return the first true value

3.and and or:

>>> 1 and 2 or False
2
>>> False and 1 or 2
2
Copy after login

(a and b) or c : If a and b are true, the result is b. If a and b are false, the result is c. In fact, the principle is the same as and and or, similar to bool in C? a: b

In addition: and Priority is higher than or

>>>False and 1 or 2
2
>>> 1 or 2 and False
1
>>> (1 or 2 ) and False
False
Copy after login

From the above three examples, the first one determines that the priority of or is not as high as and, and then From the two examples, we can know that the priorities of and and or cannot be the same. If they are the same, the result of the second example should be False

[Related recommendations]

1. Python Syntax summary of and, or and and-or

2. What are the logical operators? python logical operator, logical operation in java, PHP logical operator && the only ternary operator

3. Detailed introduction to the actual usage of and and or in Python

4. Share the operational logic example tutorial of and / or in python

5. Summary of Python’s logical operators and

6. Python: Logical judgment and operator examples

The above is the detailed content of Analyze the usage of and and or in python. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template