Search⌘ K
AI Features

What's Wrong With Booleans?

Discover how Python treats booleans as subclasses of integers, why True and False correspond to 1 and 0, and learn to predict outcomes in code involving booleans and integers. This lesson helps clarify common misconceptions and avoid mistakes related to Python's boolean behavior.

We'll cover the following...

Booleans were supposed to be straightforward: true or false. But, are they?

1.

Below is a simple example to count the number of booleans and integers in an iterable of mixed data types.

Python 3.5
mixed_list = [False, 1.0, "some_string", 3, True, [], False]
integers_found_so_far = 0
booleans_found_so_far = 0
for item in mixed_list:
if isinstance(item, int):
integers_found_so_far += 1
elif isinstance(item, bool):
booleans_found_so_far += 1
print(integers_found_so_far)
print(booleans_found_so_far)

2.

What do you think the output of the following code will be?

Python 3.5
some_bool = True
print("ftw" * some_bool)
some_bool = False
print("ftw" * some_bool)
print("--------")

3.

Can you predict the ...