提问人:Danish Khan 提问时间:7/30/2022 更新时间:7/30/2022 访问量:51
Python Booleans [已关闭]
Python Booleans [closed]
问:
在 Python 中,
if 1 and 2 not in {0, 1}: # return True
print(True)
if 0 and 2 not in {0, 1}: # return False
print(True)
不知道为什么有人可以向我解释一下?
答:
0赞
itismoej
7/30/2022
#1
这是因为它由多个条件组成。 等于和 等于 和 大多数情况下,它们可以互换使用。if
1
True
0
False
所以:
1 and 2 not in {0, 1}
# is actually
(1) and (2 not in {0, 1})
# which equals to
(True) and (2 not in {0, 1})
# and both of them are True
True and True
# -------------------
# but in this case
0 and 2 not in {0, 1}
# is actually
(0) and (2 not in {0, 1})
# which equals to
(False) and (True)
# that is False
如果要检查序列中是否有多个值,则应执行以下操作:
1 in {0, 1} and 2 in {0, 1}
2赞
RNovey
7/30/2022
#2
我想你的意思是:
if 1 not in {0, 1} and 2 not in {0, 1}:
你拥有它的方式,它正在解析(如果 1)和(如果 2 不在 {0, 1} “If 1”始终解析为 true。
2赞
EMO_4455
7/30/2022
#3
您可以利用所有
:
mySet = {0,1}
if not all(x in mySet for x in (1,2)):
print(True)
if not all(x in mySet for x in (0,2)):
print(True)
外:
True
True
评论
and
nat
in
Truth