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
95 changes: 82 additions & 13 deletions Intermediate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3961,29 +3961,33 @@
},
{
"cell_type": "markdown",
"id": "9e1c43a8",
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": "slide"
"slide_type": ""
},
"tags": [],
"editable": false
"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": {
"slideshow": {
"slide_type": ""
"slide_type": "slide"
},
"tags": [],
"editable": false
},
"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_"
]
},
{
Expand All @@ -3993,6 +3997,7 @@
"slideshow": {
"slide_type": ""
},
"tags": [],
"editable": false
},
"source": [
Expand Down Expand Up @@ -4087,6 +4092,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,
Expand All @@ -4099,24 +4169,23 @@
},
"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))"
]
},
{
"cell_type": "markdown",
"id": "d1cc0855",
"metadata": {
"slideshow": {
"slide_type": "fragment"
"slide_type": "slide"
},
"tags": [],
"editable": false
},
"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..."
]
},
Expand Down
111 changes: 98 additions & 13 deletions Intermediate_full.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4077,27 +4077,32 @@
},
{
"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": {
"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_"
]
},
{
Expand All @@ -4106,7 +4111,8 @@
"metadata": {
"slideshow": {
"slide_type": ""
}
},
"tags": []
},
"source": [
"There are several ways of importing _modules_:"
Expand Down Expand Up @@ -4200,6 +4206,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": [
"<ins>Notes:</ins>\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,
Expand All @@ -4212,23 +4298,22 @@
},
"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))"
]
},
{
"cell_type": "markdown",
"id": "d1cc0855",
"metadata": {
"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..."
]
},
Expand Down