|
19 | 19 | "cell_type": "markdown",
|
20 | 20 | "metadata": {},
|
21 | 21 | "source": [
|
22 |
| - "Learning about the `if` conditions has strongly improved your coding powers!\n", |
| 22 | + "Learning about the `if` conditional statement has strongly improved your coding powers!\n", |
23 | 23 | "\n",
|
24 | 24 | "You know what a `list` is from the [Lists of Variables notebook](002_Lists_of_Variables.ipynb), and how to interact with this useful data container.\n",
|
25 | 25 | "\n",
|
26 | 26 | "After the [Conditional Execution notebook](003_Conditional_Execution.ipynb), you can now also organize your code so that some actions are executed only when one or more specific conditions are `True`.\n",
|
27 | 27 | "\n",
|
28 |
| - "In this notebook, we will combine what you learned up to now by using a coding mechanism that iterates through all the elements in a list: the `for` loop." |
| 28 | + "We will now combine what you have learned by using a new coding mechanism: the `for` loop." |
29 | 29 | ]
|
30 | 30 | },
|
31 | 31 | {
|
|
41 | 41 | "source": [
|
42 | 42 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
43 | 43 | "\n",
|
44 |
| - "A `for` loop is used to iterate through a sequence of elements (e.g., a list)." |
| 44 | + "A `for` loop is used to move through items (**iterate**) in a sequence of elements such as a list." |
45 | 45 | ]
|
46 | 46 | },
|
47 | 47 | {
|
|
50 | 50 | "source": [
|
51 | 51 | "Similarly to the [`if`](003_Conditional_Execution.ipynb#Applying-Conditions-with-if,-elif,-and-else) statement, a `for` loop requires that:\n",
|
52 | 52 | "\n",
|
53 |
| - "- The condition following the `for` keyword must be `True`. (There is a subsequent element in the sequence.)\n", |
54 |
| - "- You add an `:` at the end of the `for` condition.\n", |
| 53 | + "- There must be a subsequent element in the sequence; the condition following the `for` keyword must be `True`. \n", |
| 54 | + "- You add a `:` at the end of the `for` condition.\n", |
55 | 55 | "- You indent all the lines of code that have to be executed in each iteration."
|
56 | 56 | ]
|
57 | 57 | },
|
58 | 58 | {
|
59 | 59 | "cell_type": "markdown",
|
60 | 60 | "metadata": {},
|
61 | 61 | "source": [
|
62 |
| - "Let's put these requirements in practice in the following code:" |
| 62 | + "Let us put these requirements in practice in the following code:" |
63 | 63 | ]
|
64 | 64 | },
|
65 | 65 | {
|
|
68 | 68 | "metadata": {},
|
69 | 69 | "outputs": [],
|
70 | 70 | "source": [
|
71 |
| - "temp_list = [21.3, 21.7, 22.0, 22.5, 21.9] # temperature measures in degree Celsius\n", |
| 71 | + "temp_list = [21.3, 21.7, 22.0, 22.5, 21.9] # temperatures in degrees Celsius\n", |
72 | 72 | "\n",
|
73 |
| - "for temp_value in temp_list: # `temp_value` is the variable that temporarily stores the subsequent value in the list\n", |
| 73 | + "for temp_value in temp_list: # `temp_value` temporarily stores the subsequent value in the list\n", |
74 | 74 | " temp_str = str(temp_value) # type-casting the `float` to a `str` for printing its value in the next row \n",
|
75 | 75 | " print(\"The temperature is \" + temp_str + \" degree Celsius.\")"
|
76 | 76 | ]
|
|
79 | 79 | "cell_type": "markdown",
|
80 | 80 | "metadata": {},
|
81 | 81 | "source": [
|
82 |
| - "By executing the above **Code** cell, the `print()` function will be called five times. Once for each item in the `temp_list`." |
| 82 | + "The `print()` function will be called five times by executing the above **Code** cell. Once for each item in the `temp_list`." |
83 | 83 | ]
|
84 | 84 | },
|
85 | 85 | {
|
86 | 86 | "cell_type": "markdown",
|
87 | 87 | "metadata": {},
|
88 | 88 | "source": [
|
89 |
| - "The header of the above `for` loop is very descriptive of what is going to happen! \n", |
| 89 | + "The header of the above `for` loop is very descriptive of what is going to happen! <br>\n", |
| 90 | + "You can almost read the `for temp_value in temp_list:` as plain English. That is, `for` each temperature value `in` the list do *something*. And the *something* is what is in the indented body of the loop:\n", |
90 | 91 | "\n",
|
91 |
| - "In fact, you can almost read the `for temp_value in temp_list:` as plain English. That is, `for` each temperature value `in` the list do *something*. And the *something* is what is in the indented body of the loop:\n", |
92 |
| - "\n", |
93 |
| - "- Convert the `temp_value` in a string.\n", |
94 |
| - "- Print the value in a sentence that is reader friendly (e.g., `The temperature is 21.3 degree Celsius.`). " |
| 92 | + "- Convert the `temp_value` into a string.\n", |
| 93 | + "- Print the temperature value in a sentence that is reader friendly (e.g., `The temperature is 21.3 degree Celsius.`). " |
95 | 94 | ]
|
96 | 95 | },
|
97 | 96 | {
|
|
104 | 103 | {
|
105 | 104 | "cell_type": "markdown",
|
106 | 105 | "metadata": {
|
107 |
| - "solution2": "shown", |
| 106 | + "solution2": "hidden", |
108 | 107 | "solution2_first": true
|
109 | 108 | },
|
110 | 109 | "source": [
|
|
117 | 116 | "cell_type": "code",
|
118 | 117 | "execution_count": null,
|
119 | 118 | "metadata": {
|
120 |
| - "solution2": "shown" |
| 119 | + "solution2": "hidden" |
121 | 120 | },
|
122 | 121 | "outputs": [],
|
123 | 122 | "source": [
|
|
155 | 154 | "cell_type": "markdown",
|
156 | 155 | "metadata": {},
|
157 | 156 | "source": [
|
158 |
| - "The `break` and `continue` keywords permit the creation of efficient loops.\n", |
| 157 | + "The `break` and `continue` keywords allow for the creation of efficient loops.\n", |
159 | 158 | "\n",
|
160 |
| - "For instance, assuming that you want to be sure that a list of temperatures has at least a value higher than 10 degree Celsius. Using `break`, you can stop the iteration when that condition is `True`." |
| 159 | + "For instance, assuming that you want to be sure that a list of temperatures has at least a value higher than 10 degrees Celsius. Using `break`, you can stop the iteration when that condition is `True`." |
161 | 160 | ]
|
162 | 161 | },
|
163 | 162 | {
|
|
166 | 165 | "metadata": {},
|
167 | 166 | "outputs": [],
|
168 | 167 | "source": [
|
169 |
| - "temp_list = [1.6, 11.7, 12.1, 2.4, 8.9] # temperature measures in degree Celsius\n", |
| 168 | + "temp_list = [1.6, 11.7, 12.1, 2.4, 8.9] # temperatures in degrees Celsius\n", |
170 | 169 | "\n",
|
171 | 170 | "for temp_value in temp_list:\n",
|
172 | 171 | " \n",
|
173 |
| - " print(\"Temperature value: \" + str(temp_value)) # an alternate way to print the value by type-casting it on the fly\n", |
| 172 | + " print(\"Temperature value: \" + str(temp_value)) # Here `temp_value` is type-casted within the `print` function \n", |
174 | 173 | " \n",
|
175 | 174 | " if temp_value > 10:\n",
|
176 |
| - " print(\"BREAK: At least one temperature value is higher than 10 degree Celsius.\")\n", |
| 175 | + " print(\"BREAK: At least one temperature value is higher than 10 degrees Celsius.\")\n", |
177 | 176 | " break # if the execution reach this point, the loop stops"
|
178 | 177 | ]
|
179 | 178 | },
|
|
234 | 233 | "source": [
|
235 | 234 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
236 | 235 | "\n",
|
237 |
| - "Using `break` or `continue`, modify the code to **stop** the printing of sound speed values when the **first** value is outside of the validity range from 1400 m/s to 1600 m/s. (If you do not remember how to use the **logical operator**, read the [Conditional Execution notebook](003_Conditional_Execution.ipynb#Boolean-Expressions) again.)" |
| 236 | + "Using `break` or `continue`, modify the code to **stop** the printing of sound speed values when the **first** value is outside of the validity range from 1400 m/s to 1600 m/s. (Hint: if you do not remember how to use the **logical operators**, read the [Conditional Execution notebook](003_Conditional_Execution.ipynb#Boolean-Expressions) again)" |
238 | 237 | ]
|
239 | 238 | },
|
240 | 239 | {
|
|
347 | 346 | "source": [
|
348 | 347 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
349 | 348 | "\n",
|
350 |
| - "The `pop()` method removes (and returns) the last value from a list." |
| 349 | + "The `pop()` method removes the last value from a list, and also provides back (**returns**) this value. Thus, the removed value can be used for additional processing." |
351 | 350 | ]
|
352 | 351 | },
|
353 | 352 | {
|
|
356 | 355 | "metadata": {},
|
357 | 356 | "outputs": [],
|
358 | 357 | "source": [
|
359 |
| - "ss_list = [1500.6, 1501.3, 1498.2, 1504.4] # sound speed in m/sec\n", |
| 358 | + "ss_list = [1500.6, 1501.3, 1498.2, 1504.4] # sound speed in m/s\n", |
360 | 359 | "\n",
|
361 | 360 | "while len(ss_list) > 0:\n",
|
362 | 361 | " \n",
|
363 | 362 | " nr_values = len(ss_list)\n",
|
364 |
| - " print(\"Values in the list: \" + str(nr_values))\n", |
365 |
| - " ss_list.pop()" |
| 363 | + " print(\"Values in the list: \" + str(ss_list))\n", |
| 364 | + " removed_value = ss_list.pop()\n", |
| 365 | + " print(\"Removed element: \" + str(removed_value))" |
366 | 366 | ]
|
367 | 367 | },
|
368 | 368 | {
|
|
385 | 385 | "metadata": {},
|
386 | 386 | "outputs": [],
|
387 | 387 | "source": [
|
388 |
| - "ss_list = [1500.6, 1501.3, 1498.2, 1504.4] # sound speed in m/sec\n", |
| 388 | + "ss_list = [1500.6, 1501.3, 1498.2, 1504.4] # sound speed in m/s\n", |
389 | 389 | "\n",
|
390 | 390 | "while len(ss_list) > 0:\n",
|
391 | 391 | " \n",
|
|
0 commit comments