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..ea6fe03 100644 --- a/book/_toc.yml +++ b/book/_toc.yml @@ -42,15 +42,15 @@ 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: - # - file: notebook_5_figures/PythonNotebook5_matplotlib.ipynb - # - file: exercise_notebooks/exercise_notebook_5_figures.ipynb + - file: notebook_5_figures/PythonNotebook5_first_page.ipynb + sections: + # - file: notebook_5_figures/PythonNotebook5_matplotlib.ipynb + - file: exercise_notebooks/exercise_notebook_5_figures.ipynb # - file: notebook_6_numpy/PythonNotebook6_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/exercise_notebooks/exercise_notebook_5_figures.ipynb b/book/exercise_notebooks/exercise_notebook_5_figures.ipynb index 9652116..0b07c50 100644 --- a/book/exercise_notebooks/exercise_notebook_5_figures.ipynb +++ b/book/exercise_notebooks/exercise_notebook_5_figures.ipynb @@ -261,7 +261,7 @@ "**Label hint:** unfortunately there is no function `plot.zlabel()`.\n", "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", " \n", - "**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", + "**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", "" ] }, diff --git a/book/exercise_notebooks/exercise_notebook_5_figures.zip b/book/exercise_notebooks/exercise_notebook_5_figures.zip index df6fdd3..506f0eb 100644 Binary files a/book/exercise_notebooks/exercise_notebook_5_figures.zip and b/book/exercise_notebooks/exercise_notebook_5_figures.zip differ diff --git a/book/exercise_notebooks/exercise_notebook_6_numpy.ipynb b/book/exercise_notebooks/exercise_notebook_6_numpy.ipynb index 12d7512..200cceb 100644 --- a/book/exercise_notebooks/exercise_notebook_6_numpy.ipynb +++ b/book/exercise_notebooks/exercise_notebook_6_numpy.ipynb @@ -184,7 +184,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, 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": { diff --git a/book/notebook_6_numpy/PythonNotebook6_first_page.ipynb b/book/notebook_6_numpy/PythonNotebook6_first_page.ipynb index 92059b6..ac1f5f7 100644 --- a/book/notebook_6_numpy/PythonNotebook6_first_page.ipynb +++ b/book/notebook_6_numpy/PythonNotebook6_first_page.ipynb @@ -46,7 +46,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/book/notebook_6_numpy/PythonNotebook6_loading_data.ipynb b/book/notebook_6_numpy/PythonNotebook6_loading_data.ipynb index 87f8e62..60d87a2 100644 --- a/book/notebook_6_numpy/PythonNotebook6_loading_data.ipynb +++ b/book/notebook_6_numpy/PythonNotebook6_loading_data.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "tags": [ "theme-remove-input-init" @@ -124,11 +124,18 @@ "2 qt specific humidity (kg/kg) - amount of water, kg of water per kg of total air \n", "3 u wind speed (m/s) in the eastward direction\n", "4 v wind speed (m/s) in the northward direction\n", - "5 TKE_init turblent kinetic energy (m/s) - a measure of the amount of turbulence\n", + "5 TKE_init turbulent kinetic energy (m/s) - a measure of the amount of turbulence\n", "```\n", "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We will revisit this file in one of the exercises, and have a look at working with its contents there. " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -186,10 +193,22 @@ "X,Y = np.meshgrid(x,y)\n", "\n", "# a function to plot\n", - "Z = X**2 + Y**2\n", - "# Z has the same shape as X and Y\n", - "# each element of Z is computed from the corresponding elements of X and Y\n", - "\n", + "Z = X**2 + Y**2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`Z` has the same shape as `X` and `Y`, and each element of `Z` is computed from the corresponding elements of `X` and `Y`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "ax = plt.axes(projection='3d')\n", "\n", "ax.plot_surface(X,Y,Z)\n", @@ -222,7 +241,9 @@ "plt.gca().set_aspect('equal') # gca() stands for get current axis\n", "\n", "plt.pcolormesh(X,Y,Z) # plot Z as function of X and Y, using colors\n", - "plt.colorbar() # shows the color bar\n", + "cbar = plt.colorbar() # shows the color bar\n", + "\n", + "cbar.set_label(\"Value of Z\") # put a label on the colourbar so it is understandable\n", "\n", "plt.show()" ] @@ -242,7 +263,7 @@ ], "metadata": { "kernelspec": { - "display_name": "base", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -256,7 +277,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.10.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/book/notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb b/book/notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb index 5364a66..4623c25 100644 --- a/book/notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb +++ b/book/notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -42,7 +42,7 @@ "source": [ "Now functions in numpy are accessible as `np.array()` for example.\n", "\n", - "The part `as np` is not necessary, but commonly done for slightly shorter code than typing `numpy.array()` every time.\n" + "The part `as np` is not necessary, but commonly done for slightly shorter code than typing `numpy.array()` every time (just like we imported `matplotlib.pyplot` as `plt` last week).\n" ] }, { @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -216,12 +216,24 @@ "This creates an array of `N` equally spaced numbers where both start and end are included.\n", "**Use whichever is more convenient.**\n", "\n", + "```{admonition} Attention\n", + ":class: danger\n", + "\n", + "Choose `N` carefully. `np.arange(0, 1, 0.1)` yields an array with 10 numbers: `[0, 0.1, 0.2, ..., 0.9]`. `np.linspace(0, 1, 10)` also yields an array with 10 numbers, but different ones: `[0, 0.1111111, 0.22222222, ..., 0.88888889, 1]`. This is the difference between including and excluding the end point. \n", + "+++\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "Let's now create an array `x` with equally spaced points, and evaluate a function on it." ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -234,6 +246,19 @@ "metadata": {}, "source": [ "Note we used `np.cos()`, which is similar to `math.cos()` but can work on numpy arrays.\n", + "\n", + "```{admonition} Attention\n", + ":class: danger\n", + "\n", + "Functions from the `math` module do not work on numpy arrays as they expect single numbers. Always use their `numpy` equivalents.\n", + "+++\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "Now each element of `y1` equals the cosine of the corresponding element in `x`. \n", "\n", "Use the cell below to print `y1` to check." @@ -241,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -257,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -300,7 +325,7 @@ ], "metadata": { "kernelspec": { - "display_name": "base", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -314,7 +339,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.10.0" }, "latex_envs": { "LaTeX_envs_menu_present": true,