Streamline previous content
This commit is contained in:
parent
5e71194787
commit
04d53956a3
11 changed files with 1089 additions and 402 deletions
|
|
@ -8,7 +8,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"# Chapter 4: Iteration"
|
||||
"# Chapter 4: Recursion & Looping"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -859,7 +859,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"39.9 µs ± 5.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
"69.6 µs ± 25.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -881,7 +881,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1.61 ms ± 8.84 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
"1.55 ms ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -903,7 +903,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"199 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"189 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -925,7 +925,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2.21 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"2.07 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -947,7 +947,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"5.8 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"5.41 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -4289,7 +4289,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"With everything *officially* introduced so far (i.e., without the introductory example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb) that only served as an overview), Python would be what is called **[Turing complete](https://en.wikipedia.org/wiki/Turing_completeness)**. That means that anything that could be formulated as an algorithm could be expressed with all the language features we have seen. Note that, in particular, we have *not* yet formally *introduced* the `for` and `while` statements!"
|
||||
"With everything *officially* introduced so far, Python would be what is called **[Turing complete](https://en.wikipedia.org/wiki/Turing_completeness)**. That means that anything that could be formulated as an algorithm could be expressed with all the language features we have seen. Note that, in particular, we have *not* yet formally *introduced* the `for` and `while` statements!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4565,7 +4565,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4.69 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"4.9 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -4877,7 +4877,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"For sequences of integers, the [range()](https://docs.python.org/3/library/functions.html#func-range) built-in makes the `for` statement even more convenient: It creates a list-like object of type `range` that generates integers \"on the fly,\" and we look closely at the underlying effects in memory in Chapter 7."
|
||||
"For sequences of integers, the [range()](https://docs.python.org/3/library/functions.html#func-range) built-in makes the `for` statement even more convenient: It creates a list-like object of type `range` that generates integers \"on the fly,\" and we look closely at the underlying effects in memory in [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences.ipynb#Mapping)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5008,13 +5008,13 @@
|
|||
"\n",
|
||||
"Now, just as we classify objects by their types, we also classify these **concrete data types** (e.g., `int`, `float`, `str`, or `list`) into **abstract concepts**.\n",
|
||||
"\n",
|
||||
"We did this already in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb) when we described a `list` object as \"some sort of container that holds [...] pointers to other objects\". So, abstractly speaking, **containers** are any objects that are \"composed\" of other objects and also \"manage\" how these objects are organized. `list` objects, for example, have the property that they model an internal order associated with its elements. There exist, however, other container types, many of which do *not* come with an order. So, containers primarily \"contain\" other objects and have *nothing* to do with looping.\n",
|
||||
"We did this already in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb#Who-am-I?-And-how-many?) when we described a `list` object as \"some sort of container that holds [...] pointers to other objects\". So, abstractly speaking, **containers** are any objects that are \"composed\" of other objects and also \"manage\" how these objects are organized. `list` objects, for example, have the property that they model an internal order associated with its elements. There exist, however, other container types, many of which do *not* come with an order. So, containers primarily \"contain\" other objects and have *nothing* to do with looping.\n",
|
||||
"\n",
|
||||
"On the contrary, the abstract concept of **iterables** is all about looping: Any object that we can loop over is, by definition, an iterable. So, `range` objects, for example, are iterables that do *not* contain other objects. Moreover, looping does *not* have to occur in a *predictable* order, although this is the case for both `list` and `range` objects.\n",
|
||||
"\n",
|
||||
"Typically, containers are iterables, and iterables are containers. Yet, only because these two concepts coincide often, we must not think of them as the same. Chapter 10 finally gives an explanation as to how abstract concepts are implemented and play together.\n",
|
||||
"Typically, containers are iterables, and iterables are containers. Yet, only because these two concepts coincide often, we must not think of them as the same. Chapter 9 finally gives an explanation as to how abstract concepts are implemented and play together.\n",
|
||||
"\n",
|
||||
"So, `list` objects like `first_names` below are iterable containers. They implement even more abstract concepts, as Chapter 7 reveals."
|
||||
"So, `list` objects like `first_names` below are iterable containers. They implement even more abstract concepts, as [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences.ipynb#Collections-vs.-Sequences) reveals."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5165,7 +5165,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"If we must have an index variable in the loop's body, we use the [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) built-in that takes an *iterable* as its argument and then generates a \"stream\" of \"pairs\" consisting of an index variable and an object provided by the iterable. There is *no* need to ever revert to the `while` statement to loop over an iterable object."
|
||||
"If we must have an index variable in the loop's body, we use the [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) built-in that takes an *iterable* as its argument and then generates a \"stream\" of \"pairs\" of an index variable, `i` below, and an object provided by the iterable, `name`, separated by a `,`. There is *no* need to ever revert to the `while` statement with an explicitly managed index variable to loop over an iterable object."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5288,7 +5288,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"In contrast to its recursive counterpart, the iterative `fibonacci()` function below is somewhat harder to read. For example, it is not so obvious as to how many iterations through the `for`-loop we need to make when implementing it. There is an increased risk of making an *off-by-one* error. Moreover, we need to track a `temp` variable along, at least until we have worked through Chapter 7. Do you understand what `temp` does?\n",
|
||||
"In contrast to its recursive counterpart, the iterative `fibonacci()` function below is somewhat harder to read. For example, it is not so obvious as to how many iterations through the `for`-loop we need to make when implementing it. There is an increased risk of making an *off-by-one* error. Moreover, we need to track a `temp` variable along.\n",
|
||||
"\n",
|
||||
"However, one advantage of calculating Fibonacci numbers in a **forward** fashion with a `for` statement is that we could list the entire sequence in ascending order as we calculate the desired number. To show this, we added `print()` statements in `fibonacci()` below.\n",
|
||||
"\n",
|
||||
|
|
@ -5843,7 +5843,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Often, we process some iterable with numeric data, for example, a list of numbers as in the introductory example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb) or, more realistically, data from a CSV file with many rows and columns.\n",
|
||||
"Often, we process some iterable with numeric data, for example, a list of numbers as in the introductory example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb#Example:-Average-of-a-Subset-of-Numbers) or, more realistically, data from a CSV file with many rows and columns.\n",
|
||||
"\n",
|
||||
"Processing numeric data usually comes down to operations that may be grouped into one of the following three categories:\n",
|
||||
"\n",
|
||||
|
|
@ -5851,7 +5851,7 @@
|
|||
"- **filtering**: throw away individual samples (e.g., statistical outliers)\n",
|
||||
"- **reducing**: collect individual samples into summary statistics\n",
|
||||
"\n",
|
||||
"We study this **map-filter-reduce** paradigm extensively in Chapter 7 after introducing more advanced data types that are needed to work with \"big\" data.\n",
|
||||
"We study this **map-filter-reduce** paradigm extensively in [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences.ipynb#The-Map-Filter-Reduce-Paradigm) after introducing more advanced data types that are needed to work with \"big\" data.\n",
|
||||
"\n",
|
||||
"In the remainder of this section, we focus on *filtering out* some samples within a `for`-loop."
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue