Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified book/.DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions book/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ parts:
- file: exercise_notebooks/exercise_notebook_3_structures_loops.ipynb
# - file: exercise_notebooks/exercise_notebook_3_loops_debugging.ipynb

# - file: notebook_4_debugging/PythonNotebook4_first_page.ipynb
# sections:
# - file: notebook_4_debugging/PythonNotebook4_debugging.ipynb
# - file: exercise_notebooks/exercise_notebook_4_debugging.ipynb
- file: notebook_4_debugging/PythonNotebook4_first_page.ipynb
sections:
- file: notebook_4_debugging/PythonNotebook4_debugging.ipynb
- file: exercise_notebooks/exercise_notebook_4_debugging.ipynb

# - file: notebook_5_figures/PythonNotebook5_first_page.ipynb
# sections:
Expand Down
Binary file modified book/exercise_notebooks/exercise_notebook_3_structures_loops.zip
Binary file not shown.
42 changes: 14 additions & 28 deletions book/exercise_notebooks/exercise_notebook_4_debugging.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
"outputs": [],
"source": [
"def is_prime(n):\n",
" return True # or False"
" \"\"\" \n",
" Check if a number is prime. The input argument n must be a positive integer.\n",
" The function returns True if n is prime, and False otherwise.\n",
" \"\"\"\n",
" ...\n"
]
},
{
Expand All @@ -54,7 +58,7 @@
"\n",
"Use your <code>is_prime()</code> function to create a list of all primes $< 1000$. What is the sum of all primes $< 1000$?\n",
"\n",
"Hint: is there a nice way to calculate the sum of the elements in a list?\n",
"Hint: you will need a <code>for</code> loop to fill the list.\n",
"</div></div>\n"
]
},
Expand All @@ -67,8 +71,8 @@
"prime_list = ...\n",
"prime_sum = ...\n",
"\n",
"print(prime_list)\n",
"print(prime_sum)"
"print(f'List of primes: {prime_list}\\n')\n",
"print(f'Sum of primes: {prime_sum}')"
]
},
{
Expand All @@ -81,7 +85,7 @@
}
},
"source": [
"<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>"
"<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>"
]
},
{
Expand All @@ -91,7 +95,7 @@
"outputs": [],
"source": [
"def get_abbreviation():\n",
" my abbreviation = \"AES\"\n",
" my abbreviation = \"EC&T\"\n",
" return my_abbreviation\n",
" \n",
"print(get_abbreviation())"
Expand All @@ -111,25 +115,14 @@
"\n",
"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",
"\n",
"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."
"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)?"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"262144"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"def factorial(x):\n",
" \"returns the factorial of x\"\n",
Expand All @@ -140,13 +133,6 @@
"\n",
"factorial(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -165,7 +151,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.13.2"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down
Binary file not shown.
54 changes: 50 additions & 4 deletions book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,57 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"f = [-2, 4, 16, 40, 82, 148]\n"
]
}
],
"source": [
"x = [0, 1, 2, 3, 4, 5]\n",
"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",
"print(f'f = {f }')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"\n",
"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)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In this cycle we added f(0) = -2\n",
"In this cycle we added f(1) = 4\n",
"In this cycle we added f(2) = 16\n",
"In this cycle we added f(3) = 40\n",
"In this cycle we added f(4) = 82\n",
"In this cycle we added f(5) = 148\n",
"f(x) = [-2, 4, 16, 40, 82, 148]\n"
]
}
],
"source": [
"x = [0, 1, 2, 3, 4, 5]\n",
"f = []\n",
"for i in x:\n",
" f.append(x[i] ** 3 + 5*x[i] - 2)\n",
" print(f'In this cycle we added f({x[i]}) = {f[i]}')\n",
"print(f'f = {f }')"
]
},
Expand Down Expand Up @@ -651,7 +683,7 @@
}
},
"source": [
"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."
"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."
]
},
{
Expand Down Expand Up @@ -705,6 +737,20 @@
"for i in range(3, 17, 2):\n",
" print(f'i is {i}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### After this Chapter you should be able to:\n",
"\n",
"- understand the differences between **`list`**, **`tuple`**, and **`dict`**\n",
"- slice lists and tuples\n",
"- use <code>for</code> loops\n",
"- use <code>while</code> loops\n",
"- use <code>break</code> and <code>continue</code> in loops\n",
"- understand <code>range()</code>"
]
}
],
"metadata": {
Expand Down
20 changes: 7 additions & 13 deletions book/notebook_4_debugging/PythonNotebook4_debugging.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,19 @@
"source": [
"#### After this Chapter you should be able to:\n",
"\n",
"- understand the differences between **`list`**, **`tuple`**, and **`dict`**\n",
"- slice lists and tuples\n",
"- use <code>for</code> loops\n",
"- use <code>while</code> loops\n",
"- use <code>break</code> and <code>continue</code> in loops\n",
"- understand <code>range()</code>\n",
"- know different types of errors\n",
"- have a plan when debugging your code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -323,7 +322,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
"version": "3.13.2"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand All @@ -342,11 +341,6 @@
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"vscode": {
"interpreter": {
"hash": "1fe2f2b718b1108b9c4176932db8a0ead471245140baaa21ea96a4066683e6b2"
}
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"# 4: Debugging\n",
"<br>\n",
"\n",
"In this chapter, we introduce debugging. We cover the three main types of errors—syntax, runtime, and logical—and strategies for identifying and resolving issues in your code. Mastering these topics will refine your <b>problem-solving expertise</b>, laying a strong foundation for tackling complex Python projects. \n",
"In this chapter, we introduce debugging. We cover the three main types of errors—syntax, runtime, and semantic—and strategies for identifying and resolving issues in your code. Mastering these topics will refine your <b>problem-solving expertise</b>, laying a strong foundation for tackling complex Python projects. \n",
"\n",
"```{admonition} Attention\n",
":class: danger\n",
Expand All @@ -27,6 +27,11 @@
"+++\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down