提问人:Kondwani Paul Kufeyani 提问时间:11/14/2023 最后编辑:GuyKondwani Paul Kufeyani 更新时间:11/15/2023 访问量:46
布尔值
Booleans Values
问:
如果以下变量表示以下布尔值,则将全面评估以下哪项,第一个选项将被选中,包括第二个选项
a = True
b = False
not a or b
a and b
a or b
not and b
b and a
b or a
我尝试应用布尔原则,或者,似乎我仍然无法正确对待它,失败了。我评估了以下选项
not a or b **short circuit**
a and b **full evaluation**
a or b **short circuit**
not a and b **full evaluation**
b and a **short circuit**
b or a **full evaluation**
答:
0赞
furas
11/15/2023
#1
您可以使用函数来查看执行了哪个函数,而不是变量。a = True
b = False
print()
def a():
print('a = True')
return True
def b():
print('b = False')
return False
print('---')
print('not a or b')
print('result:', (not a()) or b())
print('---')
print('a and b')
print('result:', a() and b() )
print('---')
print('a or b')
print('result:', a() or b())
print('---')
print('not a and b')
print('result:', (not a()) and b())
print('---')
print('b and a')
print('result:', b() and a())
print('---')
print('b or a')
print('result:', b() or a())
结果:
--
not a or b
a = True
b = False
result: False
---
a and b
a = True
b = False
result: False
---
a or b
a = True
result: True
---
not a and b
a = True
result: False
---
b and a
b = False
result: False
---
b or a
b = False
a = True
result: True
评论
not a or b
not a and b
a
True
not a
False
b
False
False and b
False and a
False or b
False or a
a
b
True
False
not a and b
(not a) and b
not (a and b)
not
and
or