True and false judgment in Python (and-or)

Table of contents

1. Boolean type

Two, the difference between Python2 and Python3

3. Judgment of authenticity

1. Boolean type

The objects included in the Boolean type can only be True or False. This type is mainly used for Boolean expressions.

Python provides a series of Boolean comparisons and logical operations.

Boolean operations:

greater than, a>100 greater than or equal to, a>=100

Less than, a<100 Less than or equal to, a<=100

Equal, a==100 Not equal, a!=100

logic operation:

logical negation, not a

Logical AND, a and b

Logical OR, a or b

Two, the difference between Python2 and Python3

There is no Boolean type in Python2. It uses the number 0 to represent False and 1 to represent True.

True and False are defined as keywords in Python 3, but their values ​​are still 1 and 0. bool is a subclass of int. True and False can be added to numbers. True==1 and False==0 will return True.

3. Judgment of authenticity

1. Python will treat the numbers 0, 0.0, empty strings, empty values ​​​​none, and empty objects (including empty tuples, empty collections, and empty lists) as False, and others as True.

>>> print(not 0)
True
>>> print(not 0.0)
True
>>> print(not [])
True
>>> print(not None)
True
>>> print(not ())
True
>>> print(not {})
True
>>> print(not '')
True
#######注意对象中不为空的时候
>>> print(not [0])
False
>>> print(not [1])
False

2. Logical operations and-or

(1)expr1 and expr2     

If expr1 is true, it means expr2 will be processed and the value of expr2 will be returned;

If expr1 is false, it means that the value of expr1 will be returned directly without processing expr2.

>>> print(True and 0)
0
>>> print(True and not 0)
True
>>> print(False and not 0)
False

(2)expr1 or expr2

If expr1 is true, it means that the value of expr1 will be returned directly without processing expr2;

If expr1 is false, it means expr2 will be processed and the value of expr2 will be returned.

>>> print(True or 0)
True
>>> print(False or 0)
0

(3) Multiple and: expr1 and expr2 and expr3

and scans Boolean expressions from left to right, returning the last true expression if all values ​​are true, and the first false expression if false.

If expr1 is true, expr1 and expr2 will return the value of expr2. At this time, expr1, expr2 and expr3 can be transformed into expr2 and expr3, and then determine whether expr2 is true or false. Repeat (1);

If expr1 is false, expr1 and expr2 will return the value of expr1. At this time, expr1, expr2 and expr3 can be transformed into expr1 and expr3, and then determine whether expr1 is true or false, repeat (1);

>>> print(1 and 0 and 1)
0
>>> print(1 and 1 and 0)
0
>>> print(0 and 0 and 1)
0
>>> print(0 and 1 and 1)
0
>>> print(1 and 1 and 1)
1

(4) Multiple OR: expr1 or expr2 or expr3

The use of or is exactly the opposite of and. It calculates the entire Boolean expression from left to right. If there is a true value, the first true value is returned immediately. If the entire expression is false, the last false value is returned. value.

If expr1 is true, expr1 or expr2 will return the value of expr1. At this time, expr1 or expr2 or expr3 can be transformed into expr1 or expr3, and then determine whether expr2 is true or false. Repeat (2);

If expr1 is false, expr1 or expr2 will return the value of expr2. At this time, expr1 or expr2 or expr3 can be transformed into expr2 or expr3, and then determine whether expr1 is true or false. Repeat (2);

>>> print(1 or 0 or 1)
1
>>> print(1 or 1 or 0)
1
>>> print(0 or 0 or 1)
1
>>> print(0 or 1 or 1)
1
>>> print(0 or 0 or 0)
0

(5)and-or并列:expr1 and expr2 or expr3

The same idea as the previous multiple and multiple or:

If expr1 is true, expr1 and expr2 will return the value of expr2. At this time, expr1 and expr2 or expr3 can be transformed into expr2 or expr3, and then repeat (2);

If expr1 is false, expr1 and expr2 will return the value of expr1. At this time, expr1 and expr2 or expr3 can be transformed into expr1 or expr3, and then repeat (2);

>>> print(1 and 1 or 0)
1
>>> print(1 and 0 or 1)
1
>>> print(0 and 1 or 0)
0
>>> print(0 and 0 or 1)
1

Guess you like

Origin blog.csdn.net/qq_54381110/article/details/131228537