From d3e14842a82c0a811f9b33cc5d501bede2f5af32 Mon Sep 17 00:00:00 2001 From: Niolon Date: Fri, 27 Feb 2026 11:13:09 +0000 Subject: [PATCH] update module section --- Intermediate.ipynb | 95 +++++++++++++++++++++---- Intermediate_full.ipynb | 152 +++++++++++++++++++++++++++------------- 2 files changed, 185 insertions(+), 62 deletions(-) diff --git a/Intermediate.ipynb b/Intermediate.ipynb index 32e64a0..a0fab5a 100644 --- a/Intermediate.ipynb +++ b/Intermediate.ipynb @@ -4470,29 +4470,33 @@ }, { "cell_type": "markdown", - "id": "9e1c43a8", + "id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e", "metadata": { "editable": false, "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [] }, "source": [ - "## Importing _modules_" + "Modules let us:\n", + "- Use code that others have already written and tested\n", + "- Load in only the functionality we need\n", + "- Know exactly where a piece of functionality comes from" ] }, { "cell_type": "markdown", - "id": "eed64626", + "id": "9e1c43a8", "metadata": { "editable": false, "slideshow": { - "slide_type": "" - } + "slide_type": "slide" + }, + "tags": [] }, "source": [ - "Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet." + "## Importing _modules_" ] }, { @@ -4502,7 +4506,8 @@ "editable": false, "slideshow": { "slide_type": "" - } + }, + "tags": [] }, "source": [ "There are several ways of importing _modules_:" @@ -4601,6 +4606,71 @@ "print(square_root(25))" ] }, + { + "cell_type": "markdown", + "id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## Why Namespaces Matter\n", + "\n", + "The word `log` means two very different things:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4de72e4c-2795-4b4d-8542-10729a74f44a", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import logging\n", + "\n", + "math.log(100, 10) # A logarithm: 2.0\n", + "\n", + "logging.log(20, \"Hello\") # A logbook entry: records a message" + ] + }, + { + "cell_type": "markdown", + "id": "36f46ead-b9a1-481f-8798-edbfc58b5504", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "Without namespaces, Python, and your reader, couldn't tell them apart." + ] + }, + { + "cell_type": "markdown", + "id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "fragment" + }, + "tags": [] + }, + "source": [ + "----\n", + "This is why importing a whole module into namespace is usually a bad idea:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -4614,10 +4684,9 @@ }, "outputs": [], "source": [ - "# import all functions, variables, and classes from a module into the global namespace\n", - "# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n", + "# import all functions, variables, and classes from a module - better to avoid this\n", "from math import *\n", - "print(int(sqrt(4.0)))" + "print(sqrt(4.0))" ] }, { @@ -4626,12 +4695,12 @@ "metadata": { "editable": false, "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" }, "tags": [] }, "source": [ - "---\n", + "## Getting help\n", "To get help on a module at the Python shell, import it the whole (the very first way), then you can..." ] }, diff --git a/Intermediate_full.ipynb b/Intermediate_full.ipynb index 03331a1..f411e01 100644 --- a/Intermediate_full.ipynb +++ b/Intermediate_full.ipynb @@ -4875,29 +4875,33 @@ }, { "cell_type": "markdown", - "id": "9e1c43a8", + "id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e", "metadata": { "editable": true, "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [] }, "source": [ - "## Importing _modules_" + "Modules let us:\n", + "- Use code that others have already written and tested\n", + "- Load in only the functionality we need\n", + "- Know exactly where a piece of functionality comes from" ] }, { "cell_type": "markdown", - "id": "eed64626", + "id": "9e1c43a8", "metadata": { "editable": true, "slideshow": { - "slide_type": "" - } + "slide_type": "slide" + }, + "tags": [] }, "source": [ - "Python comes with hundreds of _modules_ doing all sorts of things. Also, many 3rd-party modules are available to download from the Internet." + "## Importing _modules_" ] }, { @@ -4907,7 +4911,8 @@ "editable": true, "slideshow": { "slide_type": "" - } + }, + "tags": [] }, "source": [ "There are several ways of importing _modules_:" @@ -5006,6 +5011,86 @@ "print(square_root(25))" ] }, + { + "cell_type": "markdown", + "id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## Why Namespaces Matter\n", + "\n", + "The word `log` means two very different things:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4de72e4c-2795-4b4d-8542-10729a74f44a", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import logging\n", + "\n", + "math.log(100, 10) # A logarithm: 2.0\n", + "\n", + "logging.log(20, \"Hello\") # A logbook entry: records a message" + ] + }, + { + "cell_type": "markdown", + "id": "36f46ead-b9a1-481f-8798-edbfc58b5504", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "Without namespaces, Python, and your reader, couldn't tell them apart." + ] + }, + { + "cell_type": "markdown", + "id": "ccbef34a-9226-4735-859d-378640ee9cc7", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "notes" + }, + "tags": [] + }, + "source": [ + "Notes:\n", + "- In JupyterLite, logging output is suppressed by default unless you explicitly configure a handler." + ] + }, + { + "cell_type": "markdown", + "id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "fragment" + }, + "tags": [] + }, + "source": [ + "----\n", + "This is why importing a whole module into namespace is usually a bad idea:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -5019,10 +5104,9 @@ }, "outputs": [], "source": [ - "# import all functions, variables, and classes from a module into the global namespace\n", - "# - better to avoid this as some names from the module can interfere with your own variable names in the global namespace\n", + "# import all functions, variables, and classes from a module - better to avoid this\n", "from math import *\n", - "print(int(sqrt(4.0)))" + "print(sqrt(4.0))" ] }, { @@ -5031,12 +5115,12 @@ "metadata": { "editable": true, "slideshow": { - "slide_type": "fragment" + "slide_type": "slide" }, "tags": [] }, "source": [ - "---\n", + "## Getting help\n", "To get help on a module at the Python shell, import it the whole (the very first way), then you can..." ] }, @@ -5904,7 +5988,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "b10a1b92", "metadata": { "editable": true, @@ -5913,15 +5997,7 @@ }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Created example.csv with x and cos(x) values\n" - ] - } - ], + "outputs": [], "source": [ "import csv, math\n", "\n", @@ -5958,7 +6034,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "7d2b75d2-f0a2-490e-be7d-28dad71ef05c", "metadata": { "editable": true, @@ -5967,20 +6043,7 @@ }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "First few lines of the file:\n", - "x_axis,y_axis\n", - "0.0,1.0\n", - "0.1,0.9950041652780258\n", - "0.2,0.9800665778412416\n", - "0.30000000000000004,0.955336489125606\n" - ] - } - ], + "outputs": [], "source": [ "# Show the first few lines to demonstrate what we created\n", "print(\"First few lines of the file:\")\n", @@ -5991,7 +6054,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "9af8c87f-62db-4a04-86d8-d51428395210", "metadata": { "editable": true, @@ -6000,16 +6063,7 @@ }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The value of Y for X==1.0 saved in the file:\n", - "0.5403023058681398\n" - ] - } - ], + "outputs": [], "source": [ "# Show the value of Y for X==1.0 saved in the file\n", "print(\"The value of Y for X==1.0 saved in the file:\")\n",