Skip to content

Commit 8a4e728

Browse files
committed
update modules notebook with fixed formatting
1 parent 8554801 commit 8a4e728

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

L4/modules.ipynb

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
"source": [
77
"# Loading and using modules\n",
88
"\n",
9+
"```{attention}\n",
910
"Finnish university students are encouraged to use the CSC Notebooks platform.<br/>\n",
1011
"<a href=\"https://notebooks.csc.fi/#/blueprint/7e62ac3bddf74483b7ac7333721630e2\"><img alt=\"CSC badge\" src=\"https://img.shields.io/badge/launch-CSC%20notebook-blue.svg\" style=\"vertical-align:text-bottom\"></a>\n",
1112
"\n",
1213
"Others can follow the lesson and fill in their student notebooks using Binder.<br/>\n",
1314
"<a href=\"https://mybinder.org/v2/gh/geo-python/notebooks/master?urlpath=lab/tree/L4/modules.ipynb\"><img alt=\"Binder badge\" src=\"https://img.shields.io/badge/launch-binder-red.svg\" style=\"vertical-align:text-bottom\"></a>\n",
15+
"```\n",
1416
"\n",
1517
"## Modules, packages and libraries?\n",
1618
"\n",
17-
"Python [module](https://docs.python.org/3/tutorial/modules.html#modules) is simply a Python `.py` file that contains function definitions and other statements. Python [packages](https://docs.python.org/3/tutorial/modules.html#packages) are a way of organizing modules into larger entities. \n",
19+
"Python [module](https://docs.python.org/3/tutorial/modules.html#modules) refers to a piece of Python code that is designed to execute a spesific task. Technically, modules are simply Python script files (file extension`.py`) that contain function definitions and other statements. Python [packages](https://docs.python.org/3/tutorial/modules.html#packages) are a way of organizing modules into larger entities. \n",
1820
"\n",
1921
"Modules and packages are similar to what are more generally called libraries in programming languages, which again contain code related to a specific task such as mathematical operations.\n",
2022
"There are a *HUGE* number of Python modules/packages, and many of them greatly extend what can be done in a normal Python program.\n",
@@ -57,15 +59,25 @@
5759
"cell_type": "markdown",
5860
"metadata": {},
5961
"source": [
60-
"```{admonition}Built-in functions\n",
61-
"[Built-in functions](https://docs.python.org/3/library/functions.html) such as `print()` are always available without importing anything:\n",
62-
" \n",
63-
" ```python\n",
64-
" print(\"Hello world!\")\n",
65-
" ```\n",
66-
" \n",
67-
"Technically, the built-in functions belong to a module called `builtins`.\n",
68-
"```"
62+
"### Built-in functions\n",
63+
"\n",
64+
"[Built-in functions](https://docs.python.org/3/library/functions.html) such as `print()` are always available without importing anything:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"print(\"Hello world!\")"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"Technically, the built-in functions belong to a module called `builtins`."
6981
]
7082
},
7183
{
@@ -105,8 +117,7 @@
105117
"cell_type": "markdown",
106118
"metadata": {},
107119
"source": [
108-
"In this example we now see that when the math module is imported, it is imported to be usable with the name `m` instead of `math`.\n",
109-
"It doesn't matter much in our toy example here since math is not a long module name, but we will see other examples later in the course where renaming the modules is very helpful. For example, next week we will start using the `pandas` library for data analysis. It is customary to import pandas as `pd`."
120+
"Here, we imported the `math` module to be usable with the name `m` instead of `math`. We will see other examples later in the course where using an alternate name is rather useful. For example, next week we will start using the `pandas` library for data analysis. It is customary to import pandas as `pd`:"
110121
]
111122
},
112123
{
@@ -149,7 +160,7 @@
149160
"cell_type": "markdown",
150161
"metadata": {},
151162
"source": [
152-
"Though this can be useful, it has the drawback that **the imported function could conflict with other built-in or imported function names**, and you lose the information about which module contains the function.\n",
163+
"Though this can be useful, it has the drawback that **the imported function could conflict with other built-in or imported function names**, and you lose the information about which module contains the imported function.\n",
153164
"You should only do this when you truly need to.\n",
154165
"\n",
155166
"### Importing a submodule\n",
@@ -174,15 +185,15 @@
174185
"metadata": {},
175186
"outputs": [],
176187
"source": [
177-
"plt.figure()"
188+
"# Plot a simple x y line graph with default settings\n",
189+
"plt.plot([1,2,3,4,5], [5,4,3,2,1])"
178190
]
179191
},
180192
{
181193
"cell_type": "markdown",
182194
"metadata": {},
183195
"source": [
184-
"This creates a new figure window for a pyplot figure.\n",
185-
"Again, we'll see how this works and what it means later in the course."
196+
"You can read more about the plotting function in [matplotlib pyplot documentation](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html). We will introduce matplotlib in detail during week 7."
186197
]
187198
},
188199
{
@@ -230,7 +241,9 @@
230241
"execution_count": null,
231242
"metadata": {},
232243
"outputs": [],
233-
"source": []
244+
"source": [
245+
"help(math.sin)"
246+
]
234247
},
235248
{
236249
"cell_type": "markdown",
@@ -268,20 +281,20 @@
268281
"cell_type": "markdown",
269282
"metadata": {},
270283
"source": [
271-
"```{admonition}What does PEP 8 say about imports?\n",
272-
"According to good coding practices described in [PEP 8](https://www.python.org/dev/peps/pep-0008/#imports), we should always import modules at the top of the file. In this lesson, we are demonstrating how to import different modules along the way, but in general it would be better to import requried modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files by placing our imports the first code cell in the Jupyter Notebook.\n",
284+
"```{admonition} What does PEP 8 say about imports?\n",
285+
"According to good coding practices described in <a href=\"https://www.python.org/dev/peps/pep-0008/#imports\">PEP 8</a>, we should always import modules at the top of the file. In this lesson, we are demonstrating how to import different modules along the way, but in general it would be better to import requried modules as the very first thing. PEP 8 refers more to traditional script files, but we can apply the guideline to Jupyter Notebook files by placing our imports the first code cell in the Jupyter Notebook.\n",
273286
"```"
274287
]
275288
},
276289
{
277290
"cell_type": "markdown",
278291
"metadata": {},
279292
"source": [
280-
"## Extra: installing packages\n",
293+
"## Installing packages\n",
281294
"\n",
282-
"For those using their own computers, we recommend using the **Conda** package management system that comes with the Anaconda distribution. Using conda, you can always list package names and versions using the `conda list` command line command. Read more about installing Anaconda, and about managing Python packages using conda on the [course webpages](https://geo-python.github.io/site/course-info/installing-anacondas.html) and [Conda documentation](https://docs.conda.io/projects/conda/en/latest/).\n",
295+
"For those using their own computers, we recommend using the [Conda](https://docs.conda.io/projects/conda/en/latest/) package management system that comes with the Anaconda and Miniconda Python installers. Using Conda, you can always list package names and versions using the `conda list` command line command. Read more about installing Python and Conda tools for the purposes of this course [in here](https://geo-python-site.readthedocs.io/en/develop/course-info/installing-miniconda.html).\n",
283296
" \n",
284-
"It's also good to be aware of [pip](https://pypi.org/project/pip/) - the package installer for python. Pip and conda are often used for similar purposes, but the key difference is that pip is used for installing packages written in Python, while conda handles packages which might also contain code written in other languages. Generally, we encourage you to use conda when installing packages (and within conda, prefer to use the same channel for installations). However, sometimes you might need a package that is not available via conda, but can be installed with pip. Read more about differences and similarities of Conda an pip [in here](https://www.anaconda.com/understanding-conda-and-pip/)."
297+
"It's also good to be aware of [pip](https://pypi.org/project/pip/) - the package installer for python. Pip and conda are often used for similar purposes, but the key difference is that pip is used for installing packages written in Python, while conda handles packages which might also contain code written in other languages. Generally, we encourage you to use conda when installing packages (and within conda, prefer to use the same channel for installations). However, sometimes you might need a package that is not available via conda, but can be installed with pip. Read more about differences and similarities of conda an pip [in here](https://www.anaconda.com/understanding-conda-and-pip/)."
285298
]
286299
},
287300
{
@@ -300,7 +313,8 @@
300313
"outputs": [],
301314
"source": [
302315
"# List all available modules. Note: when running this command, you might first get several warnings related to deprecated packages etc.\n",
303-
"help(\"modules\")"
316+
"# We have commented the command out here to limit output on the course website\n",
317+
"#help(\"modules\")"
304318
]
305319
}
306320
],
@@ -320,7 +334,7 @@
320334
"name": "python",
321335
"nbconvert_exporter": "python",
322336
"pygments_lexer": "ipython3",
323-
"version": "3.7.4"
337+
"version": "3.7.3"
324338
}
325339
},
326340
"nbformat": 4,

0 commit comments

Comments
 (0)