Python ยท Chapter 10 of 45
Python Booleans
The `bool` type has exactly two values: `True` and `False` (capitalised).
Most values also have a 'truthiness': empty containers (`[]`, `{}`, `""`), zero and `None` are falsy; everything else is truthy.
Boolean operators
`and`, `or`, `not` combine booleans. `and` returns the first falsy value; `or` returns the first truthy value.
Comparisons
`==`, `!=`, `<`, `>`, `<=`, `>=` return booleans. Chained comparisons like `1 < x < 10` work naturally.
Example 1 (python)
print(bool(0))
print(bool([]))
print(bool("hi"))Output
False
False
Truebool() converts using truthiness rules.
Example 2 (python)
x = 15
print(x > 10 and x < 20)
print(10 < x < 20)Output
True
TrueBoth check the same range.
Key points
- Booleans are `True` and `False` (capital first letter).
- Empty containers, 0, and None are falsy.
- `and`, `or`, `not` combine booleans.
- Chained comparisons: `1 < x < 10`.
๐ก Note: In Python, `True` and `False` are actually 1 and 0 under the hood โ you can use them in math, but rarely should.
