bool is a number
In the previous lesson, comparisons gave True or False. Now let's see what these values can do in arithmetic.
Picture a vote in a classroom. The teacher asks a question, and each student either raises a hand or does not. A raised hand is a one, a lowered hand is a zero. Add up all the hands, and you know how many people said "yes".
Python works the same way. The bool type in Python is a kind of int: True behaves like 1, and False like 0.
print(True == 1, False == 0) # True True
print(True + True, True * 10) # 2 10
print(int(True), int(False)) # 1 0
print(True + 0.5) # 1.5Let's take each line:
True == 1andFalse == 0giveTrue: in a comparison with a number, they are the same thing.True + Trueis2, andTrue * 10is10.int(True)andint(False)turn them into1and0explicitly.True + 0.5gives1.5of typefloat, as adding anintto afloatdoes.
Here the hands analogy ends. True still prints as a word: print(True) prints True, not 1. It becomes a number only in arithmetic and in a comparison with a number. So True == 1 gives True, although the values have different types.
Counting with comparisons
A true comparison equals 1, and a false one equals 0. So the sum of several comparisons equals the number of true ones among them. That is how to count how many of several conditions hold without checking each one separately:
def count_positive(a, b, c):
return (a > 0) + (b > 0) + (c > 0)
print(count_positive(5, -2, 7)) # 2
print(count_positive(-1, 0, -3)) # 0Let's go through the first call step by step:
5 > 0givesTrue: the first raised hand.-2 > 0givesFalse: the hand stays down.7 > 0givesTrue: the second hand.True + False + Trueis2, and the function returns2.
In the second call, no number is greater than zero, so the sum is 0. Note that zero does not count as positive: 0 > 0 gives False.
Comparing such a sum with a number tells whether at least two of three conditions hold: count_positive(a, b, c) >= 2. The sum itself has type int, not bool: True + True is 2.
The same trick works with multiplication: price * (count > 10) equals price if there are more than ten items, and 0 otherwise. Such code is short, but its meaning takes a moment to see. So the trick is used mostly for counting.
Checking the type
The type function names the exact type of a value. isinstance(x, t) tells whether x belongs to type t or to a kind of it:
print(type(True)) # <class 'bool'>
print(isinstance(True, int)) # True
print(isinstance(1, bool)) # FalseLet's read the results:
- The exact type of
Trueisbool. - But for
isinstanceit is also anint, sinceboolis a kind ofint. - The reverse does not hold:
1is only anint.
So a function that expects a number accepts True without an error and counts it as one. Interviewers like to ask this: "What is True + True, and why?" The answer is 2, because bool is a kind of int.
Common mistake
A sum of comparisons is written without parentheses:
a = 5
b = -2
count = a > 0 + b > 0 # ✗
print(count) # FalseThere is no error, but 1 was expected. + runs before comparisons, so the expression reads as the chain a > (0 + b) > 0, that is, 5 > -2 > 0. Its second part is false, so the whole chain gives False. Put each comparison in parentheses: (a > 0) + (b > 0) gives 1.
Key points
boolis a kind ofint: in arithmetic,Trueis1, andFalseis0.- A sum of parenthesized comparisons counts how many of them hold.
isinstance(True, int)givesTrue, buttype(True)isbool, andTrueprints as a word.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.