Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下将打印'ok':
'ok'
if 5: print('ok')
然而,当我这样做时:
print(5 == True)
输出是False。
False
字符串也会发生同样的事情。为什么?
你在这里测试不同的东西。
如果标识等于 ,则仅if检查bool表达式的 (另请参见“真值测试”)是否不是。TrueTrue
if
bool
True
所以实际测试的if是:
>>> bool(5) == True True
True 的值为 1。如果您设置True = 5(仅在 python 2 中),则等式变为 True。'if' 语句就像是检查守卫是不是 0 或 None 所以每个不是 0 的数字都可以进入第一个块。事实上 False 的值为 0。
True = 5