June 2017
Beginner
352 pages
8h 39m
English
The bool type represents logical states and plays an important role in several of Python's control flow structures, as we'll see shortly. As you would expect there are two bool values, True and False, both spelled with initial capitals:
>>> TrueTrue>>> FalseFalse
There is also a bool constructor which can be used to convert from other types to bool. Let's look at how it works. For ints, zero is considered falsey and all other values truthy:
>>> bool(0)False>>> bool(42)True>>> bool(-1)True
We see the same behavior with floats where only zero is considered falsey:
>>> bool(0.0)False>>> bool(0.207)True>>> bool(-1.117)True>>> bool(float("NaN"))True
When converting from collections, such as strings or lists, only empty collections are treated ...
Read now
Unlock full access