Skip to content

Commit a7ce07c

Browse files
committed
🎨 Rearrange sequences and sets
1 parent 0611fcb commit a7ce07c

File tree

12 files changed

+99
-93
lines changed

12 files changed

+99
-93
lines changed

docs/appendix/checks.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ Checks
116116
* ``1 and 0`` → False
117117
* ``1 > 0 or []`` → True
118118

119-
:doc:`/types/lists`
120-
-------------------
119+
:doc:`/types/sequences-sets/lists`
120+
----------------------------------
121121

122122
* What does :func:`len` return for each of the following cases:
123123

@@ -167,7 +167,7 @@ Checks
167167
.. note::
168168
This code only removes the first occurrence of ``i``. To remove all
169169
occurrences of ``i`` from the list, the list could be converted to the
170-
:doc:`set </types/sets>` type, for example:
170+
:doc:`set </types/sequences-sets/sets>` type, for example:
171171

172172
.. code-block:: pycon
173173
@@ -198,8 +198,8 @@ Checks
198198
199199
* What other options could you have besides explicitly checking the type?
200200

201-
:doc:`/types/tuples`
202-
--------------------
201+
:doc:`/types/sequences-sets/tuples`
202+
-----------------------------------
203203

204204
* Explain why the following operations cannot be applied to the tuple ``t``:
205205

@@ -216,8 +216,8 @@ Checks
216216
217217
>>> sorted(t)
218218
219-
:doc:`/types/sets`
220-
------------------
219+
:doc:`/types/sequences-sets/sets`
220+
---------------------------------
221221

222222
* How many elements does a set have if it is formed from the following list
223223
``[4, 2, 3, 2, 1]``?
@@ -267,8 +267,8 @@ Checks
267267
>>> d[("Veit", "Tim", "Monique")] = None
268268
269269
* You can use a :doc:`Dictionary </types/dicts>` like a spreadsheet table by
270-
using :doc:`/types/tuples` as key row and column values. Write sample code to
271-
add and retrieve values.
270+
using :doc:`/types/sequences-sets/tuples` as key row and column values. Write
271+
sample code to add and retrieve values.
272272

273273
.. code-block:: pycon
274274

docs/control-flows/loops.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ Line 5
5959
The ``for`` loop is simple but powerful because it can iterate over any iterable
6060
type, such as a list or a tuple. Unlike many other languages, the ``for`` loop
6161
in Python iterates over every element in a sequence for example a :doc:`list
62-
<../types/lists>` or a :doc:`tuple <../types/tuples>`), which makes it more like
63-
a foreach loop. The following loop uses the `Modulo
62+
<../types/sequences-sets/lists>` or a :doc:`tuple
63+
<../types/sequences-sets/tuples>`), which makes it more like a foreach loop. The
64+
following loop uses the `Modulo
6465
<https://en.wikipedia.org/wiki/Modulo_operation>`_ operator ``%`` as a condition
6566
for the first occurrence of an integer divisible by ``5``:
6667

@@ -138,9 +139,10 @@ Each list comprehension in Python contains three elements:
138139
is the object or the value in an :samp:`{ITERABLE}`. In the example above,
139140
the value is ``i``.
140141
:samp:`{ITERABLE}`
141-
is a :doc:`list <../types/lists>`, a :doc:`set <../types/sets>`, a generator
142-
or another object that can return its elements individually. In the example
143-
above, the iterable is ``range(8)``.
142+
is a :doc:`list <../types/sequences-sets/lists>`, a :doc:`set
143+
<../types/sequences-sets/sets>`, a generator or another object that can
144+
return its elements individually. In the example above, the iterable is
145+
``range(8)``.
144146

145147
You can also use optional conditions with list comprehensions, which are usually
146148
appended to the end of the expression:

docs/functions/params.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ Variable number of arguments
150150

