Skip to content

Commit bc3bed2

Browse files
Merge pull request #26 from TeachBooks/main
main into draft-book
2 parents 7c75cdf + 0c0d771 commit bc3bed2

14 files changed

+152
-75
lines changed

book/.DS_Store

0 Bytes
Binary file not shown.

book/_toc.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ parts:
4242
- file: exercise_notebooks/exercise_notebook_3_structures_loops.ipynb
4343
# - file: exercise_notebooks/exercise_notebook_3_loops_debugging.ipynb
4444

45-
# - file: notebook_4_debugging/PythonNotebook4_first_page.ipynb
46-
# sections:
47-
# - file: notebook_4_debugging/PythonNotebook4_debugging.ipynb
48-
# - file: exercise_notebooks/exercise_notebook_4_debugging.ipynb
45+
- file: notebook_4_debugging/PythonNotebook4_first_page.ipynb
46+
sections:
47+
- file: notebook_4_debugging/PythonNotebook4_debugging.ipynb
48+
- file: exercise_notebooks/exercise_notebook_4_debugging.ipynb
4949

50-
# - file: notebook_5_figures/PythonNotebook5_first_page.ipynb
51-
# sections:
52-
# - file: notebook_5_figures/PythonNotebook5_matplotlib.ipynb
53-
# - file: exercise_notebooks/exercise_notebook_5_figures.ipynb
50+
- file: notebook_5_figures/PythonNotebook5_first_page.ipynb
51+
sections:
52+
# - file: notebook_5_figures/PythonNotebook5_matplotlib.ipynb
53+
- file: exercise_notebooks/exercise_notebook_5_figures.ipynb
5454

5555
# - file: notebook_6_numpy/PythonNotebook6_first_page.ipynb
5656
# sections:
-7 Bytes
Binary file not shown.

