From be44243136d710eec0345f8459a64da377bab357 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sun, 29 Mar 2026 13:43:38 +0200 Subject: Re-format reveal.js slides and add templates --- notebooks/python/python_intro.ipynb | 120 ++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 39 deletions(-) (limited to 'notebooks/python/python_intro.ipynb') diff --git a/notebooks/python/python_intro.ipynb b/notebooks/python/python_intro.ipynb index f129ffe..f101400 100644 --- a/notebooks/python/python_intro.ipynb +++ b/notebooks/python/python_intro.ipynb @@ -48,6 +48,19 @@ "- Calling C from Python: Cython, CFFI, ctypes" ] }, + { + "cell_type": "markdown", + "id": "779bac39-4646-4b80-9970-343a3daef1e7", + "metadata": {}, + "source": [ + "## Important PEPs\n", + "\n", + "- [PEP0](https://peps.python.org/) - Index of Python Enhancement Proposals\n", + "- [PEP8](https://peps.python.org/pep-0008/) - Style Guide for Python Code\n", + "- [PEP257](https://peps.python.org/pep-0257/) - Docstring Conventions\n", + "- [PEP484](https://peps.python.org/pep-0484/) - Type Hints" + ] + }, { "cell_type": "markdown", "id": "6d1edcfc-b001-4393-9c24-72adc9b5fccf", @@ -72,7 +85,7 @@ }, { "cell_type": "markdown", - "id": "4f144f73-51c1-419d-b71e-ee513f68f41c", + "id": "81573ed5-b76e-4726-9f44-7dc017fd0896", "metadata": {}, "source": [ "## Built-in functions\n", @@ -81,8 +94,6 @@ "\n", "[https://docs.python.org/3/library/functions.html](https://docs.python.org/3/library/functions.html)\n", "\n", - "- `dir([obj])` - returns a list of valid attributes for that object\n", - "\n", "- `id(obj)` - returns the \"identity\" of an object - an integer which is guaranteed to be unique\n", "\n", "- `print(...)` - prints objects to a text stream\n", @@ -92,6 +103,12 @@ "- `type(obj)` - returns the type of an object" ] }, + { + "cell_type": "markdown", + "id": "bc180243-fe11-405c-beb9-83db4f793a05", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "id": "a0c57535-e285-47af-80f9-1d4a16de5f25", @@ -116,26 +133,28 @@ "source": [ "# Common built-in types:\n", "\n", - "s = 'foo' # this is a string / str, same as str('foo'), may be encoded, immutable (s[0] = 'r' is NOT possible)\n", + "my_string = 'foo' # a string / str, same as str('foo'), may be encoded\n", + " # immutable (s[0] = 'r' is NOT possible)\n", "\n", - "b = b'foo' # bytes, same as bytes('foo', 'utf-8'), may be decoded, immutable\n", + "my_bytes = b'foo' # bytes, same as bytes('foo', 'utf-8'), may be decoded, immutable\n", "\n", - "i = 6 # int, same as int('6'), immutable\n", + "my_int = 6 # int, same as int('6'), immutable\n", "\n", - "f = 0.1 # float, same as float('0.1'), immutable, Note!!: Floats have a fixed size,\n", - "# hence they don't necessarily behave they way we expect from math class.\n", + "my_float = 0.1 # float, same as float('0.1'), immutable, Note!!: Floats have a fixed size,\n", + " # hence they don't necessarily behave they way we expect from math class.\n", + "# 24732847234232342892343428.3 == 24732847234232342892343428.1 # True\n", "\n", - "b = False # bool, same as bool(0), bool(''), bool(None)... immutable / constant\n", + "my_bool = False # bool, same as bool(0), bool(''), bool(None)... immutable / constant\n", "\n", - "n = None # NoneType, similar to 'null' in other languages, immutable / constant\n", + "my_none = None # NoneType, similar to 'null' in other languages, immutable / constant\n", "\n", - "l = [1, False, 'foo'] # list, same as list((1, False, 'foo'))\n", + "my_list = [1, False, 'foo'] # list, same as list((1, False, 'foo'))\n", "\n", - "t = (1, False, 'foo') # tuple, same as tuple([1, False, 'foo']), immutable\n", + "my_tuple = (1, False, 'foo') # tuple, same as tuple([1, False, 'foo']), immutable\n", "\n", - "d = {'foo': 1, 'bar': 8} # dict, same as dict(foo=1, bar=8), similar to hash in other languages\n", + "my_dict = {'foo': 1, 'bar': 8} # dict, same as dict(foo=1, bar=8), similar to hash in other languages\n", "\n", - "s = {'foo', 'bar', 1, 1, 4} # set, same as set(['foo', 'bar', 1, 1, 4]), removes duplicates" + "my_set = {'foo', 'bar', 1, 1, 4} # set, same as set(['foo', 'bar', 1, 1, 4]), removes duplicates" ] }, { @@ -203,7 +222,7 @@ "s3[0] # Out: S\n", "s3[-1] # Out: t\n", "s3[1:] # Out: tatnett\n", - "s3[1:-1] # Out: tatnett\n", + "s3[1:-1] # Out: tatnet\n", "s3[-3:] # Out: ett\n", "s3[1:-1:2] # Out: tte\n", "\n", @@ -221,6 +240,29 @@ "b'B\\xc3\\x98!'.decode('utf-8') # utf-8 is implicit in Python 3" ] }, + { + "cell_type": "markdown", + "id": "25d0f9dc-d783-4a23-a3ee-c0b6cee3d4ab", + "metadata": {}, + "source": [ + "## Lists vs. tuples\n", + "\n", + "Tuples are not sumply \"read-only\" lists.\n", + "\n", + "\"Though tuples may seem similar to lists, they are often used in different situations and for different purposes.\n", + "Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking ... or indexing.\n", + "Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.\n", + "\n", + "```python\n", + "my_tuple = (1,2)\n", + "my_list = [1,2] \n", + "\n", + "# tuples are immutable and hashable, hence they can be used as keys in mapping objects as dicts\n", + "d = {my_tuple: 1} # OK\n", + "d = {my_list: 1} # Error\n", + "```" + ] + }, { "cell_type": "markdown", "id": "6b2bc8fa-a5dc-4a58-92b2-abfb19bfe2ed", @@ -302,7 +344,7 @@ "id": "40db50e4-fb7a-4a77-a074-bc4413f27212", "metadata": {}, "source": [ - "## Creating and maintaining a Python environment (cont...)\n", + "## Creating and maintaining a Python environment - goals\n", "\n", "Desired qualities for a flexible Python environment:\n", "\n", @@ -324,10 +366,11 @@ "id": "c661bf56-add0-402c-a629-a857f81d0758", "metadata": {}, "source": [ - "## Creating and maintaining a Python environment (cont...)\n", + "## Creating and maintaining a Python environment - using devbox / WSL / UNIX systems directly\n", "\n", "Exploting the operating system can be done by:\n", "\n", + "- using the official Python packages that are maintained by the OS (Ubuntu packages in the case of WSL @ Statnett or a Red Hat enterprise VM @ Statnett) is often good enough\n", "- (re)defining `PYTHONPATH`\n", "- using symlinks to point at packages placed at different locations" ] @@ -337,7 +380,7 @@ "id": "db8952aa-0944-4644-9626-7460786fd72a", "metadata": {}, "source": [ - "## Creating and maintaining a Python environment (cont...)\n", + "## Creating and maintaining a Python environment - using the Python virtual environment - *venv*\n", "\n", "Using venv can be done by directly invoking python:\n", "\n", @@ -365,10 +408,13 @@ "id": "299641e6-3d87-4a86-a124-eb49edb75b4b", "metadata": {}, "source": [ - "## Creating and maintaining a Python environment (cont...)\n", + "## Creating and maintaining a Python environment - using Poetry\n", "\n", "Poetry [https://python-poetry.org](https://python-poetry.org) is the prefered environment and dependency management tool at Statnett.\n", "\n", + "Although Poetry creates and uses vierual environment(s) behind the scenes,\n", + "it is centered around the concept of \"projects\" and not the virtual environment itself\n", + "\n", "```bash\n", "# create project and a virtual environment from scratch\n", "poetry new my-project\n", @@ -411,7 +457,7 @@ { "data": { "text/plain": [ - "-42990858669078524" + "-3158428850515418558" ] }, "execution_count": 15, @@ -435,8 +481,8 @@ "hash(t) # returns f.i. -6333845781340707986\n", "# hash(l) # TypeError: unhashable type: 'list'\n", "\n", - "s = 'the long and winding road'\n", - "s2 = 'the long and winding road'\n", + "s = \"the long and winding road\"\n", + "s2 = \"the long and winding road\"\n", "\n", "# check if s and s2 are the same object:\n", "id(s) # Out: 139858905258704\n", @@ -816,7 +862,7 @@ "# the function will behave differently depending on what parameters were\n", "# used when calling its enclosing function (factory function)\n", "\n", - "def get_multiplier_of(base: int) -> str:\n", + "def get_multiplier_of(base: int):\n", " \"\"\"the function enclosing its nested functions\"\"\"\n", " # this function is the enclosing function of the function `multiplier_function`\n", "\n", @@ -958,19 +1004,7 @@ "execution_count": 22, "id": "4346260d-76af-437e-9c10-1c1d0c478695", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'is_admin' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[22], line 25\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m decorated\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m api_access_decorator\n\u001b[1;32m 24\u001b[0m \u001b[38;5;129m@requires_access\u001b[39m(access_secret\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mb28cfeaa65b73cf\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m---> 25\u001b[0m \u001b[38;5;129m@is_admin\u001b[39m\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msensitive_function\u001b[39m(data, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 27\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"very sensitive function\"\"\"\u001b[39;00m\n\u001b[1;32m 28\u001b[0m db\u001b[38;5;241m.\u001b[39msave(data)\n", - "\u001b[0;31mNameError\u001b[0m: name 'is_admin' is not defined" - ] - } - ], + "outputs": [], "source": [ "# Decorators (cont ...) - a complete example\n", "\n", @@ -996,7 +1030,7 @@ "\n", "\n", "@requires_access(access_secret='b28cfeaa65b73cf')\n", - "@is_admin\n", + "# @is_admin - decorators can be \"chained\"\n", "def sensitive_function(data, **kwargs):\n", " \"\"\"very sensitive function\"\"\"\n", " db.save(data)" @@ -1030,11 +1064,19 @@ "\n", "\n", "# modern Python >= 3.6 f-strings\n", - "f'{s} - {i} - {f:5.2f}' # Out: 'another string - 27 - 6.58'\n", + "f\"{s} - {i} - {f:5.2f}\" # Out: 'another string - 27 - 6.58'\n", "``` \n", "\n", "See https://docs.python.org/3/library/string.html#formatspec for the complete format specification" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23972592-e838-4fa7-81f1-ca3ca129e757", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -1053,7 +1095,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.13.9" } }, "nbformat": 4, -- cgit v1.3