151151
Python functions can also be defined to handle a variable number of arguments.
152152
This is possible in two ways. One method collects an unknown number of arguments
153-
in a :doc:`list </types/lists>`. The other method can collect an arbitrary
154-
number of arguments passed with a keyword that has no correspondingly named
155-
parameter in the function parameter list in a :doc:`dict </types/dicts>`.
153+
in a :doc:`list </types/sequences-sets/lists>`. The other method can collect an
154+
arbitrary number of arguments passed with a keyword that has no correspondingly
155+
named parameter in the function parameter list in a :doc:`dict </types/dicts>`.
156156

157157
For an indeterminate number of positional arguments, prefixing the function’s
158158
final parameter name with a ``*`` causes all excess non-keyword arguments in a
@@ -223,12 +223,13 @@ Mutable objects as arguments
223223
----------------------------
224224

225225
Arguments are passed by object reference. The parameter becomes a new reference
226-
to the object. With immutable objects such as :doc:`/types/tuples`,
227-
:doc:`/types/strings/index` and :doc:`/types/numbers/index`, what is done with a
228-
parameter has no effect outside the function. However, if you pass a mutable
229-
object, such as a :doc:`/types/lists`, a :doc:`/types/dicts` or a class
230-
instance, any change to the object changes what the argument refers to outside
231-
the function. Reassigning the parameter has no effect on the argument.
226+
to the object. With immutable objects such as
227+
:doc:`/types/sequences-sets/tuples`, :doc:`/types/strings/index` and
228+
:doc:`/types/numbers/index`, what is done with a parameter has no effect
229+
outside the function. However, if you pass a mutable object, such as a
230+
:doc:`/types/sequences-sets/lists`, a :doc:`/types/dicts` or a class instance,
231+
any change to the object changes what the argument refers to outside the
232+
function. Reassigning the parameter has no effect on the argument.
232233

233234
.. code-block:: pycon
234235

docs/libs/batteries.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
In Python, a library can consist of several components, including built-in data
55
types and constants that can be used without an import statement, such as
6-
:doc:`/types/numbers/index` and :doc:`/types/lists`, as well as some built-in
7-
:doc:`/functions/index` and :doc:`/control-flows/exceptions`. The largest part
8-
of the library is an extensive collection of :doc:`/modules/index`. If you have
9-
Python installed, there are also several libraries available for you to use.
6+
:doc:`/types/numbers/index` and :doc:`/types/sequences-sets/lists`, as well as
7+
some built-in :doc:`/functions/index` and :doc:`/control-flows/exceptions`. The
8+
largest part of the library is an extensive collection of :doc:`/modules/index`.
9+
If you have Python installed, there are also several libraries available for you
10+
to use.
1011

1112
* :ref:`data-types`
1213
* :ref:`files-storage`
@@ -61,7 +62,7 @@ Modules for data types
6162
Modules for numbers
6263
~~~~~~~~~~~~~~~~~~~
6364

64-
.. include:: ../types/numbers.rst
65+
.. include:: ../types/numbers/index.rst
6566
:start-after: number-modules:
6667
:end-before: end-number-modules:
6768

docs/save-data/pickle.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ this state in a file called ``data.pickle`` as follows:
4040
instances of user-defined classes. :py:func:`pickle.dump` saves everything.
4141

4242
The pickle module can store almost anything in this way. It can handle
43-
:doc:`/types/numbers/index`, :doc:`/types/lists`, :doc:`/types/tuples`,
44-
:doc:`/types/dicts`, :doc:`/types/strings/index` and pretty much anything
45-
made up of these object types, including all class instances. It also
46-
handles shared objects, cyclic references and other complex storage
47-
structures correctly by storing shared objects only once and restoring them
48-
as shared objects, not as identical copies.
43+
:doc:`/types/numbers/index`, :doc:`/types/sequences-sets/lists`,
44+
:doc:`/types/sequences-sets/tuples`, :doc:`/types/dicts`,
45+
:doc:`/types/strings/index` and pretty much anything made up of these object
46+
types, including all class instances. It also handles shared objects,
47+
cyclic references and other complex storage structures correctly by storing
48+
shared objects only once and restoring them as shared objects, not as
49+
identical copies.
4950

5051
#. Loading pickled data:
5152

