如何在Python中使用布尔值?

问题:如何在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

[编辑]

有关更多信息:http : //docs.python.org/library/functions.html#bool

您的代码也可以工作,因为在需要时1会转换为True。实际上,Python很长一段时间没有布尔类型了(就像在旧的C语言中一样),并且一些程序员仍然使用整数而不是布尔值。

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

布尔内建函数大写:TrueFalse

另请注意,您可以做checker = bool(some_decision)一些速记- bool只会返回TrueFalse

很高兴知道这些类定义了__nonzero____len__将要成为TrueFalse取决于这些函数的结果以供将来参考,但是实际上每个其他对象的布尔结果都是TrueNone对象,空序列和数字零除外)。

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

布尔类型在文档中定义:http :
//docs.python.org/library/stdtypes.html#boolean-values

引用自doc:

布尔值是两个常量对象False和True。它们用于表示真值(尽管其他值也可以视为假或真)。在数字上下文中(例如,当用作算术运算符的参数时),它们的行为分别类似于整数0和1。如果可以将该值解释为真值,则可以使用内置函数bool()将任何值转换为布尔值(请参见上面的真值测试部分)。

它们分别写为False和True。

因此,在Java代码中,删除括号,将其更改trueTrue,就可以了:)

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并且只有两个值:TrueFalse)。

但是,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