|
1 | 1 | {
|
2 | 2 | "cells": [
|
3 | 3 | {
|
| 4 | + "attachments": {}, |
4 | 5 | "cell_type": "markdown",
|
5 | 6 | "metadata": {},
|
6 | 7 | "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>." |
8 | 9 | ]
|
9 | 10 | },
|
10 | 11 | {
|
|
107 | 108 | ]
|
108 | 109 | },
|
109 | 110 | {
|
| 111 | + "attachments": {}, |
110 | 112 | "cell_type": "markdown",
|
111 | 113 | "metadata": {
|
112 | 114 | "colab_type": "text",
|
113 | 115 | "id": "gSXNSKvAPbsA"
|
114 | 116 | },
|
115 | 117 | "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", |
117 | 119 | "\n",
|
118 | 120 | "> Please, take the good habit of reading the documentation. \n",
|
119 | 121 | "\n",
|
120 | 122 | "The function `type` is fully described online a this link: [type](https://docs.python.org/3/library/functions.html#type)\n",
|
121 | 123 | "\n",
|
122 |
| - "You can also consult a minimal documentation using the function `help`:" |
| 124 | + "You can read a minimal documentation using the function `help`:" |
123 | 125 | ]
|
124 | 126 | },
|
125 | 127 | {
|
|
244 | 246 | ]
|
245 | 247 | },
|
246 | 248 | {
|
| 249 | + "attachments": {}, |
247 | 250 | "cell_type": "markdown",
|
248 | 251 | "metadata": {
|
249 | 252 | "colab_type": "text",
|
|
252 | 255 | "source": [
|
253 | 256 | "The `^` operator implements the bit-wise xor logical operator.\n",
|
254 | 257 | "\n",
|
255 |
| - "The operator of power is a double star:" |
| 258 | + "The power (exponent) operator is the double star `**`:" |
256 | 259 | ]
|
257 | 260 | },
|
258 | 261 | {
|
|
357 | 360 | ]
|
358 | 361 | },
|
359 | 362 | {
|
| 363 | + "attachments": {}, |
360 | 364 | "cell_type": "markdown",
|
361 | 365 | "metadata": {
|
362 | 366 | "colab_type": "text",
|
363 | 367 | "id": "IA9eXA0DFC_B"
|
364 | 368 | },
|
365 | 369 | "source": [
|
366 | 370 | "### 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", |
368 | 372 | "\n",
|
369 | 373 | "Example:"
|
370 | 374 | ]
|
|
463 | 467 | ]
|
464 | 468 | },
|
465 | 469 | {
|
| 470 | + "attachments": {}, |
466 | 471 | "cell_type": "markdown",
|
467 | 472 | "metadata": {
|
468 | 473 | "colab_type": "text",
|
469 | 474 | "id": "2xSSEsN3Tslz"
|
470 | 475 | },
|
471 | 476 | "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 `-`." |
473 | 478 | ]
|
474 | 479 | },
|
475 | 480 | {
|
| 481 | + "attachments": {}, |
476 | 482 | "cell_type": "markdown",
|
477 | 483 | "metadata": {
|
478 | 484 | "colab_type": "text",
|
|
486 | 492 | "2. [`list`](https://docs.python.org/3/library/functions.html#func-list)\n",
|
487 | 493 | "3. [`dictionary`](https://docs.python.org/3/library/functions.html#func-dict)\n",
|
488 | 494 | "\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)" |
490 | 501 | ]
|
491 | 502 | },
|
492 | 503 | {
|
|
607 | 618 | ]
|
608 | 619 | },
|
609 | 620 | {
|
| 621 | + "attachments": {}, |
610 | 622 | "cell_type": "markdown",
|
611 | 623 | "metadata": {
|
612 | 624 | "colab_type": "text",
|
613 | 625 | "id": "bvg4AMXJWfkr"
|
614 | 626 | },
|
615 | 627 | "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:" |
617 | 629 | ]
|
618 | 630 | },
|
619 | 631 | {
|
|
634 | 646 | ]
|
635 | 647 | },
|
636 | 648 | {
|
| 649 | + "attachments": {}, |
637 | 650 | "cell_type": "markdown",
|
638 | 651 | "metadata": {
|
639 | 652 | "colab_type": "text",
|
640 | 653 | "id": "ihzovuXLbfM0"
|
641 | 654 | },
|
642 | 655 | "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." |
644 | 657 | ]
|
645 | 658 | },
|
646 | 659 | {
|
|
1180 | 1193 | ]
|
1181 | 1194 | },
|
1182 | 1195 | {
|
| 1196 | + "attachments": {}, |
1183 | 1197 | "cell_type": "markdown",
|
1184 | 1198 | "metadata": {
|
1185 | 1199 | "colab_type": "text",
|
1186 | 1200 | "id": "CEUaf2zNHiKt"
|
1187 | 1201 | },
|
1188 | 1202 | "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" |
1190 | 1204 | ]
|
1191 | 1205 | },
|
1192 | 1206 | {
|
|
1436 | 1450 | "outputs": [],
|
1437 | 1451 | "source": [
|
1438 | 1452 | "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]))" |
1440 | 1454 | ]
|
1441 | 1455 | },
|
1442 | 1456 | {
|
|
1568 | 1582 | ]
|
1569 | 1583 | },
|
1570 | 1584 | {
|
| 1585 | + "attachments": {}, |
1571 | 1586 | "cell_type": "markdown",
|
1572 | 1587 | "metadata": {
|
1573 | 1588 | "colab_type": "text",
|
1574 | 1589 | "id": "jwdREYEfYG-v"
|
1575 | 1590 | },
|
1576 | 1591 | "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)." |
1578 | 1593 | ]
|
1579 | 1594 | },
|
1580 | 1595 | {
|
|
1977 | 1992 | ]
|
1978 | 1993 | },
|
1979 | 1994 | {
|
| 1995 | + "attachments": {}, |
1980 | 1996 | "cell_type": "markdown",
|
1981 | 1997 | "metadata": {
|
1982 | 1998 | "colab_type": "text",
|
1983 | 1999 | "id": "bsZMwh1PbnaX"
|
1984 | 2000 | },
|
1985 | 2001 | "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*." |
1987 | 2003 | ]
|
1988 | 2004 | },
|
1989 | 2005 | {
|
|
2032 | 2048 | ]
|
2033 | 2049 | },
|
2034 | 2050 | {
|
| 2051 | + "attachments": {}, |
2035 | 2052 | "cell_type": "markdown",
|
2036 | 2053 | "metadata": {
|
2037 | 2054 | "colab_type": "text",
|
2038 | 2055 | "id": "F5VNwahzK-Go"
|
2039 | 2056 | },
|
2040 | 2057 | "source": [
|
2041 | 2058 | "### 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", |
2043 | 2060 | "\n",
|
2044 | 2061 | "We can use lambda functions also for one-liner definition of functions.\n"
|
2045 | 2062 | ]
|
|
2128 | 2145 | ]
|
2129 | 2146 | },
|
2130 | 2147 | {
|
| 2148 | + "attachments": {}, |
2131 | 2149 | "cell_type": "markdown",
|
2132 | 2150 | "metadata": {
|
2133 | 2151 | "colab_type": "text",
|
2134 | 2152 | "id": "KFXmWFNrimVE"
|
2135 | 2153 | },
|
2136 | 2154 | "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." |
2138 | 2156 | ]
|
2139 | 2157 | },
|
2140 | 2158 | {
|
|
2164 | 2182 | ]
|
2165 | 2183 | },
|
2166 | 2184 | {
|
| 2185 | + "attachments": {}, |
2167 | 2186 | "cell_type": "markdown",
|
2168 | 2187 | "metadata": {
|
2169 | 2188 | "colab_type": "text",
|
2170 | 2189 | "id": "a0D3iGziKO3P"
|
2171 | 2190 | },
|
2172 | 2191 | "source": [
|
2173 | 2192 | "## 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", |
2175 | 2194 | "\n",
|
2176 | 2195 | "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`."
|
2177 | 2196 | ]
|
|
2206 | 2225 | ]
|
2207 | 2226 | },
|
2208 | 2227 | {
|
| 2228 | + "attachments": {}, |
2209 | 2229 | "cell_type": "markdown",
|
2210 | 2230 | "metadata": {
|
2211 | 2231 | "colab_type": "text",
|
|
2220 | 2240 | "$$\n",
|
2221 | 2241 | "\n",
|
2222 | 2242 | "\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", |
2224 | 2244 | "\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", |
2226 | 2246 | "\n"
|
2227 | 2247 | ]
|
2228 | 2248 | },
|
|
2246 | 2266 | },
|
2247 | 2267 | "outputs": [],
|
2248 | 2268 | "source": [
|
2249 |
| - "# DA FARE A LEZIONE COME ESERCIZIO\n", |
| 2269 | + "# Lab session exercise\n", |
2250 | 2270 | "# ..."
|
2251 | 2271 | ]
|
2252 | 2272 | },
|
2253 | 2273 | {
|
| 2274 | + "attachments": {}, |
2254 | 2275 | "cell_type": "markdown",
|
2255 | 2276 | "metadata": {
|
2256 | 2277 | "colab_type": "text",
|
2257 | 2278 | "id": "fDKFuL-qP4aj"
|
2258 | 2279 | },
|
2259 | 2280 | "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", |
2261 | 2282 | "\n",
|
2262 | 2283 | "To get three objects of type `Point3D`, we can run the following code."
|
2263 | 2284 | ]
|
|
2353 | 2374 | },
|
2354 | 2375 | "outputs": [],
|
2355 | 2376 | "source": [
|
2356 |
| - "# DA FARE A LEZIONE COME ESERCIZIO\n", |
| 2377 | + "# Lab session exercise\n", |
2357 | 2378 | "# ..."
|
2358 | 2379 | ]
|
2359 | 2380 | },
|
|
2423 | 2444 | ]
|
2424 | 2445 | },
|
2425 | 2446 | {
|
| 2447 | + "attachments": {}, |
2426 | 2448 | "cell_type": "markdown",
|
2427 | 2449 | "metadata": {
|
2428 | 2450 | "colab_type": "text",
|
|
2431 | 2453 | "source": [
|
2432 | 2454 | "**QUESTION:** How can we compose all the code that we have just written to define a class of type **DiscreteMeasure**?\n",
|
2433 | 2455 | "\n",
|
2434 |
| - "A possibility is the following." |
| 2456 | + "A possible solution is the following." |
2435 | 2457 | ]
|
2436 | 2458 | },
|
2437 | 2459 | {
|
|
2444 | 2466 | },
|
2445 | 2467 | "outputs": [],
|
2446 | 2468 | "source": [
|
2447 |
| - "# DA FARE A LEZIONE COME ESERCIZIO\n", |
| 2469 | + "# Lab session exercise\n", |
2448 | 2470 | "# ..."
|
2449 | 2471 | ]
|
2450 | 2472 | },
|
|
2479 | 2501 | ]
|
2480 | 2502 | },
|
2481 | 2503 | {
|
| 2504 | + "attachments": {}, |
2482 | 2505 | "cell_type": "markdown",
|
2483 | 2506 | "metadata": {
|
2484 | 2507 | "colab_type": "text",
|
2485 | 2508 | "id": "mieLMDucaV3k"
|
2486 | 2509 | },
|
2487 | 2510 | "source": [
|
2488 | 2511 | "### 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", |
2490 | 2513 | "\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", |
2492 | 2515 | "\n",
|
2493 | 2516 | "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"
|
2494 | 2517 | ]
|
|
2507 | 2530 | },
|
2508 | 2531 | "outputs": [],
|
2509 | 2532 | "source": [
|
2510 |
| - "# DA FARE A LEZIONE COME ESERCIZIO\n", |
| 2533 | + "# Lab session exercise\n", |
2511 | 2534 | "# ..."
|
2512 | 2535 | ]
|
2513 | 2536 | }
|
|
0 commit comments