diff --git a/book/.DS_Store b/book/.DS_Store index 92fabb4..09b04c5 100644 Binary files a/book/.DS_Store and b/book/.DS_Store differ diff --git a/book/_toc.yml b/book/_toc.yml index 0f6967c..24cc574 100644 --- a/book/_toc.yml +++ b/book/_toc.yml @@ -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: diff --git a/book/exercise_notebooks/exercise_notebook_3_structures_loops.zip b/book/exercise_notebooks/exercise_notebook_3_structures_loops.zip index 842f169..81cb97a 100644 Binary files a/book/exercise_notebooks/exercise_notebook_3_structures_loops.zip and b/book/exercise_notebooks/exercise_notebook_3_structures_loops.zip differ diff --git a/book/exercise_notebooks/exercise_notebook_4_debugging.ipynb b/book/exercise_notebooks/exercise_notebook_4_debugging.ipynb index 7bec943..82510a4 100644 --- a/book/exercise_notebooks/exercise_notebook_4_debugging.ipynb +++ b/book/exercise_notebooks/exercise_notebook_4_debugging.ipynb @@ -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" ] }, { @@ -54,7 +58,7 @@ "\n", "Use your is_prime() 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 for loop to fill the list.\n", "\n" ] }, @@ -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}')" ] }, { @@ -81,7 +85,7 @@ } }, "source": [ - "
(Fixing) Exercise 4.3

Fix the syntax errors so it prints \"AES\" without removing the variable that holds it. You'll need to fix 2 errors.
" + "
(Fixing) Exercise 4.3

Fix the syntax errors so it prints \"EC&T\" without removing the variable that holds it. You'll need to fix 2 errors.
" ] }, { @@ -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())" @@ -111,25 +115,14 @@ "\n", "The factorial n! is defined as n! = n * (n - 1) * (n - 2) * ... * 2 * 1. The function uses the fact that if n > 0, n! = n * (n - 1)!. This is an example of a _recursive_ function, a function that calls itself.\n", "\n", - "The code below calls the function \"factorial\" with the number 4 as input. The factorial of 4 is 4 * 3 * 2 * 1, which is 24, but the code below prints 262144. Find and fix the semantic error in this function." + "The code below calls the function factorial 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", @@ -140,13 +133,6 @@ "\n", "factorial(4)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -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, diff --git a/book/exercise_notebooks/exercise_notebook_4_debugging.zip b/book/exercise_notebooks/exercise_notebook_4_debugging.zip new file mode 100644 index 0000000..a269e98 Binary files /dev/null and b/book/exercise_notebooks/exercise_notebook_4_debugging.zip differ diff --git a/book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb b/book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb index b004e1a..b28bdfc 100644 --- a/book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb +++ b/book/notebook_3_structures_loops/PythonNotebook3_loops.ipynb @@ -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 for 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 .append (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 }')" ] }, @@ -651,7 +683,7 @@ } }, "source": [ - "As you can see, with the help of the continue keyword we managed to skip some of the iterations. Also worth noting that $0$ is divisible by any number, for that reason the calculate_cool_function(i) at i = 0 didn't run." + "As you can see, with the help of the continue keyword we skipped the execution of calculate_cool_function(i)< for all even $i$. Also worth noting that $0$ is divisible by any number, for that reason the calculate_cool_function(i) at i = 0 didn't run." ] }, { @@ -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 for loops\n", + "- use while loops\n", + "- use break and continue in loops\n", + "- understand range()" + ] } ], "metadata": { diff --git a/book/notebook_4_debugging/PythonNotebook4_debugging.ipynb b/book/notebook_4_debugging/PythonNotebook4_debugging.ipynb index b805833..02b56fe 100644 --- a/book/notebook_4_debugging/PythonNotebook4_debugging.ipynb +++ b/book/notebook_4_debugging/PythonNotebook4_debugging.ipynb @@ -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 for loops\n", - "- use while loops\n", - "- use break and continue in loops\n", - "- understand range()\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" }, @@ -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, @@ -342,11 +341,6 @@ "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false - }, - "vscode": { - "interpreter": { - "hash": "1fe2f2b718b1108b9c4176932db8a0ead471245140baaa21ea96a4066683e6b2" - } } }, "nbformat": 4, diff --git a/book/notebook_4_debugging/PythonNotebook4_first_page copy.ipynb b/book/notebook_4_debugging/PythonNotebook4_first_page.ipynb similarity index 84% rename from book/notebook_4_debugging/PythonNotebook4_first_page copy.ipynb rename to book/notebook_4_debugging/PythonNotebook4_first_page.ipynb index 9a24edb..2a22138 100644 --- a/book/notebook_4_debugging/PythonNotebook4_first_page copy.ipynb +++ b/book/notebook_4_debugging/PythonNotebook4_first_page.ipynb @@ -17,7 +17,7 @@ "# 4: Debugging\n", "
\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 problem-solving expertise, 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 problem-solving expertise, laying a strong foundation for tackling complex Python projects. \n", "\n", "```{admonition} Attention\n", ":class: danger\n", @@ -27,6 +27,11 @@ "+++\n", "```" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": {