and 和 or 操作符总是接受两个布尔值(或表达式),所以他们被认为是"二元"操作符。
下列是and 表达式,查看结果。
>>> True and True
True
>>> True and False
False
# and操作符真值表,显示布尔操作符的所有可能结果 。
表达式 | 求值为 |
True and True | True |
True and False | False |
False and True | False |
False and False | False |
下列是or 表达式,查看结果。
>>> False or True
True
>>> False or False
False
# or操作符真值表,显示布尔操作符的所有可能结果 。
表达式 | 求值为 |
True and True | True |
True and False | True |
False and True | True |
False and False | False |
下列是not 表达式,查看结果。
>>> not True
False
>>> not False
True
表达式 | 求值为 |
not True | False |
not False | True |
混合布尔和比较操作符
下列是比较操作符的布尔表达式
>>> (4 < 5)and(6 < 7)
True
>>> (4 < 5)and(7 < 6)
False>>> (1 == 2) or (2 == 2)
True
下列是一个表达式中使用多个布尔操作符
>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
True