docs/types/dicts.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Dictionaries
44
Python’s built-in dictionary data type provides associative array functionality
55
implemented using hash tables. The built-in ``len`` function returns the number
66
of key-value pairs in a dictionary. The ``del`` statement can be used to delete
7-
a key-value pair. As with :doc:`lists` , several dictionary methods
8-
(:py:meth:`clear <dict.clear>`, :py:meth:`copy <dict.copy>`, :py:meth:`get
9-
<dict.get>`, :py:meth:`items <dict.items>`, :py:meth:`keys <dict.keys>`,
10-
:py:meth:`update <dict.update>` and :py:meth:`values <dict.values>`) are
11-
available.
7+
a key-value pair. As with :doc:`sequences-sets/lists` , several dictionary
8+
methods (:py:meth:`clear <dict.clear>`, :py:meth:`copy <dict.copy>`,
9+
:py:meth:`get <dict.get>`, :py:meth:`items <dict.items>`, :py:meth:`keys
10+
<dict.keys>`, :py:meth:`update <dict.update>` and :py:meth:`values
11+
<dict.values>`) are available.
1212

1313
.. code-block:: pycon
1414
@@ -25,18 +25,18 @@ available.
2525
'nicht vorhanden'
2626
2727
Keys must be of immutable type, including :doc:`numbers/index`,
28-
:doc:`strings/index` and :doc:`tuples`.
28+
:doc:`strings/index` and :doc:`sequences-sets/tuples`.
2929

3030
.. warning::
3131
Even if you can use different key types in a dictionary, you should avoid
3232
this, as it not only makes it more difficult to read, but also sorting is
3333
also made more difficult.
3434

35-
Values can be any type of object, including mutable types such as :doc:`lists`
36-
and :doc:`dicts`. If you try to access the value of a key that is not in the
37-
dictionary, a ``KeyError`` exception is thrown. To avoid this error, the
38-
dictionary method ``get`` optionally returns a custom value if a key is not
39-
contained in a dictionary.
35+
Values can be any type of object, including mutable types such as
36+
:doc:`sequences-sets/lists` and :doc:`dicts`. If you try to access the value of
37+
a key that is not in the dictionary, a ``KeyError`` exception is thrown. To
38+
avoid this error, the dictionary method ``get`` optionally returns a custom
39+
value if a key is not contained in a dictionary.
4040

4141
``setdefault``
4242
--------------
@@ -134,8 +134,8 @@ Checks
134134
>>> d[("Veit", "Tim", "Monique")] = None
135135
136136
* You can use a :doc:`Dictionary </types/dicts>` like a spreadsheet table by
137-
using :doc:`/types/tuples` as key row and column values. Write sample code to
138-
add and retrieve values.
137+
using :doc:`/types/sequences-sets/tuples` as key row and column values. Write
138+
sample code to add and retrieve values.
139139

140140
.. code-block:: pycon
141141

docs/types/index.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ values to more complex structures like lists, dictionaries and files.
4545
:hidden:
4646

4747
numbers/index
48-
lists
49-
tuples
50-
sets
48+
sequences-sets/index
5149
dicts
5250
strings/index
5351
files
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Sequences and Sets
2+
==================
3+
4+
Sequences (:doc:`lists` and :doc:`tuples`) and sets (:ref:`set` and
5+
:ref:`frozenset`) essentially differ in the following properties:
6+
7+
+---------------+---------------+---------------+---------------+---------------+
8+
| Data type | mutable | ordered | indexed | duplicates |
9+
+===============+===============+===============+===============+===============+
10+
| List |||||
11+
+---------------+---------------+---------------+---------------+---------------+
12+
| Tuple |||||
13+
+---------------+---------------+---------------+---------------+---------------+
14+
| Set |||||
15+
+---------------+---------------+---------------+---------------+---------------+
16+
| Frozenset |||||
17+
+---------------+---------------+---------------+---------------+---------------+
18+
19+
.. toctree::
20+
:titlesonly:
21+
:hidden:
22+
23+
lists
24+
tuples
25+
sets
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Lists
44
A list in Python is similar to an array in Java or C: an ordered collection of
55
objects. However, unlike lists in many other languages, Python lists can contain
66
different types of elements; a list element can be any Python object, including
7-
:doc:`strings/index`, :doc:`tuples`, :doc:`lists`, :doc:`dicts`,
8-
:doc:`../functions/index`, :doc:`files` and any kind of :doc:`numbers/index`.
9-
You create a list by enclosing no elements or elements separated by commas in
10-
square brackets, like this:
7+
:doc:`../strings/index`, :doc:`tuples`, :doc:`lists`, :doc:`../dicts`,
8+
:doc:`../../functions/index`, :doc:`../files` and any kind of
9+
:doc:`../numbers/index`. You create a list by enclosing no elements or elements
10+
separated by commas in square brackets, like this:
1111

