Skip to content

Commit cd8ff70

Browse files
BethanyGgithub-actions[bot]
authored andcommitted
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>
1 parent b92d13f commit cd8ff70

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

concepts/bools/about.md

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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`):
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`):
22

33
```python
44
true_variable = True and True
@@ -16,54 +16,103 @@ false_variable = not True
1616
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:
1717

1818
```python
19-
not True and True # => False
20-
not (True and False) # => True
19+
>>>not True and True
20+
False
21+
22+
>>>not (True and False)
23+
True
2124
```
2225

26+
All `boolean operators` are considered lower precedence than Pythons [`comparison operators`][comparisons], such as `==`, `>`, `<`, `is` and `is not`.
27+
2328
## Type Coercion and Truthiness
2429

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`.
2631

27-
Some built-ins already defined to be considered `False`:
32+
A few `built-ins` are always considered `False` by definition:
2833

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`)
36+
- empty _sequences_ and _collections_ (`str`, `list`, `set`, `tuple`, `dict`, `range(0)`)
3237

3338
```python
34-
bool(1) # => True
35-
bool(0) # => False
39+
>>>bool(None)
40+
False
41+
42+
>>>bool(1)
43+
True
44+
>>>bool(0)
45+
False
46+
47+
>>>bool([1,2,3])
48+
True
49+
>>>bool([])
50+
False
51+
52+
>>>bool({"Pig" : 1, "Cow": 3})
53+
True
54+
>>>bool({})
55+
False
3656
```
3757

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.
58+
When a object is used in a _boolean context_, it is evaluated transparently as _truthy_ or _falsey_ using `bool()`:
3959

4060
```python
4161
a = "is this true?"
62+
b = []
63+
64+
# This will print "True", as a non-empty string is considered a "truthy" value
4265
if a:
4366
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+
if not b:
70+
print("False")
4571
```
4672

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.
4874

4975
## How Booleans work under the hood
5076

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_:
5278

5379
```python
54-
1 == True # => True
55-
0 == False # => True
80+
>>>1 == True
81+
True
82+
83+
>>>0 == False
84+
True
5685
```
5786

58-
However, they are still different as noted when checking for a Boolean identity:
87+
However, `bools` are **still different** from `ints`, as noted when compairing using the _identity operator_:
5988

6089
```python
61-
1 is True # => False
62-
0 is False # => False
90+
>>>1 is True
91+
False
92+
93+
>>>0 is False
94+
False
6395
```
6496

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.")
112+
```
66113

67114
[bool-function]: https://docs.python.org/3/library/functions.html#bool
68-
[boolean]: https://docs.python.org/3/library/stdtypes.html#truth
115+
[bool]: https://docs.python.org/3/library/stdtypes.html#truth
69116
[boolean-operators]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
117+
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
118+
[comparing to true in the wrong way]: https://docs.quantifiedcode.com/python-anti-patterns/readability/comparison_to_true.html

concepts/bools/links.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
"url": "https://problemsolvingwithpython.com/04-Data-Types-and-Variables/04.02-Boolean-Data-Type/",
2020
"description": "Problem Solving with Python - Boolean Data Type"
2121
},
22+
{
23+
"url": "https://docs.python.org/3/library/stdtypes.html#comparisons",
24+
"description": "Comparisons in Python"
25+
},
2226
{
2327
"url": "https://docs.quantifiedcode.com/python-anti-patterns/readability/comparison_to_true.html",
2428
"description": "Python Anti-Patterns Comparing things to True in the Wrong Way"

exercises/concept/bools/.docs/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Python represents true and false values with the boolean type. There are only two values: `True` and `False`. These values can be bound to a variable:
1+
Python represents true and false values with the `bool` type. There are only two values: `True` and `False`. These values can be bound to a variable:
22

33
```python
44
true_variable = True

exercises/concept/bools/.meta/design.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ The goal of this exercise is to teach the student how [`bool`](https://github.co
2525

2626
## Prerequisites
2727

28-
- `builtin-types`
28+
- `basics`
2929
- `comparison-operators`
3030

3131
## Resources
3232

3333
- [Boolean Operations — and, or, not](https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not)
3434
- [Built-in Functions - bool](https://docs.python.org/3/library/functions.html#bool)
3535
- [Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth)
36+
- [Comparisons](https://docs.python.org/3/library/stdtypes.html#comparisons)

0 commit comments

Comments
 (0)