book/exercise_notebooks/exercise_notebook_4_debugging.ipynb

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
"outputs": [],
4343
"source": [
4444
"def is_prime(n):\n",
45-
" return True # or False"
45+
" \"\"\" \n",
46+
" Check if a number is prime. The input argument n must be a positive integer.\n",
47+
" The function returns True if n is prime, and False otherwise.\n",
48+
" \"\"\"\n",
49+
" ...\n"
4650
]
4751
},
4852
{
@@ -54,7 +58,7 @@
5458
"\n",
5559
"Use your <code>is_prime()</code> function to create a list of all primes $< 1000$. What is the sum of all primes $< 1000$?\n",
5660
"\n",
57-
"Hint: is there a nice way to calculate the sum of the elements in a list?\n",
61+
"Hint: you will need a <code>for</code> loop to fill the list.\n",
5862
"</div></div>\n"
5963
]
6064
},
@@ -67,8 +71,8 @@
6771
"prime_list = ...\n",
6872
"prime_sum = ...\n",
6973
"\n",
70-
"print(prime_list)\n",
71-
"print(prime_sum)"
74+
"print(f'List of primes: {prime_list}\\n')\n",
75+
"print(f'Sum of primes: {prime_sum}')"
7276
]
7377
},
7478
{
@@ -81,7 +85,7 @@
8185
}
8286
},
8387
"source": [
84-
"<div class=\"alert alert-block alert-info\"><b>(Fixing) Exercise 4.3 </b><br><br><div style=\"text-align: justify\">Fix the syntax errors so it prints <code>\"AES\"</code> without removing the variable that holds it. You'll need to fix 2 errors.</div></div>"
88+
"<div class=\"alert alert-block alert-info\"><b>(Fixing) Exercise 4.3 </b><br><br><div style=\"text-align: justify\">Fix the syntax errors so it prints <code>\"EC&T\"</code> without removing the variable that holds it. You'll need to fix 2 errors.</div></div>"
8589
]
8690
},
8791
{
@@ -91,7 +95,7 @@
9195
"outputs": [],
9296
"source": [
9397
"def get_abbreviation():\n",
94-
" my abbreviation = \"AES\"\n",
98+
" my abbreviation = \"EC&T\"\n",
9599
" return my_abbreviation\n",
96100
" \n",
97101
"print(get_abbreviation())"
@@ -111,25 +115,14 @@
111115
"\n",
112116
"The factorial n! is defined as <code>n! = n * (n - 1) * (n - 2) * ... * 2 * 1</code>. The function uses the fact that if n > 0, <code>n! = n * (n - 1)!</code>. This is an example of a _recursive_ function, a function that calls itself.</div></div>\n",
113117
"\n",
114-
"The code below calls the function \"factorial\" with the number 4 as input. The factorial of 4 is <code>4 * 3 * 2 * 1</code>, which is 24, but the code below prints 262144. Find and fix the semantic error in this function."
118+
"The code below calls the function <code>factorial</code> with the number 4 as input. The factorial of 4 is $4 \\cdot 3 \\cdot 2 \\cdot 1 = 24$, but the code below prints 262144. Find and fix the error in this function. What kind of error is this (syntax, runtime, semantic)?"
115119
]
116120
},
117121
{
118122
"cell_type": "code",
119-
"execution_count": 5,
123+
"execution_count": null,
120124
"metadata": {},
121-
"outputs": [
122-
{
123-
"data": {
124-
"text/plain": [
125-
"262144"
126-
]
127-
},
128-
"execution_count": 5,
129-
"metadata": {},
130-
"output_type": "execute_result"
131-
}
132-
],
125+
"outputs": [],
133126
"source": [
134127
"def factorial(x):\n",
135128
" \"returns the factorial of x\"\n",
@@ -140,13 +133,6 @@
140133
"\n",
141134
"factorial(4)"
142135
]
143-
},
144-
{
145-
"cell_type": "code",
146-
"execution_count": null,
147-
"metadata": {},
148-
"outputs": [],
149-
"source": []
150136
}
151137
],
152138
"metadata": {
@@ -165,7 +151,7 @@
165151
"name": "python",
166152
"nbconvert_exporter": "python",
167153
"pygments_lexer": "ipython3",
168-
"version": "3.12.7"
154+
"version": "3.13.2"
169155
},
170156
"latex_envs": {
171157
"LaTeX_envs_menu_present": true,
1.93 KB
Binary file not shown.

book/exercise_notebooks/exercise_notebook_5_figures.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
"**Label hint:** unfortunately there is no function `plot.zlabel()`.\n",
262262
"Instead you have to use `ax.set_zlabel('...')`. You can use `ax.set_xlabel()` and `ax.set_ylabel()` too. Your script looks nicer if you handle all axes in the same way.\n",
263263
" \n",
264-
"**Note:** the z-label doesn't necessary show up anyway, perhaps it's hidden behind the plot. If you could rotate the plot it might show up. For now let's choose our battles and not worry about this.\n",
264+
"**Note:** the z-label doesn't necessarily show up anyway, perhaps it's hidden behind the plot. If you could rotate the plot it might show up. For now let's choose our battles and not worry about this.\n",
265265
"</div></div>"
266266
]
267267
},
-30 Bytes
Binary file not shown.

