Skip to content

Commit e1f796d

Browse files
committed
Preparing first lab session of 2K23
1 parent 81afeee commit e1f796d

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

notebooks/Python_in_a_Nutshell.ipynb

+49-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"cells": [
33
{
4+
"attachments": {},
45
"cell_type": "markdown",
56
"metadata": {},
67
"source": [
7-
"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by/4.0/88x31.png\" /></a><br /><span xmlns:dct=\"http://purl.org/dc/terms/\" property=\"dct:title\"><b>Python in a Nutshell</b></span> by <a xmlns:cc=\"http://creativecommons.org/ns#\" href=\"http://mate.unipv.it/gualandi\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Stefano Gualandi</a> is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\">Creative Commons Attribution 4.0 International License</a>.<br />Based on a work at <a xmlns:dct=\"http://purl.org/dc/terms/\" href=\"https://github.com/mathcoding/opt4ds\" rel=\"dct:source\">https://github.com/mathcoding/opt4ds</a>."
8+
"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by/4.0/88x31.png\" /></a><br /><span xmlns:dct=\"http://purl.org/dc/terms/\" property=\"dct:title\"><b>Python in a Nutshell</b></span> by <a xmlns:cc=\"http://creativecommons.org/ns#\" href=\"http://mate.unipv.it/gualandi\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Stefano Gualandi</a> is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\">Creative Commons Attribution 4.0 International License</a>.<br />Based on the lectures available at <a xmlns:dct=\"http://purl.org/dc/terms/\" href=\"https://github.com/mathcoding/opt4ds\" rel=\"dct:source\">https://github.com/mathcoding/opt4ds</a>."
89
]
910
},
1011
{
@@ -107,19 +108,20 @@
107108
]
108109
},
109110
{
111+
"attachments": {},
110112
"cell_type": "markdown",
111113
"metadata": {
112114
"colab_type": "text",
113115
"id": "gSXNSKvAPbsA"
114116
},
115117
"source": [
116-
"By default, the notebook interpreter executes a single block line of code, and it tries to print a value resulting from the evaluation of an expression. Note that `a = 1` is an assignment operation, which does not produce a visible result, and hence the interpreter do not print anything. While `type(a)` is a function invocation that returns the type of the variable `a`, thus printing its value on the screen.\n",
118+
"By default, the notebook interpreter executes a single block of code, and it tries to print a value resulting from the evaluation of an expression. Note that `a = 1` is an assignment operation, which does not produce a visible result, and hence the interpreter do not print anything. While `type(a)` is a function invocation that returns the type of the variable `a`, thus printing its value on the screen.\n",
117119
"\n",
118120
"> Please, take the good habit of reading the documentation. \n",
119121
"\n",
120122
"The function `type` is fully described online a this link: [type](https://docs.python.org/3/library/functions.html#type)\n",
121123
"\n",
122-
"You can also consult a minimal documentation using the function `help`:"
124+
"You can read a minimal documentation using the function `help`:"
123125
]
124126
},
125127
{
@@ -244,6 +246,7 @@
244246
]
245247
},
246248
{
249+
"attachments": {},
247250
"cell_type": "markdown",
248251
"metadata": {
249252
"colab_type": "text",
@@ -252,7 +255,7 @@
252255
"source": [
253256
"The `^` operator implements the bit-wise xor logical operator.\n",
254257
"\n",
255-
"The operator of power is a double star:"
258+
"The power (exponent) operator is the double star `**`:"
256259
]
257260
},
258261
{
@@ -357,14 +360,15 @@
357360
]
358361
},
359362
{
363+
"attachments": {},
360364
"cell_type": "markdown",
361365
"metadata": {
362366
"colab_type": "text",
363367
"id": "IA9eXA0DFC_B"
364368
},
365369
"source": [
366370
"### 1.1.3 Strings\n",
367-
"Another very useful data type is **string**, which is nothing else that an ordered sequence of characters.\n",
371+
"Another very useful data type is **string**, which is an ordered sequence of characters.\n",
368372
"\n",
369373
"Example:"
370374
]
@@ -463,16 +467,18 @@
463467
]
464468
},
465469
{
470+
"attachments": {},
466471
"cell_type": "markdown",
467472
"metadata": {
468473
"colab_type": "text",
469474
"id": "2xSSEsN3Tslz"
470475
},
471476
"source": [
472-
"First we have converted a string into lower case with `lower()` function, and then we have split the string using as delimiter the string `-`."
477+
"First, we have converted a string into lower case with `lower()` function, and then we have split the string using as delimiter the string `-`."
473478
]
474479
},
475480
{
481+
"attachments": {},
476482
"cell_type": "markdown",
477483
"metadata": {
478484
"colab_type": "text",
@@ -486,7 +492,12 @@
486492
"2. [`list`](https://docs.python.org/3/library/functions.html#func-list)\n",
487493
"3. [`dictionary`](https://docs.python.org/3/library/functions.html#func-dict)\n",
488494
"\n",
489-
"We best see these three data types into action."
495+
"We best see these three data types into action.\n",
496+
"\n",
497+
"Other useful containers are we will use during the lectures are\n",
498+
"\n",
499+
"4. [`set`](https://docs.python.org/3/library/functions.html#func-set)\n",
500+
"5. [`numpy.array`](https://numpy.org/doc/stable/reference/generated/numpy.array.html)"
490501
]
491502
},
492503
{
@@ -607,13 +618,14 @@
607618
]
608619
},
609620
{
621+
"attachments": {},
610622
"cell_type": "markdown",
611623
"metadata": {
612624
"colab_type": "text",
613625
"id": "bvg4AMXJWfkr"
614626
},
615627
"source": [
616-
"Note however that tuple are **reading-only** data types, that is we cannot modify an element of a tuple:"
628+
"Note however that tuple are **reading-only** data types, that is, we cannot modify an element of a tuple:"
617629
]
618630
},
619631
{
@@ -634,13 +646,14 @@
634646
]
635647
},
636648
{
649+
"attachments": {},
637650
"cell_type": "markdown",
638651
"metadata": {
639652
"colab_type": "text",
640653
"id": "ihzovuXLbfM0"
641654
},
642655
"source": [
643-
"If you need to store a **mutable** ordered sequence of data, you can use a `list` instead of a `tuple`, as we show next."
656+
"If you need to store a **mutable** ordered sequence of data, you can use a `list` instead of a `tuple`, as we show in the next subsection."
644657
]
645658
},
646659
{
@@ -1180,13 +1193,14 @@
11801193
]
11811194
},
11821195
{
1196+
"attachments": {},
11831197
"cell_type": "markdown",
11841198
"metadata": {
11851199
"colab_type": "text",
11861200
"id": "CEUaf2zNHiKt"
11871201
},
11881202
"source": [
1189-
"In this example, we have used the builtin function [zip](https://docs.python.org/3/library/functions.html#zip). If it is unclear how the function **zip** works, try to understand it by inspection running the following line"
1203+
"In this example, we have used the builtin function [zip](https://docs.python.org/3/library/functions.html#zip). If it is unclear how the function **zip** works, try to understand it by running the following two lines"
11901204
]
11911205
},
11921206
{
@@ -1436,7 +1450,7 @@
14361450
"outputs": [],
14371451
"source": [
14381452
"for i, e in enumerate(Ls):\n",
1439-
" print(\"index = {}, element = {}, indexed = {}\".format(i,e, Ls[i-1]))"
1453+
" print(\"index = {}, element = {}, indexed = {}\".format(i, e, Ls[i-1]))"
14401454
]
14411455
},
14421456
{
@@ -1568,13 +1582,14 @@
15681582
]
15691583
},
15701584
{
1585+
"attachments": {},
15711586
"cell_type": "markdown",
15721587
"metadata": {
15731588
"colab_type": "text",
15741589
"id": "jwdREYEfYG-v"
15751590
},
15761591
"source": [
1577-
"There are several different type of exceptions in Python, and you should familiarize with those types, in order to identify the errors in yours programs. A complete list of exceptions is available on the official documentation: [Builtin Exceptions](https://docs.python.org/3/library/exceptions.html#bltin-exceptions)."
1592+
"There are several different types of exceptions in Python, and you should familiarize with those types, in order to identify the errors in yours programs. A complete list of exceptions is available on the official documentation: [Builtin Exceptions](https://docs.python.org/3/library/exceptions.html#bltin-exceptions)."
15781593
]
15791594
},
15801595
{
@@ -1977,13 +1992,14 @@
19771992
]
19781993
},
19791994
{
1995+
"attachments": {},
19801996
"cell_type": "markdown",
19811997
"metadata": {
19821998
"colab_type": "text",
19831999
"id": "bsZMwh1PbnaX"
19842000
},
19852001
"source": [
1986-
"Notice that there is not `return` keyword in the procedure `Print(a, b, c)`. However, in this case, it is like there was a `return None` statement at the end of the procedure definition. For this reason, procedure are also called *void functions*."
2002+
"Notice that there is not `return` keyword in the procedure `Print(a, b, c)`. However, in this case, it is like there was a `return None` statement at the end of the procedure definition. For this reason, procedures are also called *void functions*."
19872003
]
19882004
},
19892005
{
@@ -2032,14 +2048,15 @@
20322048
]
20332049
},
20342050
{
2051+
"attachments": {},
20352052
"cell_type": "markdown",
20362053
"metadata": {
20372054
"colab_type": "text",
20382055
"id": "F5VNwahzK-Go"
20392056
},
20402057
"source": [
20412058
"### 1.4.3 Lambda Functions\n",
2042-
"In some case, we can use anonymous functions, called `lambda functions`. They are mostly used when we need to pass a function as a actual parameter of a formal parameter (see next section).\n",
2059+
"In some cases, it is useful to use anonymous functions, called `lambda functions`. They are mostly used when we need to pass a function as a actual parameter of a formal parameter (see next section).\n",
20432060
"\n",
20442061
"We can use lambda functions also for one-liner definition of functions.\n"
20452062
]
@@ -2128,13 +2145,14 @@
21282145
]
21292146
},
21302147
{
2148+
"attachments": {},
21312149
"cell_type": "markdown",
21322150
"metadata": {
21332151
"colab_type": "text",
21342152
"id": "KFXmWFNrimVE"
21352153
},
21362154
"source": [
2137-
"In case of doubts, you can always use the online documentation, via the `help()` function."
2155+
"Remember: you can always use the online documentation, via the `help()` function."
21382156
]
21392157
},
21402158
{
@@ -2164,14 +2182,15 @@
21642182
]
21652183
},
21662184
{
2185+
"attachments": {},
21672186
"cell_type": "markdown",
21682187
"metadata": {
21692188
"colab_type": "text",
21702189
"id": "a0D3iGziKO3P"
21712190
},
21722191
"source": [
21732192
"## 1.5 Classes and Objects\n",
2174-
"In Sections 1.1, we have seen old plain data types (numbers and strings), and in Section 1.2, the structured data types (tuple, list, dictionaries).\n",
2193+
"In Sections 1.1, we have reviewed old plain data types (numbers and strings), and in Section 1.2, the structured data types (tuple, list, dictionaries).\n",
21752194
"\n",
21762195
"In this section, we show how we can define new data types, which can be composed of other data. The new data types are defined using the syntax for defining new `classes`. You can think of `class` as a synonym of `type`."
21772196
]
@@ -2206,6 +2225,7 @@
22062225
]
22072226
},
22082227
{
2228+
"attachments": {},
22092229
"cell_type": "markdown",
22102230
"metadata": {
22112231
"colab_type": "text",
@@ -2220,9 +2240,9 @@
22202240
"$$\n",
22212241
"\n",
22222242
"\n",
2223-
"**QUESTION:** How can we define a class for the type **point in $\\mathbb{R}^3$**?\n",
2243+
"**EXERCISE 4:** How can we define a class for the type **point in $\\mathbb{R}^3$**?\n",
22242244
"\n",
2225-
"**QUESTION:** How can we define a class for the type **discrete measure**?\n",
2245+
"**EXERCISE 5:** How can we define a class for the type **discrete measure**?\n",
22262246
"\n"
22272247
]
22282248
},
@@ -2246,18 +2266,19 @@
22462266
},
22472267
"outputs": [],
22482268
"source": [
2249-
"# DA FARE A LEZIONE COME ESERCIZIO\n",
2269+
"# Lab session exercise\n",
22502270
"# ..."
22512271
]
22522272
},
22532273
{
2274+
"attachments": {},
22542275
"cell_type": "markdown",
22552276
"metadata": {
22562277
"colab_type": "text",
22572278
"id": "fDKFuL-qP4aj"
22582279
},
22592280
"source": [
2260-
"So far, we have just defined a new data type, that is, a new class. Until here, it is like we were declaring a new function. However, as with function we are interested in **applying** a function to given values to get the result value, with a class we are interested in **applying** the class to data to get an **instance** of a class, that is an **object** of the type defined by the corresponding **class**.\n",
2281+
"So far, we have just defined a new data type, that is, a new class. Until here, it is like we were declaring a new function. However, as with functions we are interested in **applying** a function to given input values to get the resulting output value, with a classes we are interested in **applying** the class to input data to get as output an **instance** of that class, that is, an **object** of the type defined by the given **class**.\n",
22612282
"\n",
22622283
"To get three objects of type `Point3D`, we can run the following code."
22632284
]
@@ -2353,7 +2374,7 @@
23532374
},
23542375
"outputs": [],
23552376
"source": [
2356-
"# DA FARE A LEZIONE COME ESERCIZIO\n",
2377+
"# Lab session exercise\n",
23572378
"# ..."
23582379
]
23592380
},
@@ -2423,6 +2444,7 @@
24232444
]
24242445
},
24252446
{
2447+
"attachments": {},
24262448
"cell_type": "markdown",
24272449
"metadata": {
24282450
"colab_type": "text",
@@ -2431,7 +2453,7 @@
24312453
"source": [
24322454
"**QUESTION:** How can we compose all the code that we have just written to define a class of type **DiscreteMeasure**?\n",
24332455
"\n",
2434-
"A possibility is the following."
2456+
"A possible solution is the following."
24352457
]
24362458
},
24372459
{
@@ -2444,7 +2466,7 @@
24442466
},
24452467
"outputs": [],
24462468
"source": [
2447-
"# DA FARE A LEZIONE COME ESERCIZIO\n",
2469+
"# Lab session exercise\n",
24482470
"# ..."
24492471
]
24502472
},
@@ -2479,16 +2501,17 @@
24792501
]
24802502
},
24812503
{
2504+
"attachments": {},
24822505
"cell_type": "markdown",
24832506
"metadata": {
24842507
"colab_type": "text",
24852508
"id": "mieLMDucaV3k"
24862509
},
24872510
"source": [
24882511
"### 1.5.3 Plotting\n",
2489-
"Finally, we want to plot our discrete distribution on $\\mathbb{R}^3$.\n",
2512+
"Finally, we want to plot our discrete distribution supported in $\\mathbb{R}^3$.\n",
24902513
"\n",
2491-
"For plotting, we will use two libraries in this course: [Matplotlib](https://matplotlib.org/) and [Altair](https://altair-viz.github.io/). The first library is very similar to the Matlab plot function, while the second permit to easily create interactive plots. In this notebook, we use only Matplotlib.\n",
2514+
"For plotting, we will use two libraries in this course: [Matplotlib](https://matplotlib.org/), [Plotly](https://plotly.com/), or [Altair](https://altair-viz.github.io/). The first library is very similar to the Matlab plot function, while the second permit to easily create interactive plots. In this notebook, we use only Matplotlib.\n",
24922515
"\n",
24932516
"Since you should already have experience with the plotting functions of Matlab, we show directly how to represent our discrete distribution using a [3D scatterplot](https://matplotlib.org/3.2.0/gallery/mplot3d/scatter3d.html).\n"
24942517
]
@@ -2507,7 +2530,7 @@
25072530
},
25082531
"outputs": [],
25092532
"source": [
2510-
"# DA FARE A LEZIONE COME ESERCIZIO\n",
2533+
"# Lab session exercise\n",
25112534
"# ..."
25122535
]
25132536
}

0 commit comments

Comments
 (0)