如何在Python中使用布尔值? [英] How do I use a Boolean in Python?

查看:313
本文介绍了如何在Python中使用布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python实际包含布尔值吗?我知道您可以做到:

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

checker = 1
if checker:
    #dostuff

但是我很书呆子,喜欢在Java中看到布尔值.例如:

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

Boolean checker;
if (someDecision)
{
    checker = true;
}
if(checker)
{
    //some stuff
}

Python中是否存在布尔值之类的东西?我似乎在文档中找不到类似的内容.

Is there such a thing as a Boolean in Python? I can't seem to find anything like it in the documentation.

推荐答案

checker = None 

if some_decision:
    checker = True

if checker:
    # some stuff

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

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

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.

这篇关于如何在Python中使用布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