|
1 |
| -TODO: add information on bools concept |
| 1 | +Python represents True and False values with the [boolean][boolean] type. There are only two [boolean][boolean] values: _True_ and _False_. These values can be assigned to a variable and combined with [boolean operators][boolean-operators] (`and`, `or`, `not`): |
| 2 | + |
| 3 | +```python |
| 4 | +true_variable = True and True |
| 5 | +false_variable = True and False |
| 6 | + |
| 7 | +true_variable = False or True |
| 8 | +false_variable = False or False |
| 9 | + |
| 10 | +true_variable = not False |
| 11 | +false_variable = not True |
| 12 | +``` |
| 13 | + |
| 14 | +[Boolean operators][boolean-operators] use _short-circuit evaluation_, which means that expression on the right-hand side of the operator is only evaluated if needed. |
| 15 | + |
| 16 | +Each of the operators has a different precedence, where `not` is evaluated before `and` and `or`. Brackets can be used to evaluate one part of the expression before the others: |
| 17 | + |
| 18 | +```python |
| 19 | +not True and True # => False |
| 20 | +not (True and False) # => True |
| 21 | +``` |
| 22 | + |
| 23 | +## Type Coercion and Truthiness |
| 24 | + |
| 25 | +The [`bool` function][bool-function] converts any type to a Boolean value. By default all values return `True` unless defined to return `False`. |
| 26 | + |
| 27 | +Some built-ins already defined to be considered `False`: |
| 28 | + |
| 29 | +- constants like `None` and `False` |
| 30 | +- zero of any _numeric type_ |
| 31 | +- empty _sequences_ and _collections_ |
| 32 | + |
| 33 | +```python |
| 34 | +bool(1) # => True |
| 35 | +bool(0) # => False |
| 36 | +``` |
| 37 | + |
| 38 | +When a value is used in a boolean context, it is used as a _truthy_ or _falsey_ value by transparently using the `bool` function. |
| 39 | + |
| 40 | +```python |
| 41 | +a = "is this true?" |
| 42 | +if a: |
| 43 | + print("True") |
| 44 | +# => This will print "True", as a non-empty string is a truthy value |
| 45 | +``` |
| 46 | + |
| 47 | +Classes may define how they are evaluated in truthy situations if they override and implement a `__bool__` method. |
| 48 | + |
| 49 | +## How Booleans work under the hood |
| 50 | + |
| 51 | +The Boolean type is a _sub-type_ of the _int_ type. `True` is numerically equal to `1`. `False` is numerically equal to `0`. This is observable when comparing them: |
| 52 | + |
| 53 | +```python |
| 54 | +1 == True # => True |
| 55 | +0 == False # => True |
| 56 | +``` |
| 57 | + |
| 58 | +However, they are still different as noted when checking for a Boolean identity: |
| 59 | + |
| 60 | +```python |
| 61 | +1 is True # => False |
| 62 | +0 is False # => False |
| 63 | +``` |
| 64 | + |
| 65 | +> Note: in python >= 3.8, using a literal value on the left side of `is` will raise a warning. |
| 66 | +
|
| 67 | +[bool-function]: https://docs.python.org/3/library/functions.html#bool |
| 68 | +[boolean]: https://docs.python.org/3/library/stdtypes.html#truth |
| 69 | +[boolean-operators]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not |
0 commit comments