Skip to content

Commit

Permalink
notebooks 0,1,3: cosmetic changes to standardize notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
robinengler committed Sep 25, 2023
1 parent 8c5fcd3 commit 121cfce
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 125 deletions.
9 changes: 7 additions & 2 deletions notebooks/00_jupyter_setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"    [exercise 0.1](#5.1) \n",
"    [exercise 0.2](#5.2) \n",
"\n",
"**[Additional material](#6)** \n",
"**[Additional Material](#6)** \n",
"    [Restarting the Jupyter Notebook kernel](#6.1) \n",
"    [Configuring Jupyter](#6.2) "
]
Expand Down Expand Up @@ -299,9 +299,14 @@
"\n",
"[Back to ToC](#toc)\n",
"\n",
"<div class=\"alert alert-block alert-info\">\n",
"\n",
"# Additional material <a id='6'></a>\n",
"------------------------------\n",
"\n",
"</div>\n",
"\n",
"\n",
"<br>\n",
"\n",
"## Restarting the Jupyter Notebook kernel <a id='6.1'></a>\n",
Expand Down Expand Up @@ -400,7 +405,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
38 changes: 23 additions & 15 deletions notebooks/01_python_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"\n",
"[**Exercises 1.1 - 1.4**](#27)\n",
"\n",
"[**Additional Theory**](#28) \n",
"[**Additional Material**](#28) \n",
"&nbsp;&nbsp;&nbsp;&nbsp;[Mutability of objects in Python](#29) \n",
"&nbsp;&nbsp;&nbsp;&nbsp;[A solution: explicit deep copy](#30) "
]
Expand Down Expand Up @@ -2304,22 +2304,26 @@
"\n",
"## Exercises 1.1 - 1.5 <a id='27'></a>\n",
"\n",
"If you have time, feel free to try the **additional exercises** for module 1.\n",
"\n",
"<br>\n",
"<br>\n",
"<br>\n",
"\n",
"[Back to ToC](#toc)\n",
"\n",
"# Additional Theory <a id='28'></a>\n",
"-----------------------------\n",
"<div class=\"alert alert-block alert-info\">\n",
"\n",
"If you have time, feel free to try the **additional exercises** for module 1.\n",
"# Additional Material <a id='28'></a>\n",
"-------------------------------------\n",
"\n",
"</div>\n",
"\n",
"<br>\n",
"\n",
"### Mutability of objects in Python <a id='29'></a>\n",
"\n",
"All objects in Python can be either **mutable** or **immutable**. This is an important notation that newcomers to Python need to be aware of, which otherwise can lead to serious bugs in our codes.\n",
"All objects in Python can be either **mutable** or **immutable**. This is an important notion that newcomers to Python need to be aware of, which otherwise can lead to serious bugs in our codes.\n",
"\n",
"What do we mean by *mutable*? We learnt earlier that **everything in Python is an object** and every variable holds an instance of an object. Once its type is set at runtime it can never change. A list is always a list, an integer is always an integer. However its value can be modified if it is mutable.\n",
"\n",
Expand Down Expand Up @@ -2365,7 +2369,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try to modify an element (an individual char) in a string: it raises a **`TypeError`** because a string in an immutable type."
"Let's try to modify an element (an individual char) in a string: it raises a **`TypeError`** because a string in an **immutable type**."
]
},
{
Expand All @@ -2387,7 +2391,7 @@
}
},
"source": [
"Let's try to modify an element in a list: this is possible, because a list is a mutable type."
"Let's try to modify an element in a list: this is possible, because a list is a **mutable type**."
]
},
{
Expand All @@ -2409,7 +2413,7 @@
}
},
"source": [
"However, the *immutable* cousin of list, the tuple, does not allow assignment:"
"However, the *immutable* cousin of `list`, the `tuple`, does not allow assignment:"
]
},
{
Expand Down Expand Up @@ -2459,9 +2463,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Now let's modify my_dict\n",
"# Let's now modify my_dict...\n",
"my_dict[\"list\"][0] = \"P\"\n",
"# and see what happens to both dictionaries\n",
"\n",
"# ... and see what happens to both dictionaries.\n",
"print(\"my_dict:\", my_dict)\n",
"print(\"another_dict:\", another_dict)"
]
Expand Down Expand Up @@ -2504,6 +2509,7 @@
"outputs": [],
"source": [
"my_dict[\"str\"] = \"Zython\"\n",
"\n",
"print(\"my_dict:\", my_dict)\n",
"print(\"another_dict:\", another_dict)"
]
Expand All @@ -2517,6 +2523,7 @@
"a_third_dict = my_dict.copy()\n",
"my_dict[\"str\"] = \"Kython\"\n",
"my_dict[\"list\"][0] = \"K\"\n",
"\n",
"print(\"my_dict:\", my_dict)\n",
"print(\"third_dict:\", a_third_dict)"
]
Expand Down Expand Up @@ -2589,17 +2596,17 @@
"source": [
"#### Python memory management: interned vs non-interned values\n",
"\n",
"> Integer values from -5 to 256 are \"interned\", which means they are created once\n",
"> Integer values from -5 to 256 are **\"interned\"**, which means that they are created once\n",
"> and then re-used over the entire runtime of the python program/session.\n",
"\n",
"```py\n",
" a = 256\n",
" b = 256\n",
" print(\"Are 'a' and 'b' pointing to the same object in memory:\", a is b)\n",
" print(\"Are 'a' and 'b' pointing to the same object in memory?:\", a is b)\n",
" print(\"Memory locations of the 2 objects:\", id(a), id(b), sep=\"\\n\", end=\"\\n\\n\")\n",
"```\n",
"```text\n",
" Are 'a' and 'b' pointing to the same object in memory: True\n",
" Are 'a' and 'b' pointing to the same object in memory?: True\n",
" Memory locations of the 2 objects:\n",
" 9801248\n",
" 9801248\n",
Expand Down Expand Up @@ -2671,7 +2678,7 @@
"<br>\n",
"\n",
"### Benchmarking: looping speed of tuples vs lists\n",
"* As can be tested below, there is no speed difference between lists and tuples.\n",
"* As can be tested below, there is no speed difference between `lists` and `tuples`.\n",
"* Generators are faster (probably because they skip the step where elements of the sequence must\n",
" be stored in memory)."
]
Expand All @@ -2683,6 +2690,7 @@
"outputs": [],
"source": [
"# Functions that do nothing but loop through a list, tuple or generator.\n",
"\n",
"loop_replicates = 1000000\n",
"\n",
"def loop_range():\n",
Expand Down Expand Up @@ -2720,7 +2728,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 121cfce

Please sign in to comment.