Python的评估0作为假 [英] Python evaluates 0 as False
本文介绍了Python的评估0作为假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python的控制台:
In the Python console:
>>> a = 0
>>> if a:
... print "L"
...
>>> a = 1
>>> if a:
... print "L"
...
L
>>> a = 2
>>> if a:
... print "L"
...
L
为什么会出现这种情况?
Why does this happen?
推荐答案
在Python中,布尔
是 INT $ C $的一个子类C>和
假
值为 0
;即使值不会隐在如果
语句转换为布尔
(它们所),假== 0
是真实的。
In Python, bool
is a subclass of int
, and False
has the value 0
; even if values weren't implicitly cast to bool
in an if
statement (which they are), False == 0
is true.
这篇关于Python的评估0作为假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文