Skip to content

Commit 302cb41

Browse files
committed
📝 Update boolean values
1 parent 049269e commit 302cb41

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

docs/appendix/checks.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,24 @@ Checks
109109

110110
* Decide whether the following statements are true or false:
111111

112-
* ``1`` → True
113-
* ``0`` → False
114-
* ``-1`` → True
115-
* ``[0]`` → True (List with one task)
116-
* ``1 and 0`` → False
117-
* ``1 > 0 or []`` → True
112+
.. code-block:: console
113+
114+
>>> bool([0])
115+
True
116+
>>> bool(1)
117+
True
118+
bool(0)
119+
False
120+
>>> bool(-1)
121+
True
122+
>>> bool([0]) # Liste mit einem Element
123+
True
124+
>>> bool([]) # Liste mit keinem Element
125+
False
126+
>>> bool(1 and 0)
127+
False
128+
>>> bool(1 > 0 or [])
129+
True
118130
119131
:doc:`/types/sequences-sets/lists`
120132
----------------------------------

docs/oop/types.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ example with a simple inheritance hierarchy makes this clearer:
8989
:func:`python3:isinstance`
9090
determines whether, for example, a class passed to a function or method
9191
is of the expected type.
92+
93+
.. start-issubclass
94+
9295
:func:`python3:issubclass`
9396
determines whether one class is the subclass of another.
9497

98+
.. end-issubclass
99+
95100
.. code-block:: pycon
96101
97102
>>> issubclass(Circle, Form)

docs/types/numbers/bool.rst

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
Boolean values
22
==============
33

4-
Boolean values are used in the following examples:
4+
Boolean values are integers in Python:
55

66
.. code-block:: pycon
77
8-
>>> x = False
9-
>>> x
10-
False
11-
>>> not x
12-
True
8+
>>> issubclass(bool, int)
9+
True
10+
11+
.. include:: ../../oop/types.rst
12+
:start-after: start-issubclass
13+
:end-before: end-issubclass
14+
15+
``True`` behaves like a ``1`` and ``False`` like a ``0``. There are, in fact,
16+
certain situations in which this is very useful.
17+
18+
The functions :py:func:`any` and :py:func:`all` check whether at least one or
19+
all elements in an iterable satisfy the respective condition:
20+
21+
.. code-block:: pycon
22+
23+
>>> numbers = [0, 1, -1, 2, 3]
24+
>>> any(n > 0 for n in numbers)
25+
True
26+
>>> any(n < 0 for n in numbers)
27+
True
28+
>>> all(n > 0 for n in numbers)
29+
False
30+
>>> all(n < 0 for n in numbers)
31+
False
32+
33+
However, neither ``any`` nor ``all`` can tell you how many elements match. To
34+
count the number of matches, you can use :py:func:`sum` instead:
1335

1436
.. code-block:: pycon
1537
16-
>>> y = True * 2
17-
>>> y
18-
2
38+
>>> sum(n > 0 for n in numbers)
39+
3
1940
20-
Apart from their representation as ``True`` and ``False``, Boolean values
21-
behave like the numbers ``1`` (``True``) and ``0`` (``False``).
41+
This involves counting the number of ``True`` values by adding together ``True``
42+
and ``False``.
2243

2344
Checks
2445
------
@@ -29,5 +50,6 @@ Checks
2950
* ``0``
3051
* ``-1``
3152
* ``[0]``
53+
* ``[]``
3254
* ``1 and 0``
3355
* ``1 > 0 or []``

0 commit comments

Comments
 (0)