5.4. Can We Do Math with Boolean Values?¶
The boolean data type is named after the British mathematician George Boole. Boole created Boolean Algebra, which is the basis of all modern computer arithmetic.
This Fun Fact brings up the question, Can we do math with boolean values?
We CAN, but we probably SHOULDN’T. Boolean values are used to make decisions, not solve calculations.
1 2 3 4
print(True*5) print(False*2) print(True + False) print(True * False)
5 0 1 0
5 gives the result
5? What times
2 gives the result
When we use a mathematical operator (
*, etc.) with boolean
values, Python automatically converts
False to the integers
Most of the time, calculations with boolean values are not very useful. Instead, we use booleans to evaluate a condition.