问题:如何在Python中使用布尔值?
Python实际上包含布尔值吗?我知道您可以做到:
checker = 1
if checker:
#dostuff
但是我相当学究,喜欢在Java中看到布尔值。例如:
Boolean checker;
if (someDecision)
{
checker = true;
}
if(checker)
{
//some stuff
}
Python中是否有布尔值之类的东西?我似乎在文档中找不到类似的内容。
Does Python actually contain a Boolean value? I know that you can do:
checker = 1
if checker:
#dostuff
But I’m quite pedantic and enjoy seeing booleans in Java. For instance:
Boolean checker;
if (someDecision)
{
checker = true;
}
if(checker)
{
//some stuff
}
Is there such a thing as a Boolean in Python? I can’t seem to find anything like it in the documentation.
回答 0
checker = None
if some_decision:
checker = True
if checker:
# some stuff
[Edit]
For more information: http://docs.python.org/library/functions.html#bool
Your code works too, since 1
is converted to True
when necessary.
Actually Python didn’t have a boolean type for a long time (as in old C), and some programmers still use integers instead of booleans.
回答 1
布尔内建函数大写:True
和False
。
另请注意,您可以做checker = bool(some_decision)
一些速记- bool
只会返回True
或False
。
很高兴知道这些类定义了__nonzero__
,__len__
将要成为True
或False
取决于这些函数的结果以供将来参考,但是实际上每个其他对象的布尔结果都是True
(None
对象,空序列和数字零除外)。
The boolean builtins are capitalized: True
and False
.
Note also that you can do checker = bool(some_decision)
as a bit of shorthand — bool
will only ever return True
or False
.
It’s good to know for future reference that classes defining __nonzero__
or __len__
will be True
or False
depending on the result of those functions, but virtually every other object’s boolean result will be True
(except for the None
object, empty sequences, and numeric zeros).
回答 2
True
…而且False
显然。
否则,None
计算结果为False,整数0
和浮点数也将计算为False 0.0
(尽管我不会那样使用浮点数)。此外,空列表[]
,空tuplet ()
和空字符串''
或""
评估为False。
使用以下函数自己尝试bool()
:
bool([])
bool(['a value'])
bool('')
bool('A string')
bool(True) # ;-)
bool(False)
bool(0)
bool(None)
bool(0.0)
bool(1)
等等..
True
… and False
obviously.
Otherwise, None
evaluates to False, as does the integer 0
and also the float 0.0
(although I wouldn’t use floats like that).
Also, empty lists []
, empty tuplets ()
, and empty strings ''
or ""
evaluate to False.
Try it yourself with the function bool()
:
bool([])
bool(['a value'])
bool('')
bool('A string')
bool(True) # ;-)
bool(False)
bool(0)
bool(None)
bool(0.0)
bool(1)
etc..
回答 3
Boolean types are defined in documentation:
http://docs.python.org/library/stdtypes.html#boolean-values
Quoted from doc:
Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function bool() can be used to cast any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).
They are written as False and True, respectively.
So in java code remove braces, change true
to True
and you will be ok :)
回答 4
是的,有一个bool
数据类型(继承自int
并且只有两个值:True
和False
)。
但是,Python也具有boolean-able
每个对象的概念,在调用函数时会使用该概念bool([x])
。
查看更多:对象。Python中的非零和布尔值对象。
Yes, there is a bool
data type (which inherits from int
and has only two values: True
and False
).
But also Python has the boolean-able
concept for every object, which is used when function bool([x])
is called.
See more: object.nonzero and boolean-value-of-objects-in-python.
回答 5
与您要声明的Java不同boolean flag = True
,在Python中,您可以声明myFlag = True
Python会将其解释为布尔变量
Unlike Java where you would declare boolean flag = True
, in Python you can just declare myFlag = True
Python would interpret this as a boolean variable