Skip to content

Commit 174cd9d

Browse files
committed
✏️ Minor improvements
* Typos * Wording * Reformat pyproject.toml files * Rearrange checks * Measure times for cache decorator * Do not use id for integer comparisons * Add dict comprehension
1 parent a0e7922 commit 174cd9d

File tree

16 files changed

+256
-129
lines changed

16 files changed

+256
-129
lines changed

docs/appendix/checks.rst

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ Checks
272272

273273
.. code-block:: pycon
274274
275-
>>> sheet = {}
276-
>>> sheet[("A", 0)] = 1
277-
>>> sheet[("A", 1)] = 2
278-
>>> sheet[("B", 0)] = 3
279-
>>> sheet[("B", 1)] = 4
280-
>>> print(sheet[("A", 1)])
275+
>>> tabular = {}
276+
>>> tabular[("A", 0)] = 1
277+
>>> tabular[("A", 1)] = 2
278+
>>> tabular[("B", 0)] = 3
279+
>>> tabular[("B", 1)] = 4
280+
>>> print(tabular[("A", 1)])
281281
2
282282
283283
* How can you remove all duplicates from a list without changing the order of the
@@ -501,7 +501,7 @@ Checks
501501
>>> pos
502502
[0, 1, 2, 3]
503503
504-
* How would you count the total number of negative numbers in the list ``[-[1,
504+
* How would you count the total number of negative numbers in the list ``[[-1,
505505
0, 1], [-1, 1, 3], [-2, 0, 2]]``?
506506

507507
.. code-block:: pycon
@@ -621,15 +621,11 @@ Checks
621621

622622
.. code-block:: pycon
623623
624-
>> def my_func(*params):
625-
... for i in reversed(params):
626-
... print(i)
627-
...
628-
>>> my_func(1, 2, 3, 4)
629-
4
630-
3
631-
2
632-
1
624+
>>> values = input("Values separated by commas: ")
625+
Values separated by commas: 1,3,2,4
626+
>>> value_list = values.split(",")
627+
>>> reverse(value_list)
628+
['4', '3', '2', '1']
633629
634630
:doc:`/functions/variables`
635631
---------------------------

docs/control-flow/boolean.rst

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,49 @@ considered ``True``.
1818
>>> x == y
1919
True
2020
21-
However, you should never compare calculated floating point numbers with
22-
each other:
21+
.. warning::
22+
However, you should never directly compare calculated floating point
23+
numbers:
2324

24-
.. code-block:: pycon
25+
.. code-block:: pycon
2526
26-
>>> u = 0.6 * 7
27-
>>> v = 0.7 * 6
28-
>>> u == v
29-
False
30-
>>> u
31-
4.2
32-
>>> v
33-
4.199999999999999
27+
>>> u = 0.6 * 7
28+
>>> v = 0.7 * 6
29+
>>> u == v
30+
False
31+
>>> u
32+
4.2
33+
>>> v
34+
4.199999999999999
35+
36+
Instead, you can use :func:`math.isclose`:
37+
38+
.. code-block:: pycon
39+
40+
>>> import math
41+
>>> math.isclose(u, v)
42+
True
43+
44+
Alternatively, you can also use :func:`round`:
45+
46+
.. code-block:: pycon
47+
48+
>>> round(u, 2) == round(v, 2)
49+
True
50+
51+
.. warning::
52+
Integers smaller than -5 or larger than 256 are recreated each time, but
53+
integers in between are pre-instantiated by the interpreter and reused.
54+
Therefore, :py:func:`id` should not be used for comparisons of integers:
55+
56+
.. code-block:: pycon
57+
58+
>>> x = 256
59+
>>> y = 257
60+
>>> id(x) == id(256)
61+
True
62+
>>> id(y) == id(257)
63+
False
3464
3565
``is``, ``is not``, ``in``, ``not in``
3666
checks the identity:

docs/control-flow/loops.rst

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ flow control is continued with ``x`` being set to the next entry in the list.
8585
After the first matching integer is found, the loop is terminated with the
8686
``break`` statement.
8787

88-
Loops with an index
89-
-------------------
88+
``for`` Loops with an index
89+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
9090

9191
You can also output the index in a ``for`` loop, for example with
9292
:py:func:`enumerate()`:
@@ -102,7 +102,7 @@ You can also output the index in a ``for`` loop, for example with
102102
2 Lists
103103
104104
List Comprehensions
105-
-------------------
105+
~~~~~~~~~~~~~~~~~~~
106106

107107
A list is usually generated as follows:
108108

@@ -153,14 +153,65 @@ appended to the end of the expression:
153153
>>> squares
154154
[16, 25, 36, 49]
155155
156+
Dict Comprehensions
157+
~~~~~~~~~~~~~~~~~~~
158+
159+
:doc:`../types/sequences-sets/lists` can be converted into :doc:`../types/dicts`
160+
as follows:
161+
162+
.. code-block:: pycon
163+
164+
>>> toc = {}
165+
>>> for index, title in enumerate(data_types):
166+
... toc[index] = title
167+
...
168+
>>> toc
169+
{0: 'Data types', 1: 'Numbers', 2: 'Lists'}
170+
171+
Mit Dict Comprehensions vereinfacht sich dies:
172+
173+
.. code-block:: pycon
174+
175+
>>> toc = {index: value for index, value in enumerate(data_types)}
176+
>>> toc
177+
{0: 'Data types', 1: 'Numbers', 2: 'Lists'}
178+
179+
Das allgemeine Format für Dict Comprehensions ist:
180+
181+
:samp:`{NEW_DICT} = \{{EXPRESSION}, {MEMBER} in {ITERABLE}\}`
182+
183+
Change a ``Collection``
184+
~~~~~~~~~~~~~~~~~~~~~~~
185+
186+
Modifying a ``collection`` while iterating over it can be difficult. Therefore,
187+
a copy of the ``collection`` is often modified instead:
188+
189+
.. code-block:: pycon
190+
191+
>>> for index, title in data_types.items():
192+
... if index == 0:
193+
... del data_types[index]
194+
...
195+
Traceback (most recent call last):
196+
File "<python-input-2>", line 1, in <module>
197+
for index, title in data_types.items():
198+
~~~~~~~~~~~~~~~~^^
199+
RuntimeError: dictionary changed size during iteration
200+
>>> for index, title in data_types.copy().items():
201+
... if index == 0:
202+
... del data_types[index]
203+
...
204+
>>> data_types
205+
{1: 'Numbers', 2: 'Lists'}
206+
156207
Checks
157208
------
158209

159210
* Removes all negative numbers from the list ``x = [ -2, -1, 0, 1, 2, 3]``.
160211

161212
* Which list comprehension would you use to achieve the same result?
162213

163-
* How would you count the total number of negative numbers in the list ``[-[1,
214+
* How would you count the total number of negative numbers in the list ``[[-1,
164215
0, 1], [-1, 1, 3], [-2, 0, 2]]``?
165216

166217
* Creates a generator that only returns odd numbers from 1 to 10.

docs/explore.rst

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,41 @@ Exploring Python
44
Whether you use :ref:`idle` or the :ref:`interactive_shell`, there are some
55
useful functions to explore Python.
66

7+
.. code-block:: pycon
8+
9+
>>> x = 4.2
10+
11+
``type()``
12+
----------
13+
14+
With :py:func:`type`, you can display the object type, for example:
15+
16+
.. code-block:: pycon
17+
18+
>>> type(x)
19+
<class 'float'>
20+
721
.. _help:
822

923
``help()``
1024
----------
1125

12-
``help()`` has two different modes. When you type ``help()``, you call the help
13-
system, which you can use to get information about modules, keywords, and other
14-
topics. When you are in the help system, you will see a prompt with ``help>``.
15-
You can now enter a module name, for example ``float``, to search the `Python
16-
documentation <https://docs.python.org/>`_ for that type.
26+
:py:func:`help` has two different modes. When you type :func:`help`, you call
27+
the help system, which you can use to get information about modules, keywords,
28+
and other topics. When you are in the help system, you will see a prompt with
29+
``help>``. You can now enter a module name, for example ``float``, to search the
30+
`Python documentation <https://docs.python.org/>`_ for that type.
1731

18-
``help()`` is part of the :doc:`pydoc <python3:library/pydoc>` library, which
32+
:func:`help` is part of the :doc:`pydoc <python3:library/pydoc>` library, which
1933
provides access to the documentation built into Python libraries. Since every
2034
Python installation comes with full documentation, you have all the
2135
documentation at your fingertips even offline.
2236

23-
Alternatively, you can use ``help()`` more specifically by passing a type or
37+
Alternatively, you can use :func:`help` more specifically by passing a type or
2438
variable name as a parameter, for example:
2539

2640
.. code-block:: pycon
2741
28-
>>> x = 4.2
2942
>>> help(x)
3043
Help on float object:
3144
@@ -39,14 +52,27 @@ variable name as a parameter, for example:
3952
| __abs__(self, /)
4053
| abs(self)
4154
...
55+
| is_integer(self, /)
56+
| Return True if the float is an integer.
57+
...
4258
4359
For example, you will learn that ``x`` is of type ``float`` and has a function
44-
:func:`__add__` that you can use with dot notation:
60+
:func:`is_integer` that you can use with dot notation:
61+
62+
.. code-block:: pycon
63+
64+
>>> x.is_integer()
65+
False
66+
67+
``id()``
68+
--------
69+
70+
:py:func:`id` specifies the identification number of an object, for example:
4571

4672
.. code-block:: pycon
4773
48-
>>> x.__add__(1)
49-
5.2
74+
>>> id(x)
75+
4304262800
5076
5177
``dir()``
5278
---------

docs/functions/decorators.rst

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,25 @@ as decorators, such as:
7878
.. code-block:: pycon
7979
:linenos:
8080
81-
>>> from functools import cache
82-
>>> @cache
81+
>>> import timeit
82+
... from functools import cache
83+
... @cache
8384
... def factorial(n):
8485
... return n * factorial(n - 1) if n else 1
85-
...
86-
>>> factorial(8)
87-
40320
88-
>>> factorial(10)
89-
3628800
90-
91-
Line 6
92-
Since there is no previously stored result, nine recursive calls are
93-
made.
94-
Line 8
95-
makes only two new calls, as the other results come from the cache.
86+
... timeit.timeit("factorial(8)", globals=globals())
87+
0.02631620899774134
88+
89+
Line 1
90+
imports the :mod:`timeit` module for measuring execution time.
91+
Line 2
92+
imports :func:`functools.cache`.
93+
Line 5
94+
The ``@cache`` decorator is used to store intermediate results, which
95+
can then be reused. In our case, the execution speed is increased
96+
approximately tenfold.
97+
Line 10
98+
:func:`timeit.timeit` measures the time of a call. Unless otherwise
99+
specified, the call is made one million times.
96100

97101
:func:`functools.wraps`
98102
This decorator makes the wrapper function look like the original function

docs/functions/params.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Python offers flexible mechanisms for passing :term:`arguments <argument>` to
2424
...
2525
>>> func2(5, w=6)
2626
45
27-
>>> def func3(u, v=1, w=1, *tup):
28-
... print((u, v, w) + tup)
27+
>>> def func3(u, v=1, w=1, *args):
28+
... print((u, v, w) + args)
2929
...
3030
>>> func3(7)
3131
(7, 1, 1)

0 commit comments

Comments
 (0)