1212
.. code-block:: python
1313
:linenos:
@@ -283,9 +283,9 @@ User-defined sorting
283283
::::::::::::::::::::
284284

285285
.. note::
286-
You must be able to define :doc:`../functions/index` for user-defined
287-
sorting. The processing of :doc:`strings/index` will also be covered in more
288-
detail later.
286+
You must be able to define :doc:`../../functions/index` for user-defined
287+
sorting. The processing of :doc:`../strings/index` will also be covered in
288+
more detail later.
289289

290290
Python usually sorts words lexicographically – upper case before lower case.
291291
However, we want to sort a list of words by the number of characters in each
@@ -309,11 +309,11 @@ The ``sorted`` function
309309
:::::::::::::::::::::::
310310

311311
Lists have an inbuilt method for sorting themselves :meth:`python3:list.sort`.
312-
However, other iterables in Python, such as the keys of :doc:`dicts`, do not
312+
However, other iterables in Python, such as the keys of :doc:`../dicts`, do not
313313
have a sorting method. However, Python offers the built-in
314314
:func:`python3:sorted` function for this purpose, which returns a sorted list
315315
from any iterable. :func:`python3:sorted` uses the same
316-
:doc:`../functions/params` ``key`` and ``reverse`` as the
316+
:doc:`../../functions/params` ``key`` and ``reverse`` as the
317317
:meth:`python3:list.sort` method:
318318

319319
.. code-block:: pycon
@@ -532,15 +532,6 @@ has any effect on the original list:
532532
>>> sup
533533
[[0], 1]
534534
535-
Summary
536-
-------
537-
538-
+---------------+---------------+---------------+---------------+---------------+
539-
| data type | mutable | ordered | indexed | duplicates |
540-
+===============+===============+===============+===============+===============+
541-
| list |||||
542-
+---------------+---------------+---------------+---------------+---------------+
543-
544535
Checks
545536
------
546537

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ situations where membership and uniqueness to the set are the most important
66
information of the object. The ``in`` operator runs faster with sets than with
77
:doc:`lists`:
88

9+
.. _set:
10+
911
``set``
1012
-------
1113

@@ -53,6 +55,8 @@ Line 19
5355
``^`` is used to find the symmetrical difference, meaning elements that are
5456
contained in one or the other set, but not in both.
5557

58+
.. _frozenset:
59+
5660
``frozenset``
5761
-------------
5862

@@ -74,22 +78,14 @@ means that they can also be members of other sets:
7478
>>> x
7579
{1, 2, 3, 4, frozenset({1, 2, 3, 4})}
7680
77-
Summary
78-
-------
81+
Order
82+
-----
7983

8084
However, the speed advantage also comes at a price: sets do not keep the
8185
elements in the correct order, whereas :doc:`lists` and :doc:`tuples` do. If the
8286
order is important to you, you should use a data structure that remembers the
8387
order.
8488

85-
+---------------+---------------+---------------+---------------+---------------+
86-
| data type | mutable | ordered | indexed | duplicates |
87-
+===============+===============+===============+===============+===============+
88-
| Sets |||||
89-
+---------------+---------------+---------------+---------------+---------------+
90-
| Frozensets |||||
91-
+---------------+---------------+---------------+---------------+---------------+
92-
9389
Checks
9490
------
9591

0 commit comments

Comments
 (0)