Streamline previous content
This commit is contained in:
parent
974362288a
commit
8fd14a614d
6 changed files with 393 additions and 384 deletions
|
|
@ -859,7 +859,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"69.6 µs ± 25.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
"36.5 µs ± 2.47 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -881,7 +881,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1.55 ms ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
"1.58 ms ± 20.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -903,7 +903,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"189 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"194 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.07 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"2.15 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.41 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"5.59 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -4107,7 +4107,7 @@
|
|||
"source": [
|
||||
"We use the built-in [isinstance()](https://docs.python.org/3/library/functions.html#isinstance) function to make sure `factorial()` is called with an `int` object as the argument. We further **validate the input** by verifying that the integer is non-negative.\n",
|
||||
"\n",
|
||||
"Meanwhile, we also see how we manually raise exceptions with the `raise` [statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement), another way of controlling the flow of execution.\n",
|
||||
"Meanwhile, we also see how we manually raise exceptions with the `raise` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement)), another way of controlling the flow of execution.\n",
|
||||
"\n",
|
||||
"The first two branches in the revised `factorial()` function act as **guardians** ensuring that the code does not produce *unexpected* runtime errors: Errors may be expected when mentioned in the docstring.\n",
|
||||
"\n",
|
||||
|
|
@ -4322,7 +4322,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Whereas functions combined with `if` statements suffice to model any repetitive logic, Python comes with a `while` [statement](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement) that often makes it easier to implement iterative ideas.\n",
|
||||
"Whereas functions combined with `if` statements suffice to model any repetitive logic, Python comes with a compound `while` statement (cf., [reference](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement)) that often makes it easier to implement iterative ideas.\n",
|
||||
"\n",
|
||||
"It consists of a header line with a boolean expression followed by an indented code block. Before the first and after every execution of the code block, the boolean expression is evaluated, and if it is (still) equal to `True`, the code block runs (again). Eventually, some variable referenced in the boolean expression is changed in the code block such that the condition becomes `False`.\n",
|
||||
"\n",
|
||||
|
|
@ -4565,7 +4565,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4.9 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
"4.69 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -4792,9 +4792,9 @@
|
|||
"source": [
|
||||
"Recursion and the `while` statement are two sides of the same coin. Disregarding that in the case of recursion Python internally faces some additional burden for managing the stack of frames in memory, both approaches lead to the *same* computational steps in memory. More importantly, we can re-formulate a recursive implementation in an iterative way and vice versa despite one of the two ways often \"feeling\" a lot more natural given a particular problem.\n",
|
||||
"\n",
|
||||
"So how does the `for` [statement](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement) we saw in the very first example in this book fit into this picture? It is a *redundant* language construct to provide a *shorter* and more *convenient* syntax for common applications of the `while` statement. In programming, such additions to a language are called **syntactic sugar**. A cup of tea tastes better with sugar, but we may drink tea without sugar too.\n",
|
||||
"So how does the compound `for` statement (cf., [reference](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)) in this book's very first example fit into this picture? It is a *redundant* language construct to provide a *shorter* and more *convenient* syntax for common applications of the `while` statement. In programming, such additions to a language are called **syntactic sugar**. A cup of tea tastes better with sugar, but we may drink tea without sugar too.\n",
|
||||
"\n",
|
||||
"Consider the following `numbers` list. Without the `for` statement, we have to manage a temporary **index variable** `i` to loop over all the elements and also obtain the individual elements with the `[]` operator in each iteration of the loop."
|
||||
"Consider `elements` below. Without the `for` statement, we have to manage a temporary **index variable**, `index`, to loop over all the elements and also obtain the individual elements with the `[]` operator in each iteration of the loop."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4807,7 +4807,7 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"numbers = [0, 1, 2, 3, 4]"
|
||||
"elements = [0, 1, 2, 3, 4]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4828,12 +4828,12 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"i = 0\n",
|
||||
"while i < len(numbers):\n",
|
||||
" number = numbers[i]\n",
|
||||
" print(number, end=\" \")\n",
|
||||
" i += 1\n",
|
||||
"del i"
|
||||
"index = 0\n",
|
||||
"while index < len(elements):\n",
|
||||
" element = elements[index]\n",
|
||||
" print(element, end=\" \")\n",
|
||||
" index += 1\n",
|
||||
"del index"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4865,8 +4865,8 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \")"
|
||||
"for element in elements:\n",
|
||||
" print(element, end=\" \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -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](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences.ipynb#Mapping)."
|
||||
"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)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4898,8 +4898,8 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in range(5):\n",
|
||||
" print(number, end=\" \")"
|
||||
"for element in range(5):\n",
|
||||
" print(element, end=\" \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4955,8 +4955,8 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in [1, 3, 5, 7, 9]:\n",
|
||||
" print(number, end=\" \")"
|
||||
"for element in [1, 3, 5, 7, 9]:\n",
|
||||
" print(element, end=\" \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -4977,8 +4977,8 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in range(1, 10, 2):\n",
|
||||
" print(number, end=\" \")"
|
||||
"for element in range(1, 10, 2):\n",
|
||||
" print(element, end=\" \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5097,7 +5097,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The cell below shows the *exact* workings of the `in` operator: Although `3.0` is *not* contained in `numbers`, it evaluates equal to the `3` that is, which is why the following expression evaluates to `True`. So, while we could colloquially say that `numbers` \"contains\" `3.0`, it actually does not."
|
||||
"The cell below shows the *exact* workings of the `in` operator: Although `3.0` is *not* contained in `elements`, it evaluates equal to the `3` that is, which is why the following expression evaluates to `True`. So, while we could colloquially say that `elements` \"contains\" `3.0`, it actually does not."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5121,7 +5121,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"3.0 in numbers"
|
||||
"3.0 in elements"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5550,7 +5550,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Let's say we have a `numbers` list and want to check if the square of at least one of its elements is above a `threshold` of `100`."
|
||||
"Let's say we have a `samples` list and want to check if the square of at least one of its elements is above a `threshold` of `100`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5563,7 +5563,7 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"numbers = [3, 7, 2, 9, 11, 4, 7, 9, 4, 5]"
|
||||
"samples = [3, 7, 2, 9, 11, 4, 7, 9, 4, 5]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5587,7 +5587,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"A first naive implementation could look like this: We loop over *every* element in `numbers` and set an **indicator variable** `is_above`, initialized as `False`, to `True` once we encounter an element satisfying the search condition."
|
||||
"A first naive implementation could look like this: We loop over *every* element in `samples` and set an **indicator variable** `is_above`, initialized as `False`, to `True` once we encounter an element satisfying the search condition."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5603,22 +5603,22 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 7 2 9 11 4 7 9 4 5 => at least one number's square is above 100\n"
|
||||
"3 7 2 9 11 4 7 9 4 5 => at least one sample's square is above 100\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"is_above = False\n",
|
||||
"\n",
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \") # added for didactical purposes\n",
|
||||
" if number ** 2 > threshold:\n",
|
||||
"for sample in samples:\n",
|
||||
" print(sample, end=\" \") # added for didactical purposes\n",
|
||||
" if sample ** 2 > threshold:\n",
|
||||
" is_above = True\n",
|
||||
"\n",
|
||||
"if is_above:\n",
|
||||
" print(\"=> at least one number's square is above\", threshold)\n",
|
||||
" print(\"=> at least one sample's square is above\", threshold)\n",
|
||||
"else:\n",
|
||||
" print(\"=> no number's square is above\", threshold)"
|
||||
" print(\"=> no sample's square is above\", threshold)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5629,11 +5629,11 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"This implementation is *inefficient* as even if the *first* element in `numbers` has a square greater than `100`, we loop until the last element: This could take a long time for a big list.\n",
|
||||
"This implementation is *inefficient* as even if the *first* element in `samples` has a square greater than `100`, we loop until the last element: This could take a long time for a big list.\n",
|
||||
"\n",
|
||||
"Moreover, we must initialize `is_above` *before* the `for`-loop and write an `if`-`else`-logic *after* it to check for the result. The actual business logic is *not* clear right away.\n",
|
||||
"\n",
|
||||
"Luckily, Python provides a `break` [statement](https://docs.python.org/3/reference/simple_stmts.html#the-break-statement) that lets us stop the `for`-loop in any iteration of the loop. Conceptually, it is yet another means of controlling the flow of execution."
|
||||
"Luckily, Python provides the `break` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-break-statement)) that lets us stop the `for`-loop in any iteration of the loop. Conceptually, it is yet another means of controlling the flow of execution."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5649,23 +5649,23 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 7 2 9 11 => at least one number's square is above 100\n"
|
||||
"3 7 2 9 11 => at least one sample's square is above 100\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"is_above = False\n",
|
||||
"\n",
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \") # added for didactical purposes\n",
|
||||
" if number ** 2 > threshold:\n",
|
||||
"for sample in samples:\n",
|
||||
" print(sample, end=\" \") # added for didactical purposes\n",
|
||||
" if sample ** 2 > threshold:\n",
|
||||
" is_above = True\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
"if is_above:\n",
|
||||
" print(\"=> at least one number's square is above\", threshold)\n",
|
||||
" print(\"=> at least one sample's square is above\", threshold)\n",
|
||||
"else:\n",
|
||||
" print(\"=> no number's square is above\", threshold)"
|
||||
" print(\"=> no sample's square is above\", threshold)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5687,7 +5687,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"### The `for`-`else` Clause"
|
||||
"### The `for`-`else`-Clause"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5698,7 +5698,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To express the logic in a prettier way, we add an `else`-clause at the end of the `for`-loop. The `else`-branch is executed *iff* the `for`-loop is *not* stopped with a `break` statement **prematurely** (i.e., *before* reaching the *last* iteration in the loop). The word \"else\" implies a somewhat unintuitive meaning and had better been named a `then`-clause. In most scenarios, however, the `else`-clause logically goes together with some `if` statement in the `for`-loop's body.\n",
|
||||
"To express the logic in a prettier way, we add an `else`-clause at the end of the `for`-loop (cf., [reference](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)). The `else`-clause is executed *iff* the `for`-loop is *not* stopped with a `break` statement **prematurely** (i.e., *before* reaching the *last* iteration in the loop). The word \"else\" implies a somewhat unintuitive meaning and had better been named a `then`-clause. In most scenarios, however, the `else`-clause logically goes together with some `if` statement in the `for`-loop's body.\n",
|
||||
"\n",
|
||||
"Overall, the code's expressive power increases. Not many programming languages support a `for`-`else`-branching, which turns out to be very useful in practice."
|
||||
]
|
||||
|
|
@ -5727,23 +5727,23 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 7 2 9 11 => at least one number's square is above 100\n"
|
||||
"3 7 2 9 11 => at least one sample's square is above 100\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \") # added for didactical purposes\n",
|
||||
" if number ** 2 > threshold:\n",
|
||||
"for sample in samples:\n",
|
||||
" print(sample, end=\" \") # added for didactical purposes\n",
|
||||
" if sample ** 2 > threshold:\n",
|
||||
" is_above = True\n",
|
||||
" break\n",
|
||||
"else:\n",
|
||||
" is_above = False\n",
|
||||
"\n",
|
||||
"if is_above:\n",
|
||||
" print(\"=> at least one number's square is above\", threshold)\n",
|
||||
" print(\"=> at least one sample's square is above\", threshold)\n",
|
||||
"else:\n",
|
||||
" print(\"=> no number's square is above\", threshold)"
|
||||
" print(\"=> no sample's square is above\", threshold)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5770,18 +5770,18 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 7 2 9 11 => at least one number's square is above 100\n"
|
||||
"3 7 2 9 11 => at least one sample's square is above 100\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \") # added for didactical purposes\n",
|
||||
" if number ** 2 > threshold:\n",
|
||||
" print(\"=> at least one number's square is above\", threshold)\n",
|
||||
"for sample in samples:\n",
|
||||
" print(sample, end=\" \") # added for didactical purposes\n",
|
||||
" if sample ** 2 > threshold:\n",
|
||||
" print(\"=> at least one sample's square is above\", threshold)\n",
|
||||
" break\n",
|
||||
"else:\n",
|
||||
" print(\"=> no number's square is above\", threshold)"
|
||||
" print(\"=> no sample's square is above\", threshold)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5792,7 +5792,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Of course, if we set the `threshold` an element's square has to pass higher, for example, to `200`, we have to loop through the entire `numbers` list. There is *no way* to optimize this **[linear search](https://en.wikipedia.org/wiki/Linear_search)** further, at least as long as we model `numbers` as a `list` object. More advanced data types, however, exist that mitigate that downside."
|
||||
"Of course, if we set the `threshold` an element's square has to pass higher, for example, to `200`, we have to loop over all `samples`. There is *no way* to optimize this **[linear search](https://en.wikipedia.org/wiki/Linear_search)** further, at least as long as we model `samples` as a `list` object. More advanced data types, however, exist that mitigate that downside."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5808,20 +5808,20 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 7 2 9 11 4 7 9 4 5 => no number's square is above 200\n"
|
||||
"3 7 2 9 11 4 7 9 4 5 => no sample's square is above 200\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"threshold = 200\n",
|
||||
"\n",
|
||||
"for number in numbers:\n",
|
||||
" print(number, end=\" \") # added for didactical purposes\n",
|
||||
" if number ** 2 > threshold:\n",
|
||||
" print(\"=> at least one number's square is above\", threshold)\n",
|
||||
"for sample in samples:\n",
|
||||
" print(sample, end=\" \") # added for didactical purposes\n",
|
||||
" if sample ** 2 > threshold:\n",
|
||||
" print(\"=> at least one sample's square is above\", threshold)\n",
|
||||
" break\n",
|
||||
"else:\n",
|
||||
" print(\"=> no number's square is above\", threshold)"
|
||||
" print(\"=> no sample's square is above\", threshold)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -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#Example:-Average-of-a-Subset-of-Numbers) 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 this book's introductory example in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb#Example:-Averaging-Even-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",
|
||||
|
|
@ -5875,7 +5875,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Calculate the sum of all even numbers from $1$ through $12$ after squaring them and adding $1$ to the squares:\n",
|
||||
"Calculate the sum of all even numbers in `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]` after squaring them and adding `1` to the squares:\n",
|
||||
"\n",
|
||||
"- **\"all\"** => loop over an iterable\n",
|
||||
"- **\"even\"** => *filter* out the odd numbers\n",
|
||||
|
|
@ -5893,7 +5893,7 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"
|
||||
"numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5909,7 +5909,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2 > 5 4 > 17 6 > 37 8 > 65 10 > 101 12 > 145 "
|
||||
"8 > 65 12 > 145 2 > 5 6 > 37 10 > 101 4 > 17 "
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -5926,11 +5926,11 @@
|
|||
"source": [
|
||||
"total = 0\n",
|
||||
"\n",
|
||||
"for x in numbers:\n",
|
||||
" if x % 2 == 0: # only keep even numbers\n",
|
||||
" y = (x ** 2) + 1\n",
|
||||
" print(x, y, sep=\" > \", end=\" \") # added for didactical purposes\n",
|
||||
" total += y\n",
|
||||
"for number in numbers:\n",
|
||||
" if number % 2 == 0: # only keep even numbers\n",
|
||||
" square = (number ** 2) + 1\n",
|
||||
" print(number, square, sep=\" > \", end=\" \") # added for didactical purposes\n",
|
||||
" total += square\n",
|
||||
"\n",
|
||||
"total"
|
||||
]
|
||||
|
|
@ -5969,7 +5969,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Calculate the sum of every third and even number from $1$ through $12$ after squaring them and adding $1$ to the squares:\n",
|
||||
"Calculate the sum of every third and even number in `[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]` after squaring them and adding `1` to the squares:\n",
|
||||
"\n",
|
||||
"- **\"every\"** => loop over an iterable\n",
|
||||
"- **\"third\"** => *filter* out all numbers except every third\n",
|
||||
|
|
@ -5990,7 +5990,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"
|
||||
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 78,
|
||||
|
|
@ -6015,13 +6015,13 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"6 > 37 12 > 145 "
|
||||
"8 > 65 12 > 145 4 > 17 "
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"182"
|
||||
"227"
|
||||
]
|
||||
},
|
||||
"execution_count": 79,
|
||||
|
|
@ -6032,12 +6032,12 @@
|
|||
"source": [
|
||||
"total = 0\n",
|
||||
"\n",
|
||||
"for i, x in enumerate(numbers, start=1):\n",
|
||||
"for i, number in enumerate(numbers, start=1):\n",
|
||||
" if i % 3 == 0: # only keep every third number\n",
|
||||
" if x % 2 == 0: # only keep even numbers\n",
|
||||
" y = (x ** 2) + 1\n",
|
||||
" print(x, y, sep=\" > \", end=\" \") # added for didactical purposes \n",
|
||||
" total += y\n",
|
||||
" if number % 2 == 0: # only keep even numbers\n",
|
||||
" square = (number ** 2) + 1\n",
|
||||
" print(number, square, sep=\" > \", end=\" \") # added for didactical purposes \n",
|
||||
" total += square\n",
|
||||
"\n",
|
||||
"total"
|
||||
]
|
||||
|
|
@ -6052,7 +6052,7 @@
|
|||
"source": [
|
||||
"With already three levels of indentation, less horizontal space is available for the actual code block. Of course, one could flatten the two `if` statements with the logical `and` operator, as shown in [Chapter 3](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals.ipynb#The-if-Statement). Then, however, we trade off horizontal space against a more \"complex\" `if` logic, and this is *not* a real improvement.\n",
|
||||
"\n",
|
||||
"A Pythonista would instead make use of the `continue` [statement](https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement) that causes a loop to jump into the next iteration skipping the rest of the code block.\n",
|
||||
"A Pythonista would instead make use of the `continue` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement)) that causes a loop to jump into the next iteration skipping the rest of the code block.\n",
|
||||
"\n",
|
||||
"The revised code fragment below occupies more vertical space and less horizontal space: A *good* trade-off."
|
||||
]
|
||||
|
|
@ -6081,13 +6081,13 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"6 > 37 12 > 145 "
|
||||
"8 > 65 12 > 145 4 > 17 "
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"182"
|
||||
"227"
|
||||
]
|
||||
},
|
||||
"execution_count": 80,
|
||||
|
|
@ -6098,15 +6098,15 @@
|
|||
"source": [
|
||||
"total = 0\n",
|
||||
"\n",
|
||||
"for i, x in enumerate(numbers, start=1):\n",
|
||||
"for i, number in enumerate(numbers, start=1):\n",
|
||||
" if i % 3 != 0: # only keep every third number\n",
|
||||
" continue\n",
|
||||
" elif x % 2 != 0: # only keep even numbers\n",
|
||||
" elif number % 2 != 0: # only keep even numbers\n",
|
||||
" continue\n",
|
||||
"\n",
|
||||
" y = (x ** 2) + 1\n",
|
||||
" print(x, y, sep=\" > \", end=\" \") # added for didactical purposes \n",
|
||||
" total += y\n",
|
||||
" square = (number ** 2) + 1\n",
|
||||
" print(number, square, sep=\" > \", end=\" \") # added for didactical purposes \n",
|
||||
" total += square\n",
|
||||
"\n",
|
||||
"total"
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue