Skip to content

Commit 0c8e361

Browse files
2024 march edition - update materials with python intro (#179)
1 parent 381a930 commit 0c8e361

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+12763
-0
lines changed
Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<p><font size=\"6\"><b>Jupyter notebook introduction </b></font></p>\n",
8+
"\n",
9+
"> *© 2024, Joris Van den Bossche and Stijn Van Hoey (<mailto:[email protected]>, <mailto:[email protected]>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
10+
"\n",
11+
"-----\n",
12+
"\n",
13+
"> This notebook is based on material of the [*Python Scientific Lecture Notes*](https://scipy-lectures.github.io/), the [*Software Carptentry: Programming with Python gapminder course*](https://swcarpentry.github.io/python-novice-gapminder) and the [*Software Carptentry: Programming with Python inflammation course*](https://swcarpentry.github.io/python-novice-inflammation/)."
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"# Introduction"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"To run Python, we are going to use Jupyter Notebooks via JupyterLab for this course. Jupyter notebooks are common in data science and visualization and serve as a convenient common-denominator experience for running Python code interactively. Jupyter notebooks let us __execute and view the results of our Python code interactively__ within the notebook.\n",
28+
"\n",
29+
"__JupyterLab__ is an application server with a web user interface from [Project Jupyter](https://jupyter.org/) that enables one to work with documents and activities such as __Jupyter notebooks__ (a notebook is a file stored on your computer as a JSON file)."
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"<div class=\"alert alert-info\">\n",
37+
"\n",
38+
"__INFO__\n",
39+
"\n",
40+
"Jupyter Notebook files have the file extension `.ipynb` to distinguish them from plain-text files.\n",
41+
" \n",
42+
"</div>"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"Let's have a check at the GUI provided by Juypyterlab. More detailed information on JupyterLab can be found in the [JupyterLab user interface documentation](https://jupyterlab.readthedocs.io/en/stable/user/interface.html) as a reference."
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"# Notebook cell types\n",
57+
"\n",
58+
"Jupyter mixes code and text in different types of blocks, called cells. We often use the term “code” to mean “the source code of software written in a language such as Python”. A “code cell” in a Notebook is a cell that contains software; a “text cell” is one that contains ordinary prose written for human beings."
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"## Code: Python"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 3,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"Hello Course\n"
78+
]
79+
}
80+
],
81+
"source": [
82+
"# Code cell, then we are using python\n",
83+
"print('Hello Course')"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"15\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"DS = 10\n",
101+
"print(DS + 5)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"Writing code is what you will do most during this course!"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"metadata": {},
114+
"source": [
115+
"## Text: Markdown"
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {},
121+
"source": [
122+
"Text cells, using Markdown syntax. With the syntax, you can make text **bold** or *italic*, amongst many other things..."
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"* list\n",
130+
"* with\n",
131+
"* items"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"Including images:\n",
139+
"\n",
140+
"![images](https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg)\n",
141+
"\n",
142+
"and links [Link to interesting resources](https://www.youtube.com/watch?v=z9Uz1icjwrM)"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"Mathematical formulas can also be incorporated (LaTeX it is...)\n",
150+
"$$\\frac{dBZV}{dt}=BZV_{in} - k_1 .BZV$$\n",
151+
"$$\\frac{dOZ}{dt}=k_2 .(OZ_{sat}-OZ) - k_1 .BZV$$"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"Or tables:\n",
159+
"\n",
160+
"course | points\n",
161+
" --- | --- \n",
162+
" Math | 8\n",
163+
" Chemistry | 4"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"Code can also be incorporated, but than just to illustrate:"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"metadata": {},
176+
"source": [
177+
"```python\n",
178+
"BOT = 12\n",
179+
"print(BOT)\n",
180+
"```"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"Get more background on the markdown syntax: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"<div class=\"alert alert-success\">\n",
195+
"\n",
196+
"**EXERCISE**:\n",
197+
"\n",
198+
"What is displayed when a Python cell in a notebook that contains several calculations is executed? \n",
199+
" \n",
200+
"- Create a new code cell\n",
201+
"- Add the following content to the cell \n",
202+
" ```\n",
203+
" 7 * 3\n",
204+
" 2 + 1\n",
205+
" ```\n",
206+
"- Execute the cell and check the output\n",
207+
" \n",
208+
"\n",
209+
"<details><summary>Hints</summary>\n",
210+
"\n",
211+
"- To add a new cell, you can use the `+`-icon button in the notebook menu\n",
212+
"- To execute a cell, you can use the play-icon button in the notebook menu of use SHIFT + ENTER\n",
213+
"\n",
214+
"</details> \n",
215+
" \n",
216+
"</div>"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 5,
222+
"metadata": {
223+
"tags": [
224+
"nbtutor-solution"
225+
]
226+
},
227+
"outputs": [
228+
{
229+
"data": {
230+
"text/plain": [
231+
"3"
232+
]
233+
},
234+
"execution_count": 5,
235+
"metadata": {},
236+
"output_type": "execute_result"
237+
}
238+
],
239+
"source": [
240+
"# Jupyter returns the output of the last calculation.\n",
241+
"7 * 3\n",
242+
"2 + 1"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"<div class=\"alert alert-success\">\n",
250+
"\n",
251+
"**EXERCISE**:\n",
252+
"\n",
253+
"What happens if you write some Python in a code cell and then you switch it to a Markdown cell? \n",
254+
" \n",
255+
"- Create a new code cell\n",
256+
"- Add the following content to the cell \n",
257+
" ```\n",
258+
" x = 6 * 7 + 12\n",
259+
" print(x)\n",
260+
" ```\n",
261+
"- Execute the cell and check the output \n",
262+
"- Change the cell to a 'Markdown' cell type \n",
263+
" \n",
264+
"<details><summary>Hints</summary>\n",
265+
"\n",
266+
"- To switch a cell type, us the dropdown menu with cell types in the notebook menu\n",
267+
"\n",
268+
"</details> \n",
269+
" \n",
270+
"</div>"
271+
]
272+
},
273+
{
274+
"cell_type": "markdown",
275+
"metadata": {
276+
"tags": [
277+
"nbtutor-solution"
278+
]
279+
},
280+
"source": [
281+
"x = 6 * 7 + 12\n",
282+
"print(x)"
283+
]
284+
},
285+
{
286+
"cell_type": "markdown",
287+
"metadata": {},
288+
"source": [
289+
"## Execution: SHIFT + ENTER\n",
290+
"\n",
291+
"To run a cell: <strike>push the start triangle in the menu or</strike> type **SHIFT + ENTER**\n",
292+
"\n",
293+
"![](../../img/shiftenter.jpg)"
294+
]
295+
},
296+
{
297+
"cell_type": "markdown",
298+
"metadata": {},
299+
"source": [
300+
"# When in trouble"
301+
]
302+
},
303+
{
304+
"cell_type": "markdown",
305+
"metadata": {},
306+
"source": [
307+
"<div class=\"alert alert-danger\">\n",
308+
"\n",
309+
"__NOTE__: \n",
310+
" \n",
311+
"When you're stuck, or your notebook crashes: \n",
312+
" \n",
313+
"- first try <code>Kernel</code> > <code>Interrupt</code> -> your cell should stop running\n",
314+
"- if no succes -> <code>Kernel</code> > <code>Restart</code> -> restart your notebook\n",
315+
" \n",
316+
"</div>"
317+
]
318+
},
319+
{
320+
"cell_type": "markdown",
321+
"metadata": {},
322+
"source": [
323+
"<div class=\"alert alert-info\">\n",
324+
"\n",
325+
"__Kernels?__\n",
326+
"\n",
327+
"The JupyterLab docs define kernels as “separate processes started by the server that runs your code in different programming languages and environments.” When we open a Jupyter Notebook, that starts a kernel - a process - that is going to run the code. In this lesson, we’ll be using the Jupyter ipython kernel which lets us run Python 3 code interactively.\n",
328+
"\n",
329+
"Using other Jupyter kernels for other programming languages would let us write and execute code in other programming languages in the same JupyterLab interface, like R, Java, Julia, Ruby, JavaScript, Fortran, etc.\n",
330+
"</div>"
331+
]
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": null,
336+
"metadata": {},
337+
"outputs": [],
338+
"source": []
339+
}
340+
],
341+
"metadata": {
342+
"jupytext": {
343+
"formats": "ipynb,md:myst"
344+
},
345+
"kernelspec": {
346+
"display_name": "Python 3 (ipykernel)",
347+
"language": "python",
348+
"name": "python3"
349+
},
350+
"language_info": {
351+
"codemirror_mode": {
352+
"name": "ipython",
353+
"version": 3
354+
},
355+
"file_extension": ".py",
356+
"mimetype": "text/x-python",
357+
"name": "python",
358+
"nbconvert_exporter": "python",
359+
"pygments_lexer": "ipython3",
360+
"version": "3.8.16"
361+
},
362+
"nav_menu": {},
363+
"toc": {
364+
"navigate_menu": true,
365+
"number_sections": true,
366+
"sideBar": true,
367+
"threshold": 6,
368+
"toc_cell": false,
369+
"toc_section_display": "block",
370+
"toc_window_display": true
371+
},
372+
"toc_position": {
373+
"height": "860px",
374+
"left": "0px",
375+
"right": "1568px",
376+
"top": "106px",
377+
"width": "301px"
378+
},
379+
"widgets": {
380+
"application/vnd.jupyter.widget-state+json": {
381+
"state": {},
382+
"version_major": 2,
383+
"version_minor": 0
384+
}
385+
}
386+
},
387+
"nbformat": 4,
388+
"nbformat_minor": 4
389+
}

0 commit comments

Comments
 (0)