Execute Program

Python for Programmers: Booleans as Integers

Welcome to the Booleans as Integers lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • Internally, Python represents booleans as integers. True is represented by 1, and False by 0. Calling int on a boolean converts it to the corresponding integer.

  • >
    [int(True), int(False)]
    Result:
    [1, 0]Pass Icon
  • >
    True + True + False
    Result:
    2Pass Icon
  • Sometimes you'll see this used (some would say abused) in real-world code. For example, we might have a list of dictionaries, and want to count how many dictionaries are not empty.

  • Remember, an empty dictionary is falsey. Converting it into an bool gives us False, and converting that bool into an int gives us 0.

  • >
    a_dict = {}
    bool(a_dict)
    Result:
    FalsePass Icon
  • >
    a_dict = {}
    int(bool(a_dict))
    Result:
    0Pass Icon
  • >
    a_dict = {
    "foo": "bar"
    }
    int(bool(a_dict))
    Result:
    1Pass Icon
  • Knowing this, we can check each dictionary in an if statement. If it's empty, it'll be false!

  • >
    users = [
    {
    "name": "Amir"
    },
    {
    "name": "Betty"
    },
    {},
    ]
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    user_count = 0
    for user in users:
    if user:
    user_count += 1
    user_count
    Result:
    2Pass Icon
  • Or we can use the "booleans as integers" trick.

  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    sum([bool(user) for user in users])
    Result:
    2Pass Icon
  • Let's review what happened there:

    • bool(user) returned False for empty user dictionaries and True for non-empty dictionaries.
    • We called sum, which ultimately added the values together with +. Since True is 1 and False is 0, calling sum on a list of booleans counts the number of Trues.
  • The second version of our code is certainly shorter than the loop-based version! But optimizing for the fewest lines of code is rarely a good idea. In this case, there's a less-clever solution that's only slightly longer.

  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    len([user for user in users if user])
    Result:
    2Pass Icon
  • Different programmers will have different opinions about the if user in that comprehension. Some think that it's a fine way to check for an empty dictionary. Others prefer a more explicit "is this dictionary empty?" check. The latter group might write this code instead:

  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    len([user for user in users if len(user) > 0])
    Result:
    2Pass Icon
  • All of the comprehension-based solutions here are shorter than our first version with the for loop. Of these four solutions, we'd recommend one of the last two. We want our code to be terse, but we also want it to be clear. The last two solutions both strike a good balance.