You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bools: Add Links and Clean Up Concept Doc (exercism#2533)
* Added concept links and additional information to bools concept.
* Edited intro to refer to bool type not boolean.
* Removed built-in types requirements and added basics and comparison operators.
* [CI] Format code
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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`):
1
+
Python represents True and False values with the [bool][bool] type. There are only two boolean values: `True` and `False`. These values can be assigned to a variable and combined with the[boolean operators][boolean-operators] (`and`, `or`, `not`):
2
2
3
3
```python
4
4
true_variable =TrueandTrue
@@ -16,54 +16,103 @@ false_variable = not True
16
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
17
18
18
```python
19
-
notTrueandTrue# => False
20
-
not (TrueandFalse) # => True
19
+
>>>notTrueandTrue
20
+
False
21
+
22
+
>>>not (TrueandFalse)
23
+
True
21
24
```
22
25
26
+
All `boolean operators` are considered lower precedence than Pythons [`comparison operators`][comparisons], such as `==`, `>`, `<`, `is` and `is not`.
27
+
23
28
## Type Coercion and Truthiness
24
29
25
-
The [`bool` function][bool-function] converts any type to a Boolean value. By default all values return `True` unless defined to return `False`.
30
+
The bool function ([`bool()`][bool-function]) converts any object to a Boolean value. By default all objects return `True` unless defined to return `False`.
26
31
27
-
Some built-ins already defined to be considered `False`:
32
+
A few `built-ins` are always considered `False` by definition:
28
33
29
-
-constants like`None` and `False`
30
-
- zero of any _numeric type_
31
-
- empty _sequences_ and _collections_
34
+
-the constants`None` and `False`
35
+
- zero of any _numeric type_ (`int`, `float`, `complex`, `decimal`, or `fraction`)
When a value is used in a boolean context, it is used as a _truthy_ or _falsey_value by transparently using the `bool` function.
58
+
When a object is used in a _boolean context_, it is evaluated transparently as _truthy_ or _falsey_ using `bool()`:
39
59
40
60
```python
41
61
a ="is this true?"
62
+
b = []
63
+
64
+
# This will print "True", as a non-empty string is considered a "truthy" value
42
65
if a:
43
66
print("True")
44
-
# => This will print "True", as a non-empty string is a truthy value
67
+
68
+
# This will print "False", as an empty list is considered a "falsey" value
69
+
ifnot b:
70
+
print("False")
45
71
```
46
72
47
-
Classes may define how they are evaluated in truthy situations if they override and implement a `__bool__` method.
73
+
Classes may define how they are evaluated in truthy situations if they override and implement a `__bool__()` method, and/or a `__len__()` method.
48
74
49
75
## How Booleans work under the hood
50
76
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:
77
+
The `bool` type is implemented as a _sub-type_ of _int_. That means that `True` is _numerically equal_ to `1` and `False` is _numerically equal_ to `0`. This is observable when comparing them using an _equality operator_:
52
78
53
79
```python
54
-
1==True# => True
55
-
0==False# => True
80
+
>>>1==True
81
+
True
82
+
83
+
>>>0==False
84
+
True
56
85
```
57
86
58
-
However, they are still differentas noted when checking for a Boolean identity:
87
+
However, `bools` are **still different** from `ints`, as noted when compairing using the _identity operator_:
59
88
60
89
```python
61
-
1isTrue# => False
62
-
0isFalse# => False
90
+
>>>1isTrue
91
+
False
92
+
93
+
>>>0isFalse
94
+
False
63
95
```
64
96
65
-
> Note: in python >= 3.8, using a literal value on the left side of `is` will raise a warning.
97
+
> Note: in python >= 3.8, using a literal (such as 1, '', [], or {}) on the left side of `is` will raise a warning.
98
+
99
+
It is considered a [Python anti-pattern][comparing to true in the wrong way] to use the equality operator to comaire a booleand variable to `True` or `False`. Instead, the identity operator `is` should be used:
100
+
101
+
```python
102
+
103
+
flag =True
104
+
105
+
# Not "Pythonic"
106
+
if flag ==True:
107
+
print("This works, but it's not considered Pythonic.")
108
+
109
+
# A better way
110
+
if flag:
111
+
print("Pythonistas prefer this pattern as more Pythonic.")
0 commit comments