1.5 KiB
1.5 KiB
title | localeTitle |
---|---|
Python Boolean Operations | Python布尔运算 |
这些是布尔运算,按升序排序:
操作|结果|笔记
--------- | ------------------------------------ | -----
x或y |如果x为假,则为y,否则为x | (1)
x和y |如果x为假,则为x,否则为y | (2)
不是x |如果x为假,则为True,否则为False | (3)
笔记:
- 这是一个短路运算符,因此只有在第一个参数为False时才会计算第二个参数。
- 这是一个短路操作符,因此只有在第一个参数为True时才会计算第二个参数。
- 没有比非布尔运算符更低的优先级,因此不是a == b被解释为不是(a == b),而a ==不是b是语法错误。
例子:
not
:
>>> not True
False
>>> not False
True
and
:
>>> True and False # Short-circuited at first argument.
False
>>> False and True # Second argument is evaluated.
False
>>> True and True # Second argument is evaluated.
True
or
:
>>> True or False # Short-circuited at first argument.
True
>>> False or True # Second argument is evaluated.
True
>>> False or False # Second argument is evaluated.
False