【发布时间】:2021-03-22 23:19:01
【问题描述】:
我是 Python 的初学者,我在 YouTube 上看到了 Cory Schafer 编写的关于布尔值和条件的教程。当他试图展示 Python 认为哪些值是 False 时,他有一个片段。他对它们一一进行了测试,但我想知道是否有更有效/更有趣的方法来做到这一点,所以我尝试提出这个 for 循环语句。我期望输出是 8 行 Evaluated to False,但我不断得到 Evaluated to True。有人可以启发我吗?谢谢!
condition = (False, None, 0, 0.00, '', (), [], {})
for i in condition:
if condition: # It is assumed that condition == true here, right?
print('Evaluated to True')
else:
print('Evaluated to False ')
#OUT:
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
【问题讨论】:
标签: python python-3.x for-loop boolean boolean-logic