Section 11.1: and 和 or 不确定返回布尔值
使用 or 时,如果表达式中的第一个值为 True,则返回第一个值,否则返回第二个值。也就是说,or 相当于
def or_(a, b):
if a:
return a
else:
return b
对于 and ,如果第一个值为 false,则返回第一个值,否则返回最后一个值:
def and_(a, b):
if not a:
return a
else:
return b
Section 11.2: 一个简单的例子
在 Python 中,您可以使用两个二进制运算符对单个元素进行比较--一边一个:
if 3.14 < x < 3.142:
print("x is near pi")

在许多(大多数?)编程语言中,这将以一种与常规数学相反的方式进行评估:(3.14 < x) < 3.142,但在 Python 中,它被处理成3.14 < x 和 x < 3.142,就像大多数非程序员所期望的那样。
Section 11.3: 短路评估
Python 只对布尔表达式进行最小限度的求值。
>>> def true_func():
... print("true_func()")
... return True
...
>>> def false_func():
... print("false_func()")
... return False
...
>>> true_func() or false_func()
true_func()
True
>>> false_func() or true_func()
false_func()
true_func()
True
>>> true_func() and false_func()
true_func()
false_func()
False
>>> false_func() and false_func()
false_func()
False

Section 11.4: and
当且仅当两个参数都是真参数时,才对第二个参数求值。否则,计算第一个假参数。
x = True
y = True
z = x and y # z = True
x = True
y = False
z = x and y # z = False
x = False
y = True
z = x and y # z = False
x = False
y = False
z = x and y # z = False
x = 1
y = 1
z = x and y # z = y,所以 z = 1
x = 0
y = 1
z = x and y # z = x, 所以 z = 0 (see above)
x = 1
y = 0
z = x and y # z = y, 所以 z = 0 (see above)
x = 0
y = 0
z = x and y # z = x, 所以 z = 0 (see above)

上例中的 1 可以改成任何真值,0 可以改成任何假值。
Section 11.5: or
如果其中一个参数是真参数,则对第一个真参数进行求值。如果两个参数都是假的,则求值为第二个参数。
x = True
y = True
z = x or y # z = True
x = True
y = False
z = x or y # z = True
x = False
y = True
z = x or y # z = True
x = False
y = False
z = x or y # z = False
x = 1
y = 1
z = x or y # z = x, 所以 z = 1
x = 1
y = 0
z = x or y # z = x, 所以 z = 1
x = 0
y = 1
z = x or y # z = y, 所以 z = 1
x = 0
y = 0
z = x or y # z = y, 所以 z = 0

上例中的 1 可以改成任何真值,0 可以改成任何假值。
Section 11.6: not
它的返回结果与下面的语句相反:
x = True
y = not x # y = False
x = False
y = not x # y = True
