Booleans

Boolean variables can either be True or False. They are good for recording whether something has or hasn't happened, or whether something needs to be done or not.

Technically True and False are both keywords in Python. They are kinds of numbers: True has the value 1, and False has the value 0.
Note: This is different to other languages. In C-like languages true is equivalent to -1 (all bits set in two's complement arithmetic)

Testing booleans

When testing boolean variables you don't need to compare them with True and False, just use them directly:


    if keyFound and chestLocked:
        "The key turns easily in the lock and you get the chest open."
        $ chestLocked = False

Instead of this:


    if keyFound == True and chestLocked == True:
        "The key turns easily in the lock and you get the chest open."
        $ chestLocked = False

Adding booleans

Because True and False behave like the numbers 1 and 0 you can add them together to see how many variables are set to True:


    $ count = keyFound + paperFound + daggerFound
    if count == 0:
        "You haven't found any of the things you need yet."
    elif count == 3:
        "You have everything you need."
    else:
        "You have [count] of the 3 things you need."