如何在Python中使用布尔值?

时间:2021-08-26 11:49:28

Does Python actually contain a Boolean value? I know that you can do:

Python实际上是否包含布尔值?我知道你可以做到:

checker = 1
if checker:
    #dostuff

But I'm quite pedantic and enjoy seeing booleans in Java. For instance:

但我很迂腐,喜欢在Java中看到布尔人。例如:

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.

Python中有布尔值吗?我似乎在文件中找不到类似的东西。

6 个解决方案

#1


99  

checker = None # not necessary

if some_decision:
    checker = True

if checker:
    # some stuff

[Edit]

(编辑)

For more information: http://docs.python.org/library/functions.html#bool

更多信息:http://docs.python.org/library/functions.html #保龄球

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 sill use integers instead of booleans.

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

#2


81  

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.

还需要注意的是,您可以将checker = bool(some_decision)作为一个简短的缩写,bool只会返回True或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).

对于将来的参考来说,很高兴知道定义__nonzero__或__len__的类根据这些函数的结果是真或假,但是几乎所有其他对象的布尔结果都是真(除了无对象、空序列和数字零)。

#3


10  

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.

否则,None将计算为False,整数0和浮点0.0也一样(尽管我不会那样使用浮点数)。同样,空列表[]、空的tuplets()、空字符串“或”“值为False”。

Try it yourself with the function bool():

用bool()函数来尝试一下:

bool([])
bool(['a value'])
bool('')
bool('A string')
bool(True)  # ;-)
bool(False)
bool(0)
bool(None)
bool(0.0)
bool(1)

etc..

等。

#4


5  

Boolean types are defined in documentation:
http://docs.python.org/library/stdtypes.html#boolean-values

布尔类型在文档中定义: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).

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

They are written as False and True, respectively.

它们分别被写成False和True。

So in java code remove braces, change true to True and you will be ok :)

因此,在java代码中删除大括号,将true更改为true,您就可以:)

#5


3  

Yes, there is a bool data type (which inherits from int and has only two values: True and False).

是的,有一个bool数据类型(它继承自int,只有两个值:True和False)。

But also Python has the boolean-able concept for every object, which is used when function bool([x]) is called.

但是Python对于每个对象都有可引导的概念,这在调用函数bool([x])时使用。

See more: object.nonzero and boolean-value-of-objects-in-python.

看到更多:对象。非零和boolean-value-of-objects-in-python。

#6


0  

Unlike Java where you would declare boolean flag = True, in Python you can just declare myFlag = True

与Java不同的是,在这里您可以声明boolean标志= True,在Python中,您可以只声明myFlag = True。

Python would interpret this as a boolean variable

Python将其解释为布尔变量

#1


99  

checker = None # not necessary

if some_decision:
    checker = True

if checker:
    # some stuff

[Edit]

(编辑)

For more information: http://docs.python.org/library/functions.html#bool

更多信息:http://docs.python.org/library/functions.html #保龄球

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 sill use integers instead of booleans.

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

#2


81  

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.

还需要注意的是,您可以将checker = bool(some_decision)作为一个简短的缩写,bool只会返回True或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).

对于将来的参考来说,很高兴知道定义__nonzero__或__len__的类根据这些函数的结果是真或假,但是几乎所有其他对象的布尔结果都是真(除了无对象、空序列和数字零)。

#3


10  

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.

否则,None将计算为False,整数0和浮点0.0也一样(尽管我不会那样使用浮点数)。同样,空列表[]、空的tuplets()、空字符串“或”“值为False”。

Try it yourself with the function bool():

用bool()函数来尝试一下:

bool([])
bool(['a value'])
bool('')
bool('A string')
bool(True)  # ;-)
bool(False)
bool(0)
bool(None)
bool(0.0)
bool(1)

etc..

等。

#4


5  

Boolean types are defined in documentation:
http://docs.python.org/library/stdtypes.html#boolean-values

布尔类型在文档中定义: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).

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

They are written as False and True, respectively.

它们分别被写成False和True。

So in java code remove braces, change true to True and you will be ok :)

因此,在java代码中删除大括号,将true更改为true,您就可以:)

#5


3  

Yes, there is a bool data type (which inherits from int and has only two values: True and False).

是的,有一个bool数据类型(它继承自int,只有两个值:True和False)。

But also Python has the boolean-able concept for every object, which is used when function bool([x]) is called.

但是Python对于每个对象都有可引导的概念,这在调用函数bool([x])时使用。

See more: object.nonzero and boolean-value-of-objects-in-python.

看到更多:对象。非零和boolean-value-of-objects-in-python。

#6


0  

Unlike Java where you would declare boolean flag = True, in Python you can just declare myFlag = True

与Java不同的是,在这里您可以声明boolean标志= True,在Python中,您可以只声明myFlag = True。

Python would interpret this as a boolean variable

Python将其解释为布尔变量