|
19 | 19 | "cell_type": "markdown",
|
20 | 20 | "metadata": {},
|
21 | 21 | "source": [
|
22 |
| - "It is time to enrich your baggage of containers with a new useful one: the `dict`.\n", |
| 22 | + "It is time to enrich your baggage of Python containers with a new useful one: the `dict`.\n", |
23 | 23 | "\n",
|
24 |
| - "Each item in a `dict` is represented by a pair of a key and a value: e.g., mapping a [chemical symbol](https://en.wikipedia.org/wiki/Symbol_(chemistry)) to the chemical element name: e.g., `H` to `Hydrogen`." |
| 24 | + "Each item in a `dict` is represented by a pair of a key and the corresponding value. \n", |
| 25 | + "\n", |
| 26 | + "For example, a `dict` can be used to map a [chemical symbol](https://en.wikipedia.org/wiki/Symbol_(chemistry)) to the corresponding element name. Thus, a possible pair would be `H` as symbol and `Hydrogen` as element name." |
25 | 27 | ]
|
26 | 28 | },
|
27 | 29 | {
|
|
30 | 32 | "source": [
|
31 | 33 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
32 | 34 | "\n",
|
33 |
| - "A `dict` maps a set of indices, called **keys**, to a set of values." |
| 35 | + "A `dict` maps a set of indices, called **keys**, to a set of **values**." |
34 | 36 | ]
|
35 | 37 | },
|
36 | 38 | {
|
|
44 | 46 | "cell_type": "markdown",
|
45 | 47 | "metadata": {},
|
46 | 48 | "source": [
|
47 |
| - "To create a `dict`, you can call the `dict()` constructor. Then, you can start to add items to the `dict` by using square brackets as in the code below: " |
| 49 | + "Similarly to [the creation approach #2 for lists](002_Lists_of_Variables.ipynb#Creation-of-a-List:-Approach-#2), you may create a empty dictionary by calling its constructor: `dict()`. Items are then added by using square brackets and assignments like in the code below: " |
48 | 50 | ]
|
49 | 51 | },
|
50 | 52 | {
|
|
74 | 76 | "- For each item, there are two parts separated by an `-`: the key on the left (e.g., `\"Li\"`) and the value on the right (e.g., `\"Lithium\"`).\n"
|
75 | 77 | ]
|
76 | 78 | },
|
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "An alternative way to create a dictionary with the same content as above is to use something similar to what was printed:" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "chem_dict = {\"H\": \"Hydrogen\", \"He\": \"Helium\", \"Li\": \"Lithium\", \"Be\": \"Beryllium\", \"B\": \"Boron\"}\n", |
| 93 | + "\n", |
| 94 | + "print(chem_dict)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "The above method creates a `dict` and its items with a single statement. It is similar to [approach #1 for lists](002_Lists_of_Variables.ipynb#Creation-of-a-List:-Approach-#1)." |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "The follow example uses the same code, but splits the dictionary creation statement into a few rows for readability:" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "chem_dict = {\n", |
| 118 | + " \"H\": \"Hydrogen\",\n", |
| 119 | + " \"He\": \"Helium\",\n", |
| 120 | + " \"Li\": \"Lithium\",\n", |
| 121 | + " \"Be\": \"Beryllium\",\n", |
| 122 | + " \"B\": \"Boron\"\n", |
| 123 | + "}\n", |
| 124 | + "\n", |
| 125 | + "print(chem_dict)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n", |
| 133 | + "\n", |
| 134 | + "You may have noticed that the `str` variables in the above `chem_dict` are printed within `'` rather than `\"`. This is an alternative and valid way to define strings in Python. However, mixing `'` and `\"` in the same string is a Python error. For consistency, we always use `\"`." |
| 135 | + ] |
| 136 | + }, |
77 | 137 | {
|
78 | 138 | "cell_type": "markdown",
|
79 | 139 | "metadata": {},
|
|
85 | 145 | "cell_type": "markdown",
|
86 | 146 | "metadata": {},
|
87 | 147 | "source": [
|
88 |
| - "When you print the content of a dictionary, you may have the items presented in a order that differs from the one that you used to populate the `dict`. This is **not** an error, but a declared property of a `dict`." |
| 148 | + "When you print the content of a dictionary, you may have the items presented in a order that differs from the one that you used to populate the `dict`. This is **not** an error, but a specific property of a `dict`." |
89 | 149 | ]
|
90 | 150 | },
|
91 | 151 | {
|
|
94 | 154 | "source": [
|
95 | 155 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
96 | 156 | "\n",
|
97 |
| - "A `dict` is an **unordered** container. The order of items is not preserved." |
| 157 | + "A `dict` is an **unordered** container. The order of items insertion is not preserved." |
98 | 158 | ]
|
99 | 159 | },
|
100 | 160 | {
|
|
180 | 240 | "cell_type": "markdown",
|
181 | 241 | "metadata": {},
|
182 | 242 | "source": [
|
183 |
| - "A dictionary differs from a list for several aspects:\n", |
| 243 | + "A dictionary differs from a [list](002_Lists_of_Variables.ipynb) for several aspects:\n", |
184 | 244 | "\n",
|
185 | 245 | "| Topic | List | Dictionary |\n",
|
186 | 246 | "| :-----| :---- | :--------- |\n",
|
|
196 | 256 | "source": [
|
197 | 257 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
|
198 | 258 | "\n",
|
199 |
| - "In the table above, the '(almost) any type' for `dict` indexing is because the type must be [hashable](https://docs.python.org/3.6/glossary.html). That is, it must be possible to calculate a [hash value](https://en.wikipedia.org/wiki/Hash_function) from the key which never changes during its lifetime, and can be compared to other keys." |
| 259 | + "In the table above, the *\"(almost) any type\"* for `dict` indexing is because the type must be [hashable](https://docs.python.org/3.6/glossary.html). That is, it must be possible to calculate a [hash value](https://en.wikipedia.org/wiki/Hash_function) from the key which never changes during its lifetime, and can be compared to other keys." |
200 | 260 | ]
|
201 | 261 | },
|
202 | 262 | {
|
|
217 | 277 | "cell_type": "markdown",
|
218 | 278 | "metadata": {},
|
219 | 279 | "source": [
|
220 |
| - "In Ocean Mapping you will often encounter the concept of [metadata](https://en.wikipedia.org/wiki/Metadata). One of the most common use of metadata is to help users discover and identify data resources. \n", |
| 280 | + "In Ocean Mapping you will often encounter the concept of [metadata](https://en.wikipedia.org/wiki/Metadata). " |
| 281 | + ] |
| 282 | + }, |
| 283 | + { |
| 284 | + "cell_type": "markdown", |
| 285 | + "metadata": {}, |
| 286 | + "source": [ |
| 287 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 288 | + "\n", |
| 289 | + "**Metadata** are a set of data that gives information about other data." |
| 290 | + ] |
| 291 | + }, |
| 292 | + { |
| 293 | + "cell_type": "markdown", |
| 294 | + "metadata": {}, |
| 295 | + "source": [ |
| 296 | + "One of the most common use of metadata is to help users discover and identify data resources. \n", |
221 | 297 | "\n",
|
222 | 298 | "There are different [metadata standards](https://en.wikipedia.org/wiki/Metadata#Standards) for each different field of study, and ocean mapping uses many of these standards. However, for the task of this notebook, we will not explore them. You will get familiar with these standards during your future Ocean Mapping courses. "
|
223 | 299 | ]
|
|
267 | 343 | "cell_type": "markdown",
|
268 | 344 | "metadata": {},
|
269 | 345 | "source": [
|
270 |
| - "This is the first time that we use the [`datetime`](https://docs.python.org/3.6/library/datetime.html?#module-datetime) type! " |
| 346 | + "And yes, you are correct: this is the first time that we use the [`datetime`](https://docs.python.org/3.6/library/datetime.html?#module-datetime) type! " |
271 | 347 | ]
|
272 | 348 | },
|
273 | 349 | {
|
|
290 | 366 | "cell_type": "markdown",
|
291 | 367 | "metadata": {},
|
292 | 368 | "source": [
|
293 |
| - "As you can read from the [Python documentation](https://docs.python.org/3.6/library/datetime.html?#datetime-objects), the `datetime` constructor is part of the `datetime` module (yes, they have both the same name!) and takes several parameters: \n", |
| 369 | + "As you can read from the [Python documentation](https://docs.python.org/3.6/library/datetime.html?#datetime-objects), the `datetime` constructor is part of the `datetime` module (they have both the same name!) and takes several parameters: \n", |
294 | 370 | "\n",
|
295 | 371 | "- `datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)` "
|
296 | 372 | ]
|
|
405 | 481 | "cell_type": "markdown",
|
406 | 482 | "metadata": {},
|
407 | 483 | "source": [
|
408 |
| - "In this last section of this notebook, we will explore different mechanisms that Python provides for printing (**string formatting**) a value." |
| 484 | + "In the last section of this notebook, we will explore a different mechanism for printing (**string formatting**) a value in Python." |
409 | 485 | ]
|
410 | 486 | },
|
411 | 487 | {
|
|
431 | 507 | "cell_type": "markdown",
|
432 | 508 | "metadata": {},
|
433 | 509 | "source": [
|
434 |
| - "You also know that you can type-casting types using `str()`:" |
| 510 | + "You also know that `str()` can be used to **type-cast** values that are different from `str`:" |
435 | 511 | ]
|
436 | 512 | },
|
437 | 513 | {
|
|
453 | 529 | "cell_type": "markdown",
|
454 | 530 | "metadata": {},
|
455 | 531 | "source": [
|
456 |
| - "It is possible to achieve the same results by using the `%` modulo operator like in the following examples:" |
| 532 | + "We now introduce a new method that achieves the same result by using the `%` modulo operator.\n", |
| 533 | + "\n", |
| 534 | + "These are a couple of examples of its usage:" |
457 | 535 | ]
|
458 | 536 | },
|
459 | 537 | {
|
|
487 | 565 | "cell_type": "markdown",
|
488 | 566 | "metadata": {},
|
489 | 567 | "source": [
|
490 |
| - "If you look at the above examples, you will noticed the presence of `%s` as placeholders in the string. The string is followed by a `%` operator, then by one or more variables enclosed in square brackets." |
| 568 | + "By examining the above examples, you will notice that: \n", |
| 569 | + "\n", |
| 570 | + "* The `%s` is used as a **placeholder** in the string containing the message. \n", |
| 571 | + "* The message string is followed by the `%` operator, then by one or more variables enclosed in rounded brackets." |
491 | 572 | ]
|
492 | 573 | },
|
493 | 574 | {
|
|
504 | 585 | "cell_type": "markdown",
|
505 | 586 | "metadata": {},
|
506 | 587 | "source": [
|
507 |
| - "String formatting using the `%` operator provides [additional printing options](https://docs.python.org/3.6/library/stdtypes.html#printf-style-string-formatting). Among them, you can decide how many decimal digits will be printed for a `float` value. \n", |
| 588 | + "String formatting using the `%` operator provides [additional printing options](https://docs.python.org/3.6/library/stdtypes.html#printf-style-string-formatting). One of these options is to define how many decimal digits to print for a `float` value. \n", |
508 | 589 | "\n",
|
509 | 590 | "For instance, by using `%.4f` as a placeholder, Python will print **only** the first four decimal digits: "
|
510 | 591 | ]
|
|
522 | 603 | "print(\"The position is: %.4f, %.4f\" % (metadata[\"latitude\"], metadata[\"longitude\"]))"
|
523 | 604 | ]
|
524 | 605 | },
|
| 606 | + { |
| 607 | + "cell_type": "markdown", |
| 608 | + "metadata": {}, |
| 609 | + "source": [ |
| 610 | + " " |
| 611 | + ] |
| 612 | + }, |
| 613 | + { |
| 614 | + "cell_type": "markdown", |
| 615 | + "metadata": { |
| 616 | + "solution2": "hidden", |
| 617 | + "solution2_first": true |
| 618 | + }, |
| 619 | + "source": [ |
| 620 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n", |
| 621 | + "\n", |
| 622 | + "Print the depth value in the `metadata` dictionary with a centimetric accuracy:" |
| 623 | + ] |
| 624 | + }, |
| 625 | + { |
| 626 | + "cell_type": "code", |
| 627 | + "execution_count": null, |
| 628 | + "metadata": { |
| 629 | + "solution2": "hidden" |
| 630 | + }, |
| 631 | + "outputs": [], |
| 632 | + "source": [ |
| 633 | + "metadata = dict()\n", |
| 634 | + "metadata[\"depth\"] = 129.121\n", |
| 635 | + "\n", |
| 636 | + "print(\"The depth is %.2f m.\" % (metadata[\"depth\"]))" |
| 637 | + ] |
| 638 | + }, |
| 639 | + { |
| 640 | + "cell_type": "code", |
| 641 | + "execution_count": null, |
| 642 | + "metadata": {}, |
| 643 | + "outputs": [], |
| 644 | + "source": [ |
| 645 | + "metadata = dict()\n", |
| 646 | + "metadata[\"depth\"] = 129.121" |
| 647 | + ] |
| 648 | + }, |
525 | 649 | {
|
526 | 650 | "cell_type": "markdown",
|
527 | 651 | "metadata": {},
|
|
0 commit comments