book/exercise_notebooks/exercise_notebook_6_numpy.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
"name": "python",
185185
"nbconvert_exporter": "python",
186186
"pygments_lexer": "ipython3",
187-
"version": "3.11.5"
187+
"version": "3.10.0"
188188
},
189189
"latex_envs": {
190190
"LaTeX_envs_menu_present": true,

book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,57 @@
278278
},
279279
{
280280
"cell_type": "code",
281-
"execution_count": null,
281+
"execution_count": 1,
282282
"metadata": {},
283-
"outputs": [],
283+
"outputs": [
284+
{
285+
"name": "stdout",
286+
"output_type": "stream",
287+
"text": [
288+
"f = [-2, 4, 16, 40, 82, 148]\n"
289+
]
290+
}
291+
],
284292
"source": [
285293
"x = [0, 1, 2, 3, 4, 5]\n",
286294
"f = [x[0] ** 3 + 5*x[0] - 2, x[1] ** 3 + 5*x[1] - 2, x[2] ** 3 + 5*x[2] - 2, x[3] ** 3 + 5*x[3] - 2, x[4] ** 3 + 5*x[4] - 2, x[5] ** 3 + 5*x[5] - 2]\n",
287295
"print(f'f = {f }')"
288296
]
289297
},
298+
{
299+
"cell_type": "markdown",
300+
"metadata": {},
301+
"source": [
302+
"You can see above, that a list was created by using the equation for every item in $x$. A more convenient way is to use a <code>for</code> loop, as shown below. This is especially important if you need to cycle over a large number of items/values.\n",
303+
"\n",
304+
"You can see that we first create an empty list $f$; in each cycle of the loop, we will then add a new item to $f$. In order to do so, we use <code>.append</code> (see Section 3.1)."
305+
]
306+
},
290307
{
291308
"cell_type": "code",
292309
"execution_count": null,
293310
"metadata": {},
294-
"outputs": [],
311+
"outputs": [
312+
{
313+
"name": "stdout",
314+
"output_type": "stream",
315+
"text": [
316+
"In this cycle we added f(0) = -2\n",
317+
"In this cycle we added f(1) = 4\n",
318+
"In this cycle we added f(2) = 16\n",
319+
"In this cycle we added f(3) = 40\n",
320+
"In this cycle we added f(4) = 82\n",
321+
"In this cycle we added f(5) = 148\n",
322+
"f(x) = [-2, 4, 16, 40, 82, 148]\n"
323+
]
324+
}
325+
],
295326
"source": [
296327
"x = [0, 1, 2, 3, 4, 5]\n",
297328
"f = []\n",
298329
"for i in x:\n",
299330
" f.append(x[i] ** 3 + 5*x[i] - 2)\n",
331+
" print(f'In this cycle we added f({x[i]}) = {f[i]}')\n",
300332
"print(f'f = {f }')"
301333
]
302334
},
@@ -651,7 +683,7 @@
651683
}
652684
},
653685
"source": [
654-
"As you can see, with the help of the <b><code>continue</code></b> keyword we managed to skip some of the iterations. Also worth noting that $0$ is divisible by any number, for that reason the <b><code>calculate_cool_function(i)</code></b> at <b><code>i = 0</code></b> didn't run."
686+
"As you can see, with the help of the <b><code>continue</code></b> keyword we skipped the execution of <code>calculate_cool_function(i)<</code> for all even $i$. Also worth noting that $0$ is divisible by any number, for that reason the <b><code>calculate_cool_function(i)</code></b> at <b><code>i = 0</code></b> didn't run."
655687
]
656688
},
657689
{
@@ -705,6 +737,20 @@
705737
"for i in range(3, 17, 2):\n",
706738
" print(f'i is {i}')"
707739
]
740+
},
741+
{
742+
"cell_type": "markdown",
743+
"metadata": {},
744+
"source": [
745+
"#### After this Chapter you should be able to:\n",
746+
"\n",
747+
"- understand the differences between **`list`**, **`tuple`**, and **`dict`**\n",
748+
"- slice lists and tuples\n",
749+
"- use <code>for</code> loops\n",
750+
"- use <code>while</code> loops\n",
751+
"- use <code>break</code> and <code>continue</code> in loops\n",
752+
"- understand <code>range()</code>"
753+
]
708754
}
709755
],
710756
"metadata": {

book/notebook_4_debugging/PythonNotebook4_debugging.ipynb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,19 @@
296296
"source": [
297297
"#### After this Chapter you should be able to:\n",
298298
"\n",
299-
"- understand the differences between **`list`**, **`tuple`**, and **`dict`**\n",
300-
"- slice lists and tuples\n",
301-
"- use <code>for</code> loops\n",
302-
"- use <code>while</code> loops\n",
303-
"- use <code>break</code> and <code>continue</code> in loops\n",
304-
"- understand <code>range()</code>\n",
305299
"- know different types of errors\n",
306300
"- have a plan when debugging your code"
307301
]
302+
},
303+
{
304+
"cell_type": "markdown",
305+
"metadata": {},
306+
"source": []
308307
}
309308
],
310309
"metadata": {
311310
"kernelspec": {
312-
"display_name": "Python 3 (ipykernel)",
311+
"display_name": "base",
313312
"language": "python",
314313
"name": "python3"
315314
},
@@ -323,7 +322,7 @@
323322
"name": "python",
324323
"nbconvert_exporter": "python",
325324
"pygments_lexer": "ipython3",
326-
"version": "3.10.0"
325+
"version": "3.13.2"
327326
},
328327
"latex_envs": {
329328
"LaTeX_envs_menu_present": true,
@@ -342,11 +341,6 @@
342341
"latex_user_defs": false,
343342
"report_style_numbering": false,
344343
"user_envs_cfg": false
345-
},
346-
"vscode": {
347-
"interpreter": {
348-
"hash": "1fe2f2b718b1108b9c4176932db8a0ead471245140baaa21ea96a4066683e6b2"
349-
}
350344
}
351345
},
352346
"nbformat": 4,

0 commit comments

Comments
 (0)