Skip to content

Commit e518493

Browse files
ErikSchierboomBethanyGgithub-actions[bot]
committed
bools - replace about.md file with concept files (exercism#2351)
* Pre-populate bools concept's about.md file from after.md file * Pre-populate bools concept's links.json file from after.md file * bools - Remove after.md document * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * Update languages/concepts/bools/links.json * [CI] Format code Co-authored-by: BethanyG <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3e41c7c commit e518493

File tree

3 files changed

+99
-71
lines changed

3 files changed

+99
-71
lines changed

concepts/bools/about.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,69 @@
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

concepts/bools/links.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
[]
1+
[
2+
{
3+
"url": "https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values",
4+
"description": "boolean values"
5+
},
6+
{
7+
"url": "https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not",
8+
"description": "boolean-operators"
9+
},
10+
{
11+
"url": "https://docs.python.org/3/library/stdtypes.html#truth",
12+
"description": "Truth Value Testing"
13+
},
14+
{
15+
"url": "https://docs.python.org/3/library/functions.html#bool",
16+
"description": "bool() function"
17+
},
18+
{
19+
"url": "https://problemsolvingwithpython.com/04-Data-Types-and-Variables/04.02-Boolean-Data-Type/",
20+
"description": "Problem Solving with Python - Boolean Data Type"
21+
},
22+
{
23+
"url": "https://docs.quantifiedcode.com/python-anti-patterns/readability/comparison_to_true.html",
24+
"description": "Python Anti-Patterns Comparing things to True in the Wrong Way"
25+
},
26+
{
27+
"url": "https://www.python.org/dev/peps/pep-0285/",
28+
"description": "PEP285 - Adding a bool type"
29+
}
30+
]

exercises/concept/bools/.docs/after.md

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)