How do I use a Boolean in Python?

Federer picture Federer · Nov 17, 2009 · Viewed 626.5k times · Source

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.

Answer

Bastien Léonard picture Bastien Léonard · Nov 17, 2009
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.