ANALYSIS
# 逻辑运算符
我们还应该学习的一件事是逻辑运算符。
逻辑运算符, 也称为布尔运算符,结合并评估两个条件。他们是 and, or, 和 not 运营商:
and返回True如果两个条件都是True, 并返回False否则。or返回True如果至少一个条件是True, 和False否则。not返回True如果条件是False, 反之亦然。
以下是一些示例:
PRTCL // PY
if hunger > 4 and anger > 1: print('Hangry')如果 hunger 变量大于 4 和 的 anger 变量大于 1,则程序打印“Hangry”。
PRTCL // PY
if coffee > 0 or bubble_tea > 0: print('😊')如果 coffee 变量大于 0 或者 的 bubble_tea 变量大于 0,则程序会打印笑脸。
PRTCL // PY
if not tired: print('Time to code!')如果 tired 变量是 不是 True, 然后程序打印“是时候编码了!”
所以你可能想知道: and 和 or 非常相似,我怎么记得两者之间的区别?让我们以表格形式将其分解:
| 一个 | 溴 | A 和 B | A 或 B |
|---|---|---|---|
False | False | False | False |
False | True | False | True |
True | False | False | True |
True | True | True | True |
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
