Last active
April 26, 2026 09:59
-
-
Save mvoelk/7ff52dbc2723f3a50752e00d2bb629df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Overview\n", | |
| "\n", | |
| "Gist\n", | |
| "https://gist.github.com/mvoelk/7ff52dbc2723f3a50752e00d2bb629df\n", | |
| "\n", | |
| "Google Colab\n", | |
| "https://colab.research.google.com/gist/mvoelk/7ff52dbc2723f3a50752e00d2bb629df\n", | |
| "\n", | |
| "## Motivation: Why Python?\n", | |
| "\n", | |
| "- Widely used, general purpose programming language with all kinds of libraries\n", | |
| "- Used for scientific computing, similar functionalities as Matlab, R, ...\n", | |
| "- Used by deep learning frameworks such as TensorFlow, PyTorch or JAX\n", | |
| "- Easy to start with :)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Setup\n", | |
| "\n", | |
| "## Python Insallation\n", | |
| "- On **Linux** Python is typically already installed; if not, install it via the os package management (<code>apt install python3</code> under Ubuntu, Debian, etc.)\n", | |
| "- On **Windows** use the installer from https://www.python.org\n", | |
| "\n", | |
| "## IDE and Editors\n", | |
| "- Visual Studio Code (https://code.visualstudio.com) is a free and full-featured IDE form Microsoft\n", | |
| "- Sublime Text (https://www.sublimetext.com) is text editor with basic features like syntax highlighting (no limitations without registration)\n", | |
| "- Nano and Vim are popular editors on the Linux Terminal\n", | |
| "\n", | |
| "## Jupyter Notebook\n", | |
| "Jupyter Notebook is what you see here, a file format and tool for writing, interactive code execution, visualization and much more.\n", | |
| "- It can be installed as python package via <code>pip install notebook</code>\n", | |
| "- The server can be started via <code>jupyter-notebook</code> in your python environment and can be reached under http://localhost:8888 in your browser\n", | |
| "- The Notebook files have the extension <code>.ipynb</code> and are also supported by VS Code or GitHub\n", | |
| "- Google Colab provides a free environment for notebook execution and basic GPU support" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Python" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Python 3.12.3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!python --version" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Help\n", | |
| "\n", | |
| "Official Python documentation \n", | |
| "https://docs.python.org/3\n", | |
| "\n", | |
| "- Python comes with a build in help system and documentation is typically done as part of the code" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Help on _Helper in module _sitebuiltins object:\n", | |
| "\n", | |
| "class _Helper(builtins.object)\n", | |
| " | Define the builtin 'help'.\n", | |
| " |\n", | |
| " | This is a wrapper around pydoc.help that provides a helpful message\n", | |
| " | when 'help' is typed at the Python interactive prompt.\n", | |
| " |\n", | |
| " | Calling help() at the Python prompt starts an interactive help session.\n", | |
| " | Calling help(thing) prints help for the python object 'thing'.\n", | |
| " |\n", | |
| " | Methods defined here:\n", | |
| " |\n", | |
| " | __call__(self, *args, **kwds)\n", | |
| " | Call self as a function.\n", | |
| " |\n", | |
| " | __repr__(self)\n", | |
| " | Return repr(self).\n", | |
| " |\n", | |
| " | ----------------------------------------------------------------------\n", | |
| " | Data descriptors defined here:\n", | |
| " |\n", | |
| " | __dict__\n", | |
| " | dictionary for instance variables\n", | |
| " |\n", | |
| " | __weakref__\n", | |
| " | list of weak references to the object\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "help(help)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "View documentation and source code of objects and functions in Jupyter Notebook" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "\u001b[31mSignature:\u001b[39m help(*args, **kwds)\n", | |
| "\u001b[31mType:\u001b[39m _Helper\n", | |
| "\u001b[31mString form:\u001b[39m Type help() for interactive help, or help(object) for help about object.\n", | |
| "\u001b[31mNamespace:\u001b[39m Python builtin\n", | |
| "\u001b[31mFile:\u001b[39m /usr/lib/python3.12/_sitebuiltins.py\n", | |
| "\u001b[31mDocstring:\u001b[39m \n", | |
| "Define the builtin 'help'.\n", | |
| "\n", | |
| "This is a wrapper around pydoc.help that provides a helpful message\n", | |
| "when 'help' is typed at the Python interactive prompt.\n", | |
| "\n", | |
| "Calling help() at the Python prompt starts an interactive help session.\n", | |
| "Calling help(thing) prints help for the python object 'thing'." | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "help?" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "\u001b[31mSignature:\u001b[39m help(*args, **kwds)\n", | |
| "\u001b[31mType:\u001b[39m _Helper\n", | |
| "\u001b[31mString form:\u001b[39m Type help() for interactive help, or help(object) for help about object.\n", | |
| "\u001b[31mNamespace:\u001b[39m Python builtin\n", | |
| "\u001b[31mFile:\u001b[39m /usr/lib/python3.12/_sitebuiltins.py\n", | |
| "\u001b[31mSource:\u001b[39m \n", | |
| "\u001b[38;5;28;01mclass\u001b[39;00m _Helper(object):\n", | |
| " \u001b[33m\"\"\"Define the builtin 'help'.\u001b[39m\n", | |
| "\n", | |
| "\u001b[33m This is a wrapper around pydoc.help that provides a helpful message\u001b[39m\n", | |
| "\u001b[33m when 'help' is typed at the Python interactive prompt.\u001b[39m\n", | |
| "\n", | |
| "\u001b[33m Calling help() at the Python prompt starts an interactive help session.\u001b[39m\n", | |
| "\u001b[33m Calling help(thing) prints help for the python object 'thing'.\u001b[39m\n", | |
| "\u001b[33m \"\"\"\u001b[39m\n", | |
| "\n", | |
| " \u001b[38;5;28;01mdef\u001b[39;00m __repr__(self):\n", | |
| " \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[33m\"Type help() for interactive help, \"\u001b[39m \\\n", | |
| " \u001b[33m\"or help(object) for help about object.\"\u001b[39m\n", | |
| " \u001b[38;5;28;01mdef\u001b[39;00m __call__(self, *args, **kwds):\n", | |
| " \u001b[38;5;28;01mimport\u001b[39;00m pydoc\n", | |
| " \u001b[38;5;28;01mreturn\u001b[39;00m pydoc.help(*args, **kwds)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "help??" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Syntax\n", | |
| "\n", | |
| "- Indentation is used to indicate code blocks and enforce readability\n", | |
| "- No curly brackets or semicolons as in C-like languages\n", | |
| "- Spaces and tabs are not the same and require some attention" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, World!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "if 5 > 2:\n", | |
| " print(\"Hello, World!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Comments" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Hello, World!\n", | |
| "Hello, World!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# this is a comment\n", | |
| "print(\"Hello, World!\") # this is also a comment \n", | |
| "\n", | |
| "'''\n", | |
| "This is a comment\n", | |
| "over multiple lines\n", | |
| "'''\n", | |
| "print(\"Hello, World!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Variables and Data Types\n", | |
| "\n", | |
| "Python is dynamically typed by default, but it supports optional static typing via type hints" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "int" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x = 5\n", | |
| "\n", | |
| "type(x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "isinstance(x, int)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "isinstance(x, str)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Strings\n", | |
| "\n", | |
| "| Escape Characters | |\n", | |
| "| :--- | :--- |\n", | |
| "| \\\\\" | Double Quote |\n", | |
| "| \\\\' | Single Quote |\n", | |
| "| \\\\\\\\ | Backslash |\n", | |
| "| \\n | New Line |\n", | |
| "| \\r | Carriage Return |\n", | |
| "| \\t | Tab |\n", | |
| "| \\b | Backspace |\n", | |
| "| \\f | Form Feed |\n", | |
| "| \\ooo | Octal value |\n", | |
| "| \\xhh | Hex value |" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "\"hello 'world' !\"" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s1 = 'hello'\n", | |
| "s2 = \"'world' !\"\n", | |
| "\n", | |
| "s3 = s1 + ' ' + s2\n", | |
| "\n", | |
| "s3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'hello hello hello !!!'" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s4 = (s1 + ' ') * 3 + '!' * 3\n", | |
| "s4" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(str, 15)" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(s3), len(s3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'orl'" | |
| ] | |
| }, | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s2[2:5]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "\"orld'\"" | |
| ] | |
| }, | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s2[2:-2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'world' in s3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'long\\nlong\\nstring'" | |
| ] | |
| }, | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s = \"\"\"long\n", | |
| "long\n", | |
| "string\"\"\"\n", | |
| "\n", | |
| "s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "long\n", | |
| "long\n", | |
| "string\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Numbers" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(100000, 50159747054, 29973)" | |
| ] | |
| }, | |
| "execution_count": 18, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Numeric Literals (decimal, hexadecimal, binary)\n", | |
| "100000, 0xbadc0ffee, 0b0111010100010101" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(100000, 50159747054, 29973)" | |
| ] | |
| }, | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Syntactic tweak for readability, since Python 3.6\n", | |
| "100_000, 0xbad_c0ffee, 0b_0111_0101_0001_0101" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(-1, int)" | |
| ] | |
| }, | |
| "execution_count": 20, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = -1\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(1.0, float)" | |
| ] | |
| }, | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = 1.\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "((2+1j), complex)" | |
| ] | |
| }, | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = 1.0j + 2\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(-8.77e+101, float)" | |
| ] | |
| }, | |
| "execution_count": 23, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = -87.7e100\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Number Functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(5, 25)" | |
| ] | |
| }, | |
| "execution_count": 24, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "abs(-5), pow(-5, 2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Lists\n", | |
| "\n", | |
| "Lists are ordered and changeable collections, that allow duplicates" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(list, [1, 2, 3, 'foobar'])" | |
| ] | |
| }, | |
| "execution_count": 25, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "\n", | |
| "type(l), l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 26, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 26, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l[2]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2, 'foobar', 4, 5, 9]" | |
| ] | |
| }, | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "l.remove(3)\n", | |
| "l.append(4)\n", | |
| "l.extend([5,9])\n", | |
| "l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('foobar', [1, 2, 99, 3])" | |
| ] | |
| }, | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "e = l.pop(3)\n", | |
| "l.insert(2, 99)\n", | |
| "e, l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 29, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[]" | |
| ] | |
| }, | |
| "execution_count": 29, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "l.clear()\n", | |
| "l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "1" | |
| ] | |
| }, | |
| "execution_count": 30, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "l.index(2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2" | |
| ] | |
| }, | |
| "execution_count": 31, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,2]\n", | |
| "l.count(2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 32, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['foobar', 3, 2, 1]" | |
| ] | |
| }, | |
| "execution_count": 32, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,2,3,'foobar']\n", | |
| "l.reverse()\n", | |
| "l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 33, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2, 3, 6]" | |
| ] | |
| }, | |
| "execution_count": 33, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,6,3,2]\n", | |
| "l.sort()\n", | |
| "l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[3, 2, 1, 6]" | |
| ] | |
| }, | |
| "execution_count": 34, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = [1,6,3,2]\n", | |
| "l.sort(key=lambda x: abs(3-x))\n", | |
| "l" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "([1, 2, 3, 4], [1, 2, 3, 'foobar'])" | |
| ] | |
| }, | |
| "execution_count": 35, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l1 = [1,2,3,'foobar']\n", | |
| "l2 = l1.copy()\n", | |
| "l1[3] = 4\n", | |
| "l1, l2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2, 3, 4, 5, 6]" | |
| ] | |
| }, | |
| "execution_count": 36, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l1 = [1,2,3]\n", | |
| "l2 = [4,5,6]\n", | |
| "l3 = l1 + l2\n", | |
| "l3" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "List Functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(4, 10, 1, 4)" | |
| ] | |
| }, | |
| "execution_count": 37, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l1 = [1,2,3,4]\n", | |
| "len(l1), sum(l1), min(l1), max(l1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2.2, 5, 8, 9]" | |
| ] | |
| }, | |
| "execution_count": 38, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sorted([1,8,9,2.2,5])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "List Comprehensios" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 4, 9, 16, 25]" | |
| ] | |
| }, | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l1 = [1,2,3,4,5]\n", | |
| "l2 = [ i**2 for i in l1 ]\n", | |
| "l2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 40, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2, 4]" | |
| ] | |
| }, | |
| "execution_count": 40, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l1 = [1,2,3,4,5]\n", | |
| "l2 = [ i for i in l1 if i%2 == 0 ]\n", | |
| "l2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Tuples" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Tuples are ordered and unchangeable collections, that allow duplicates" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 41, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(tuple, (1, 2, 3, 'foobar'))" | |
| ] | |
| }, | |
| "execution_count": 41, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x = (1,2,3,'foobar')\n", | |
| "\n", | |
| "type(x), x" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 42, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(2, 3)" | |
| ] | |
| }, | |
| "execution_count": 42, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x[1:3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 43, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(3, 'foobar')" | |
| ] | |
| }, | |
| "execution_count": 43, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x[-2:]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 44, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "4" | |
| ] | |
| }, | |
| "execution_count": 44, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "len(x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 45, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 45, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "3 in x" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Sets\n", | |
| "\n", | |
| "Sets are unordered and unindexed collections without duplicates" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 46, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(set, {'abc', 'cde', 'efg'})" | |
| ] | |
| }, | |
| "execution_count": 46, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s = {'abc', 'cde', 'efg'}\n", | |
| "type(s), s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 47, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(True, False)" | |
| ] | |
| }, | |
| "execution_count": 47, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'cde' in s, 'xyz' in s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 48, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(4, {'cde', 'efg', 'hij', 'klm'})" | |
| ] | |
| }, | |
| "execution_count": 48, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s.add('hij')\n", | |
| "s.update({'cde', 'klm'})\n", | |
| "#s.remove('abc')\n", | |
| "s.discard('abc') # will not throw an error if element does not exist\n", | |
| "\n", | |
| "len(s), s" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Set Operations" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 49, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'abc', 'cde', 'efg'}" | |
| ] | |
| }, | |
| "execution_count": 49, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s1 = {'abc', 'cde', 'efg'}\n", | |
| "s2 = {'efg', 'cde', 'efg'}\n", | |
| "s1.union(s2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 50, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'cde', 'efg'}" | |
| ] | |
| }, | |
| "execution_count": 50, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s1.intersection(s2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 51, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'cde', 'efg'}" | |
| ] | |
| }, | |
| "execution_count": 51, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "set.intersection(s1,s2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 52, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'abc', 'cde', 'efg'}" | |
| ] | |
| }, | |
| "execution_count": 52, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "set.union(s1, s2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Dictionaries\n", | |
| "\n", | |
| "Dictionaries are unordered, changeable collections, which map keys to values\n", | |
| "- A key can be everything that can be hashed" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 53, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'first': 'Max', 'name': 'Muster', 'age': 24}" | |
| ] | |
| }, | |
| "execution_count": 53, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d = {\n", | |
| " 'first': 'Max',\n", | |
| " 'name': 'Muster',\n", | |
| " 'age': 24,\n", | |
| "}\n", | |
| "\n", | |
| "d" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 54, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 54, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "len(d)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 55, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Muster'" | |
| ] | |
| }, | |
| "execution_count": 55, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d['name']" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 56, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'first': 'Max', 'name': 'Muster', 'age': 24, 3: 5}\n", | |
| "{'first': 'Max', 'name': 'Muster', 'age': 24}\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "5" | |
| ] | |
| }, | |
| "execution_count": 56, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d[3] = 5\n", | |
| "print(d)\n", | |
| "v = d.pop(3)\n", | |
| "print(d)\n", | |
| "v" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 57, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "dict_keys(['first', 'name', 'age'])" | |
| ] | |
| }, | |
| "execution_count": 57, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d.keys()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 58, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "dict_values(['Max', 'Muster', 24])" | |
| ] | |
| }, | |
| "execution_count": 58, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d.values()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 59, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "dict_items([('first', 'Max'), ('name', 'Muster'), ('age', 24)])" | |
| ] | |
| }, | |
| "execution_count": 59, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d.items()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 60, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "first Max\n", | |
| "name Muster\n", | |
| "age 24\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for k, v in d.items():\n", | |
| " print(k, v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Dict Comprehension" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 61, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'k1': 1, 'k2': 4, 'k3': 9, 'k4': 16, 'k5': 25}" | |
| ] | |
| }, | |
| "execution_count": 61, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d1 = {'k'+str(i): i**2 for i in [1,2,3,4,5]}\n", | |
| "d1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 62, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'k1': 2, 'k2': 8, 'k3': 18, 'k4': 32, 'k5': 50}" | |
| ] | |
| }, | |
| "execution_count": 62, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d2 = { k: v*2 for k,v in d1.items() }\n", | |
| "d2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Union and update" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 63, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'foo': 3, 'bar': 7, 'key': 8}" | |
| ] | |
| }, | |
| "execution_count": 63, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d1 = {'foo': 3, 'bar': 2}\n", | |
| "d2 = {'bar': 7, 'key': 8}\n", | |
| "\n", | |
| "{**d1, **d2}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 64, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'foo': 3, 'bar': 7, 'key': 8}" | |
| ] | |
| }, | |
| "execution_count": 64, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d1.update(d2)\n", | |
| "d1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Since Python 3.9" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 65, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'foo': 3, 'bar': 7, 'key': 8}" | |
| ] | |
| }, | |
| "execution_count": 65, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d1 = {'foo': 3, 'bar': 2}\n", | |
| "d2 = {'bar': 7, 'key': 8}\n", | |
| "\n", | |
| "d1 | d2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 66, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'foo': 3, 'bar': 7, 'key': 8}" | |
| ] | |
| }, | |
| "execution_count": 66, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d1 |= d2\n", | |
| "d1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Booleas" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 67, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "bool" | |
| ] | |
| }, | |
| "execution_count": 67, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "b1 = True\n", | |
| "b2 = False\n", | |
| "\n", | |
| "type(b1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Boolean Operations" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 68, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 68, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "b2 and b1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 69, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 69, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "b2 or b1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 70, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 70, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "not b1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Bytes\n", | |
| "\n", | |
| "The <code>bytes</code> type represents binary data\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 71, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(bytes, 6)" | |
| ] | |
| }, | |
| "execution_count": 71, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d = b'foobar'\n", | |
| "type(d), len(d)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Casting" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 72, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(1, int)" | |
| ] | |
| }, | |
| "execution_count": 72, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = int(1)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 73, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(2, int)" | |
| ] | |
| }, | |
| "execution_count": 73, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = int(2.6)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 74, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(33, int)" | |
| ] | |
| }, | |
| "execution_count": 74, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = int('33')\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 75, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(1.0, float)" | |
| ] | |
| }, | |
| "execution_count": 75, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = float(1)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 76, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(2.3, float)" | |
| ] | |
| }, | |
| "execution_count": 76, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = float(2.3)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 77, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(2.3, float)" | |
| ] | |
| }, | |
| "execution_count": 77, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = float('2.3')\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 78, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('1', str)" | |
| ] | |
| }, | |
| "execution_count": 78, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = str(1)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 79, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('2.3', str)" | |
| ] | |
| }, | |
| "execution_count": 79, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = str(2.3)\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 80, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('2.3', str)" | |
| ] | |
| }, | |
| "execution_count": 80, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "v = str('2.3')\n", | |
| "v, type(v)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 81, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(True, True, True)" | |
| ] | |
| }, | |
| "execution_count": 81, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "bool('abc'), bool(123), bool(['abc','xyz'])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 82, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(False, False, False, False, False, False)" | |
| ] | |
| }, | |
| "execution_count": 82, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "bool(None), bool(0), bool(\"\"), bool(()), bool([]), bool({})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 83, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "('True', 1, 1.0)" | |
| ] | |
| }, | |
| "execution_count": 83, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "str(True), int(True), float(True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 84, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "([1, 2, 3], (1, 2, 3))" | |
| ] | |
| }, | |
| "execution_count": 84, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list((1,2,3)), tuple([1,2,3])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Operators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Arithmetic\n", | |
| "\n", | |
| "| Operator | Name | Example |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| + | Addition | x + y |\n", | |
| "| - | Subtraction | x - y |\n", | |
| "| * | Multiplication | x * y |\n", | |
| "| / | Division | x / y |\n", | |
| "| % | Modulus | x % y |\n", | |
| "| // | Floor division | x // y |\n", | |
| "| ** | Exponentiation | x ** y |\n", | |
| "\n", | |
| "### Bitwise\n", | |
| "\n", | |
| "| Operator | Name | Description |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| & | AND | Sets each bit to 1 if both bits are 1 |\n", | |
| "| | | OR | Sets each bit to 1 if one of two bits is 1 |\n", | |
| "| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |\n", | |
| "| ~ | NOT | Inverts all the bits |\n", | |
| "| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |\n", | |
| "| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |\n", | |
| "\n", | |
| "\n", | |
| "### Assignment\n", | |
| "\n", | |
| "| Operator | Example | Same As |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| = | x = 5 | x = 5 |\n", | |
| "| += | x += 3 | x = x + 3 |\n", | |
| "| -= | x -= 3 | x = x - 3 |\n", | |
| "| *= | x *= 3 | x = x * 3 |\n", | |
| "| /= | x /= 3 | x = x / 3 |\n", | |
| "| %= | x %= 3 | x = x % 3 |\n", | |
| "| //= | x //= 3 | x = x // 3 |\n", | |
| "| **= | x **= 3 | x = x ** 3 |\n", | |
| "| &= | x &= 3 | x = x & 3 |\n", | |
| "| |= | x |= 3 | x = x | 3 |\n", | |
| "| ^= | x ^= 3 | x = x ^ 3 |\n", | |
| "| >>= | x >>= 3 | x = x >> 3 |\n", | |
| "| <<= | x <<= 3 | x = x << 3 |\n", | |
| "\n", | |
| "### Coparison\n", | |
| "\n", | |
| "| Operator | Name | Example |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| == | Equal | x == y |\n", | |
| "| != | Not equal | x != y |\n", | |
| "| > | Greater than | x > y |\n", | |
| "| < | Less than | x < y |\n", | |
| "| >= | Greater than or equal to | x >= y |\n", | |
| "| <= | Less than or equal to | x <= y |\n", | |
| "\n", | |
| "### Logical\n", | |
| "\n", | |
| "| Operator | Description | Example |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| and | Returns True if both statements are true | x < 5 and x < 10 |\n", | |
| "| or | Returns True if one of the statements is true | x < 5 or x < 4 |\n", | |
| "| not | Reverse the result, returns False if the result is true | not (x < 5 and x < 10) |\n", | |
| "\n", | |
| "### Identity\n", | |
| "\n", | |
| "| Operator | Description | Example |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| is | Returns True if both variables are the same object | x is y |\n", | |
| "| is not | Returns True if both variables are not the same object | x is not y |\n", | |
| "\n", | |
| "### Membership\n", | |
| "\n", | |
| "| Operator | Description | Example |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| in | Returns True if a sequence with the specified value is present in the object | x in y |\n", | |
| "| not in | Returns True if a sequence with the specified value is not present in the object | x not in y |\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Control Flow" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### If ... Else Conditions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 85, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "a equal b\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "a, b = 30, 30\n", | |
| "\n", | |
| "if a > b:\n", | |
| " print('a greater b')\n", | |
| "elif a < b:\n", | |
| " print('a less b')\n", | |
| "else:\n", | |
| " print('a equal b')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 86, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "if a > b: print('a greater b')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 87, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "4" | |
| ] | |
| }, | |
| "execution_count": 87, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "c = 4 if b > 3 else 5\n", | |
| "\n", | |
| "c" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Comparison Examples" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 88, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(True, True, False, True, False, True)" | |
| ] | |
| }, | |
| "execution_count": 88, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "5 > 3, 3 <= 4, 3 == 5, 3 != 5, 3 > 2 and 4 < 2, 5 >= 5 and not 3 > 4" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<code>==</code> vs. <code>is</code> or equal vs. identical\n", | |
| "\n", | |
| "- <code>==</code> compares values\n", | |
| "- <code>is</code> compares references" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 89, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(True, True, True, False)" | |
| ] | |
| }, | |
| "execution_count": 89, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "a = [1,2,3]\n", | |
| "b = a\n", | |
| "c = [1,2,3]\n", | |
| "\n", | |
| "a == b, a == c, a is b, a is c" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<code>a</code> and <code>b</code> point to the sampe object, <code>c</code> does not" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### While Loops\n", | |
| "\n", | |
| "Executes a set of statements as long as a condition is <code>True</code>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 90, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 2 3 4 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "i = 1\n", | |
| "while i < 5:\n", | |
| " print(i, end=' ')\n", | |
| " i += 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### For Loops\n", | |
| "\n", | |
| "Iterates over iterables\n", | |
| "\n", | |
| "- Iterables are object we can loop over\n", | |
| "- Due to the Python interpreter, loops are notoriously slow in general\n", | |
| "- For most data elements, <code>map</code>, list comprehensions, generator expressions or NumPy operations are much faster then loops" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 91, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "a\n", | |
| "b\n", | |
| "c\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for c in 'abc':\n", | |
| " print(c)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 92, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "abc\n", | |
| "def\n", | |
| "ghi\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for s in ['abc', 'def', 'ghi']:\n", | |
| " print(s)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 93, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0 1 2 3 4 5 6 7 8 9 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for i in range(10):\n", | |
| " print(i, end=' ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Break and Continue\n", | |
| "\n", | |
| "The <code>break</code> statement stops the loop before it has looped through all iterations\n", | |
| "\n", | |
| "The <code>continue</code> statement stops the current iteration of the loop and continue with the next iteration" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 94, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "8 5\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "k = 0\n", | |
| "i = 0\n", | |
| "\n", | |
| "while True:\n", | |
| " i += 1\n", | |
| " if i == 8:\n", | |
| " break\n", | |
| " if i % 3 == 0:\n", | |
| " continue\n", | |
| " k += 1\n", | |
| "\n", | |
| "print(i, k)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 95, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "8 5\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "k = 0\n", | |
| "\n", | |
| "for i in range(10):\n", | |
| " if i == 8:\n", | |
| " break\n", | |
| " if i % 3 == 0:\n", | |
| " continue\n", | |
| " k += 1\n", | |
| "\n", | |
| "print(i, k)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Exceptions and Error Handling\n", | |
| "\n", | |
| "- Assertions are mainly used during development or for debugging\n", | |
| "- Execptions are used to handle expected errors at runtime\n", | |
| "\n", | |
| "Throwing an Assertion" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 96, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "AssertionError", | |
| "evalue": "3 must be equal to 2", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[31m---------------------------------------------------------------------------\u001b[39m", | |
| "\u001b[31mAssertionError\u001b[39m Traceback (most recent call last)", | |
| "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[96]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m \u001b[32m3\u001b[39m == \u001b[32m2\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33m3 must be equal to 2\u001b[39m\u001b[33m\"\u001b[39m\n", | |
| "\u001b[31mAssertionError\u001b[39m: 3 must be equal to 2" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "assert 3 == 2, \"3 must be equal to 2\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Throwing an Exception" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 97, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "Exception", | |
| "evalue": "Sorry for being an exception!", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[31m---------------------------------------------------------------------------\u001b[39m", | |
| "\u001b[31mException\u001b[39m Traceback (most recent call last)", | |
| "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[97]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mSorry for being an exception!\u001b[39m\u001b[33m\"\u001b[39m)\n", | |
| "\u001b[31mException\u001b[39m: Sorry for being an exception!" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "raise Exception(\"Sorry for being an exception!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 98, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "Sorry, wrong type!", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[31m---------------------------------------------------------------------------\u001b[39m", | |
| "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", | |
| "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[98]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m x = \u001b[32m3.0\u001b[39m\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mint\u001b[39m):\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33mSorry, wrong type!\u001b[39m\u001b[33m\"\u001b[39m)\n", | |
| "\u001b[31mTypeError\u001b[39m: Sorry, wrong type!" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = 3.0\n", | |
| "if not isinstance(x, int):\n", | |
| " raise TypeError(\"Sorry, wrong type!\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Handle Exceptions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 99, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "An exception occurred\n", | |
| "continue code execution\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "try:\n", | |
| " print(some_variable_that_does_not_exist)\n", | |
| "except:\n", | |
| " print(\"An exception occurred\")\n", | |
| "print('continue code execution')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 100, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "<class 'NameError'>\n", | |
| "<built-in method with_traceback of NameError object at 0x7349041f4ee0>\n", | |
| "(\"name 'some_variable_that_does_not_exist' is not defined\",)\n", | |
| "3\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "try:\n", | |
| " i = 3\n", | |
| " i = some_variable_that_does_not_exist\n", | |
| "except (TypeError, ValueError):\n", | |
| " pass\n", | |
| "except Exception as e:\n", | |
| " print(type(e))\n", | |
| " print(e.with_traceback)\n", | |
| " print(e.args)\n", | |
| "finally:\n", | |
| " print(i)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "List of built-in exceptions \n", | |
| "https://docs.python.org/3/library/exceptions.html" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Functions\n", | |
| "Functions make functionality reusable and help to structure code" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 101, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "some string len 6\n", | |
| "some new string len 10\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "10" | |
| ] | |
| }, | |
| "execution_count": 101, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "def some_function(s):\n", | |
| " l = len(s)\n", | |
| " print('some ' + s + ' len ' + str(l))\n", | |
| " return l\n", | |
| "\n", | |
| "l = some_function('string')\n", | |
| "some_function('new string')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Multiple return values" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 102, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1 2 3\n", | |
| "2 2 6\n", | |
| "3 2 c\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tuple" | |
| ] | |
| }, | |
| "execution_count": 102, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "def some_function(a, b, c):\n", | |
| " print(a, b, c)\n", | |
| " return a+1, b, c*2\n", | |
| "\n", | |
| "l = [1,2,3]\n", | |
| "\n", | |
| "a, b, c = some_function(*l)\n", | |
| "print(a, b, c)\n", | |
| "\n", | |
| "l = some_function(3, 2, 'c')\n", | |
| "type(l)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Variable number of arguments\n", | |
| "- Arguments can be named or unnamed\n", | |
| "- Named arguments can have default values" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 103, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "x: 33\n", | |
| "y: 5\n", | |
| "args: (6, 'abc')\n", | |
| "kwargs: {'foo': 24, 'bar': 'xyz'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "def some_function(x, y=3, *args, **kwargs):\n", | |
| " print('x:', x)\n", | |
| " print('y:', y)\n", | |
| " print('args:', args)\n", | |
| " print('kwargs:', kwargs)\n", | |
| "\n", | |
| "some_function(33, 5, 6, 'abc', foo=24, bar='xyz')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 104, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "x: 33\n", | |
| "y: 3\n", | |
| "args: ()\n", | |
| "kwargs: {'foo': 5}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "some_function(33, 3, foo=5)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Lambda Functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 105, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "3" | |
| ] | |
| }, | |
| "execution_count": 105, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "f = lambda x,y: x+y\n", | |
| "f(1,2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Iterators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Range\n", | |
| "\n", | |
| "Returns an iterator over a sequence of numbers from start to stop with a certain step\n", | |
| "\n", | |
| "- If the start is ommited, the iterator starts with 0\n", | |
| "- If the step is ommited, the step is 1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 106, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
| ] | |
| }, | |
| "execution_count": 106, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( range(10) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 107, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[10, 8, 6, 4, 2]" | |
| ] | |
| }, | |
| "execution_count": 107, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( range(10, 0, -2) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Generators\n", | |
| "\n", | |
| "Generates one item at a time\n", | |
| "\n", | |
| "- Generators can be used to load only the data that is needed at a certain time instead of keeping large lists of objects in memory\n", | |
| "- The <code>yield</code> keyword is used to return the values inside a function" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 108, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def generator():\n", | |
| " for i in range(5):\n", | |
| " yield i*i\n", | |
| "\n", | |
| "g = generator()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 109, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "generator" | |
| ] | |
| }, | |
| "execution_count": 109, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(g)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 110, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0" | |
| ] | |
| }, | |
| "execution_count": 110, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "next(g)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 111, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "1" | |
| ] | |
| }, | |
| "execution_count": 111, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "next(g)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 112, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0 1 4 9 16 " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "g = generator()\n", | |
| "\n", | |
| "for i in g:\n", | |
| " print(i, end=' ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Generator Expressions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 113, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "generator" | |
| ] | |
| }, | |
| "execution_count": 113, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "l = ['aa', 'bb', 'cc']\n", | |
| "\n", | |
| "g = ( s.upper() for s in l )\n", | |
| "\n", | |
| "type(g)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 114, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "AA BB CC " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for i in g:\n", | |
| " print(i, end=' ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Enumerate\n", | |
| "\n", | |
| "Iterates over elements and their corresponding indices" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 115, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[(0, 'abc'), (1, 'def'), (2, 'ghi')]" | |
| ] | |
| }, | |
| "execution_count": 115, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( enumerate(['abc', 'def', 'ghi']) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 116, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0 abc\n", | |
| "1 def\n", | |
| "2 ghi\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "l = ['abc', 'def', 'ghi']\n", | |
| "for i, e in enumerate(l):\n", | |
| " print(i, e)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Zip\n", | |
| "\n", | |
| "Takes iterables (can be zero or more), aggregates them in a tuple" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 117, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[(1, 4), (2, 5), (3, 6)]" | |
| ] | |
| }, | |
| "execution_count": 117, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( zip([1,2,3], [4,5,6]) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Reversed\n", | |
| "\n", | |
| "Iterates in reverse order" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 118, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[16, 8, 3]" | |
| ] | |
| }, | |
| "execution_count": 118, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( reversed([3,8,16]) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Map\n", | |
| "\n", | |
| "Applies a given function to each item of an iterable (list, tuple etc.)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 119, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 4, 9]" | |
| ] | |
| }, | |
| "execution_count": 119, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( map(lambda x: x**2, [1,2,3]) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Filter\n", | |
| "\n", | |
| "Constructs an iterator from elements of an iterable for which a function returns True" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 120, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[2, 4]" | |
| ] | |
| }, | |
| "execution_count": 120, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( filter(lambda x: x%2==0, [1,2,3,4]) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Classes and Objects\n", | |
| "\n", | |
| "Practically everything in Python is an object" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Instances\n", | |
| "\n", | |
| "Classes define the blueprint; Instances are the concrete objects created from that blueprint" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 121, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "class SomeClass:\n", | |
| " pass\n", | |
| "\n", | |
| "some_instance = SomeClass()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 122, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "__main__.SomeClass" | |
| ] | |
| }, | |
| "execution_count": 122, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(some_instance)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Attributes\n", | |
| "\n", | |
| "Attributes are variables which hold data belonging to an instance of a class" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 123, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "3\n", | |
| "{'some_other_property': 3}\n", | |
| "{'some_other_property': 3, 'some_property': 1}\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['__class__',\n", | |
| " '__delattr__',\n", | |
| " '__dict__',\n", | |
| " '__dir__',\n", | |
| " '__doc__',\n", | |
| " '__eq__',\n", | |
| " '__format__',\n", | |
| " '__ge__',\n", | |
| " '__getattribute__',\n", | |
| " '__getstate__',\n", | |
| " '__gt__',\n", | |
| " '__hash__',\n", | |
| " '__init__',\n", | |
| " '__init_subclass__',\n", | |
| " '__le__',\n", | |
| " '__lt__',\n", | |
| " '__module__',\n", | |
| " '__ne__',\n", | |
| " '__new__',\n", | |
| " '__reduce__',\n", | |
| " '__reduce_ex__',\n", | |
| " '__repr__',\n", | |
| " '__setattr__',\n", | |
| " '__sizeof__',\n", | |
| " '__str__',\n", | |
| " '__subclasshook__',\n", | |
| " '__weakref__',\n", | |
| " 'some_other_property',\n", | |
| " 'some_property']" | |
| ] | |
| }, | |
| "execution_count": 123, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "class SomeClass:\n", | |
| " some_property = 1\n", | |
| " \n", | |
| "some_instance = SomeClass()\n", | |
| "\n", | |
| "some_instance.some_other_property = 3\n", | |
| "\n", | |
| "print(some_instance.some_property)\n", | |
| "print(some_instance.some_other_property)\n", | |
| "\n", | |
| "print(some_instance.__dict__)\n", | |
| "some_instance.some_property = 1\n", | |
| "print(some_instance.__dict__)\n", | |
| "\n", | |
| "dir(some_instance)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Methods\n", | |
| "\n", | |
| "Methods are functions defined within a class that operate on instances\n", | |
| "\n", | |
| "- The first parameter <code>self</code> passed to a method is the reference to the current instance\n", | |
| "- The <code>self</code> parameter can be used to access the attributes and methods of the instance" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 124, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0\n", | |
| "value: text\n", | |
| "value: more text\n", | |
| "2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "class SomeClass:\n", | |
| " def __init__(self, x):\n", | |
| " self.x = x\n", | |
| " \n", | |
| " def some_other_method(self, v):\n", | |
| " self.x += 1\n", | |
| " print('value: ' + v)\n", | |
| " \n", | |
| " def some_method(self, v):\n", | |
| " self.v = v\n", | |
| " self.some_other_method(v)\n", | |
| " \n", | |
| "some_instance = SomeClass(0)\n", | |
| "\n", | |
| "print(some_instance.x)\n", | |
| "some_instance.some_method('text')\n", | |
| "some_instance.some_method('more text')\n", | |
| "print(some_instance.x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Inheritance\n", | |
| "\n", | |
| "- The parent or base class is the class being inherited from\n", | |
| "- The child or derived class is the class that inherits form another class\n", | |
| "- Child classes inherit the attributes and methods from the parent\n", | |
| "- The <code>super</code> function can be used to call methods from the parent class that get overwritten by a child class" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 125, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "parent value: text1\n", | |
| "child value: text2\n", | |
| "2\n", | |
| "10\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "class ParentClass:\n", | |
| " def __init__(self, y):\n", | |
| " self.y = y\n", | |
| " \n", | |
| " def some_parent_method(self, v):\n", | |
| " print('parent value: ' + v)\n", | |
| "\n", | |
| "class ChildClass(ParentClass):\n", | |
| " def __init__(self, x):\n", | |
| " self.x = x\n", | |
| " super().__init__(x+1) \n", | |
| " \n", | |
| " def some_child_method(self, v):\n", | |
| " print('child value: ' + v)\n", | |
| " self.y = 10\n", | |
| " \n", | |
| "some_instance = ChildClass(2)\n", | |
| "\n", | |
| "some_instance.some_parent_method('text1')\n", | |
| "some_instance.some_child_method('text2')\n", | |
| "print(some_instance.x)\n", | |
| "print(some_instance.y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "some_instance." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "- Jupyter Notebook has autocompletion to show the properties and methods of an object\n", | |
| "- <code>some_instance.<tab></code> in this example" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Scopes\n", | |
| "\n", | |
| "- A local scope is created inside functions or methods\n", | |
| "- Local variables are created inside functions and methods\n", | |
| "- Global variables are created outsid of functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 127, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "global x 4\n", | |
| "local x function1 3\n", | |
| "local x function2 2\n", | |
| "local x function1 3\n", | |
| "global x 4\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = 4\n", | |
| "\n", | |
| "def function1():\n", | |
| " x = 3\n", | |
| " \n", | |
| " def function2():\n", | |
| " x = 2\n", | |
| " print('local x function2 %i' % x)\n", | |
| " \n", | |
| " print('local x function1 %i' % x)\n", | |
| " function2()\n", | |
| " print('local x function1 %i' % x)\n", | |
| "\n", | |
| "\n", | |
| "print('global x %i' % x)\n", | |
| "function1()\n", | |
| "print('global x %i' % x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "- Global variables can be accessed via the <code>global</code> keyword\n", | |
| "- Global and local variables can also be accessed via the <code>globals</code> and <code>locals</code> function\n", | |
| "- The <code>vars</code> function can be used for objects" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 128, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "local x 3\n", | |
| "local y 6\n", | |
| "locals x 3\n", | |
| "globals x 4\n", | |
| "global x 4\n", | |
| "global y 6\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "x = 4\n", | |
| "y = 5\n", | |
| "\n", | |
| "def function():\n", | |
| " x = 3\n", | |
| " global y\n", | |
| " y = 6\n", | |
| " print('local x %i' % x )\n", | |
| " print('local y %i' % y )\n", | |
| " print('locals x', locals()['x'])\n", | |
| " print('globals x', globals()['x'])\n", | |
| " \n", | |
| "function()\n", | |
| "print('global x %i' % x)\n", | |
| "print('global y %i' % y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## File Handling\n", | |
| "\n", | |
| "Files can be opend in different modes\n", | |
| "\n", | |
| "| Mode | Name | Comment |\n", | |
| "| :--- | :--- | :--- |\n", | |
| "| r | Read | Returns an error if file does not exist |\n", | |
| "| w | Write | Creates the file if it does not exist |\n", | |
| "| a | Append | Creates the file if it does not exist |\n", | |
| "| x | Create | Returns an error if file exist |\n", | |
| "\n", | |
| "Files can be handled in binary or text mode\n", | |
| "\n", | |
| "| Mode | Name |\n", | |
| "| :--- | :--- |\n", | |
| "| t | Text |\n", | |
| "| b | Binary |" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Write File" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 129, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "f = open('textfile.txt', 'w')\n", | |
| "f.write('file has some \\n')\n", | |
| "f.write('content!')\n", | |
| "f.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Read File" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 130, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "file has some \n", | |
| "content!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# entire file\n", | |
| "f = open('textfile.txt', 'rt')\n", | |
| "print( f.read() )\n", | |
| "f.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 131, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "file has some \n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# by line\n", | |
| "f = open('textfile.txt', 'rt')\n", | |
| "print( f.readline() )\n", | |
| "f.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 132, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "file has some \n", | |
| "\n", | |
| "content!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "f = open('textfile.txt', 'rt')\n", | |
| "for x in f:\n", | |
| " print(x)\n", | |
| "f.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 133, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "file \n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# by number of characters\n", | |
| "f = open('textfile.txt', 'rt')\n", | |
| "print( f.read(5) )\n", | |
| "f.close()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "The <code>open</code> function can also be used with the <code>with</code> statement as a context manager " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 134, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "file has some \n", | |
| "content!\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "with open('textfile.txt', 'rt', encoding='utf-8') as f:\n", | |
| " content = f.read()\n", | |
| "\n", | |
| "print(content)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 135, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "!rm textfile*.txt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## String Manipulation\n", | |
| "\n", | |
| "### String Methods" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 136, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Hello World!'" | |
| ] | |
| }, | |
| "execution_count": 136, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Yello World!'.replace('Y', 'H')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 137, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['Hello', 'World!']" | |
| ] | |
| }, | |
| "execution_count": 137, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.split(' ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 138, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Hello World!'" | |
| ] | |
| }, | |
| "execution_count": 138, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "' '.join(['Hello', 'World!'])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 139, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'hello world!'" | |
| ] | |
| }, | |
| "execution_count": 139, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.lower()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 140, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'HELLO WORLD!'" | |
| ] | |
| }, | |
| "execution_count": 140, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.upper()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 141, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Hello World!'" | |
| ] | |
| }, | |
| "execution_count": 141, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "' Hello World! '.strip()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 142, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'Hello World! '" | |
| ] | |
| }, | |
| "execution_count": 142, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.ljust(16)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 143, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "' Hello World!'" | |
| ] | |
| }, | |
| "execution_count": 143, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.rjust(16)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 144, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'00015'" | |
| ] | |
| }, | |
| "execution_count": 144, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'15'.zfill(5)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 145, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 145, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.startswith('Hell')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 146, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "execution_count": 146, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.endswith('Hell')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 147, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "7" | |
| ] | |
| }, | |
| "execution_count": 147, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'Hello World!'.find('orld')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 148, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(False, True, True)" | |
| ] | |
| }, | |
| "execution_count": 148, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s = '33'\n", | |
| "s.isalpha(), s.isnumeric(), s.isalnum()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Format Strings\n", | |
| "\n", | |
| "Old style, like printf in C" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 149, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'some text 08.33'" | |
| ] | |
| }, | |
| "execution_count": 149, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'some %-8s %05.2f' % ('text', 8.3333333)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "New style, more object oriented" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 150, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'some text 08.33'" | |
| ] | |
| }, | |
| "execution_count": 150, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'some {:8s} {:05.2f}'.format('text', 8.3333333)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 151, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'|left | center | right|'" | |
| ] | |
| }, | |
| "execution_count": 151, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "'|{:<10s}|{:^10s}|{:>10s}|'.format('left', 'center', 'right')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Formatted String Literals, since Python 3.6" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 152, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'text 16.6666666'" | |
| ] | |
| }, | |
| "execution_count": 152, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "s1, f1 = 'text', 8.3333333\n", | |
| "f'{s1} {f1*2}'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 153, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'some text 08.33'" | |
| ] | |
| }, | |
| "execution_count": 153, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "f'some {s1:8s} {f1:05.2f}'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 154, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'error 1e5b8dcc54'" | |
| ] | |
| }, | |
| "execution_count": 154, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "error = 130385038420\n", | |
| "f'error {error:x}'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Debugging Specifier, since Python 3.8" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 155, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "s1='text'\n", | |
| "f1=8.3333333\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(f'{s1=}\\n{f1=}') # instead of print(f's1={s1}\\nf1={f1}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "https://docs.python.org/3/library/string.html#string-formatting" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Modules" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 156, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import os\n", | |
| "import os, sys\n", | |
| "from os import path\n", | |
| "import os.path as p\n", | |
| "from os import path as p" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 157, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0]'" | |
| ] | |
| }, | |
| "execution_count": 157, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sys.version" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 158, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['/usr/lib/python312.zip',\n", | |
| " '/usr/lib/python3.12',\n", | |
| " '/usr/lib/python3.12/lib-dynload',\n", | |
| " '',\n", | |
| " '/home/mvoe/venv/lib/python3.12/site-packages']" | |
| ] | |
| }, | |
| "execution_count": 158, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sys.path" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 159, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'/bin/bash'" | |
| ] | |
| }, | |
| "execution_count": 159, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "os.getenv('SHELL')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Standard Library\n", | |
| "\n", | |
| "A complete list and documentation of the Python Standard Library can be found under\n", | |
| "\n", | |
| "https://docs.python.org/3/library/" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Dates" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 160, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "1.0004639625549316" | |
| ] | |
| }, | |
| "execution_count": 160, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import time\n", | |
| "\n", | |
| "t1 = time.time()\n", | |
| "time.sleep(1)\n", | |
| "t2 = time.time()\n", | |
| "\n", | |
| "t2 - t1" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 161, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "2026-04-26 11:49:09.437056\n", | |
| "2026 49\n", | |
| "Sunday 26 04 2026\n", | |
| "2020-05-17 08:20:05\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import datetime\n", | |
| "\n", | |
| "d = datetime.datetime.now()\n", | |
| "\n", | |
| "print(d)\n", | |
| "print(d.year, d.minute)\n", | |
| "print(d.strftime('%A %d %m %Y'))\n", | |
| "\n", | |
| "d = datetime.datetime(2020, 5, 17, 8, 20, 5)\n", | |
| "\n", | |
| "print(d)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "https://docs.python.org/3/library/datetime.html" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Math" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 162, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "4.0\n", | |
| "2\n", | |
| "1\n", | |
| "3.141592653589793\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import math\n", | |
| "\n", | |
| "print( math.sqrt(16) )\n", | |
| "print( math.ceil(1.6) )\n", | |
| "print( math.floor(1.6) )\n", | |
| "print( math.pi )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Itertools" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 163, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import itertools\n", | |
| "\n", | |
| "l = [1,2,3]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 164, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[(1, 2), (1, 3), (2, 3)]" | |
| ] | |
| }, | |
| "execution_count": 164, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( itertools.combinations(l, 2) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 165, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]" | |
| ] | |
| }, | |
| "execution_count": 165, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( itertools.permutations(l, 2) )" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 166, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]" | |
| ] | |
| }, | |
| "execution_count": 166, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "list( itertools.product(l, repeat=2))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 167, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]" | |
| ] | |
| }, | |
| "execution_count": 167, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "g = itertools.cycle(l)\n", | |
| "[ next(g) for i in range(10) ]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### JSON\n", | |
| "\n", | |
| "JavaScript Object Notation (JSON) is a human-readable format for data-exchange derived from JavaScript" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 168, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'{\"name\": \"John\", \"age\": 32, \"city\": \"New York\"}'" | |
| ] | |
| }, | |
| "execution_count": 168, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import json\n", | |
| "\n", | |
| "s = '{ \"name\":\"John\", \"age\":30, \"city\":\"New York\"}'\n", | |
| "\n", | |
| "d = json.loads(s)\n", | |
| "\n", | |
| "d['age'] = 32\n", | |
| "\n", | |
| "json.dumps(d)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 169, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{\n", | |
| " \"name\": \"John\",\n", | |
| " \"age\": 30,\n", | |
| " \"married\": true,\n", | |
| " \"divorced\": false,\n", | |
| " \"children\": [\n", | |
| " \"Ann\",\n", | |
| " \"Billy\"\n", | |
| " ],\n", | |
| " \"pets\": null,\n", | |
| " \"cars\": [\n", | |
| " {\n", | |
| " \"model\": \"BMW 230\",\n", | |
| " \"mpg\": 27.5\n", | |
| " },\n", | |
| " {\n", | |
| " \"model\": \"Ford Edge\",\n", | |
| " \"mpg\": 24.1\n", | |
| " }\n", | |
| " ]\n", | |
| "}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "d = {\n", | |
| " \"name\": \"John\",\n", | |
| " \"age\": 30,\n", | |
| " \"married\": True,\n", | |
| " \"divorced\": False,\n", | |
| " \"children\": (\"Ann\",\"Billy\"),\n", | |
| " \"pets\": None,\n", | |
| " \"cars\": [\n", | |
| " {\"model\": \"BMW 230\", \"mpg\": 27.5},\n", | |
| " {\"model\": \"Ford Edge\", \"mpg\": 24.1}\n", | |
| " ]\n", | |
| "}\n", | |
| "\n", | |
| "print(json.dumps(d, indent=4))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### RegEx\n", | |
| "\n", | |
| "Regular Expressions (RegEx or RegExp) are a formal description language for describing search patterns used for text manipulation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 170, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "['rai', 'pai']" | |
| ] | |
| }, | |
| "execution_count": 170, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import re\n", | |
| "\n", | |
| "txt = 'The rain in Spain'\n", | |
| "re.findall('.ai', txt)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 173, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "The_rain_in_Spain\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "txt = 'The rain in Spain'\n", | |
| "x = re.sub(r'\\s', '_', txt)\n", | |
| "print(x) " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 175, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "<re.Match object; span=(0, 17), match='The rain in Spain'>" | |
| ] | |
| }, | |
| "execution_count": 175, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "txt = 'The rain in Spain'\n", | |
| "res = re.search(r'^The.*Spain$', txt)\n", | |
| "res" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 176, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "The rain in Spain\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "txt = 'The rain in Spain'\n", | |
| "res = re.search(r'\\bS\\w+', txt)\n", | |
| "print(res.string)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "https://docs.python.org/3/library/re.html" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Threading" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 177, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1777197043.801864 start service\n", | |
| "1777197043.803734 start worker\n", | |
| "1777197044.804604 worker alive True\n", | |
| "1777197045.803960 end worker\n", | |
| "1777197046.802625 end service\n", | |
| "1777197046.803222 worker alive False\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import threading\n", | |
| "import time\n", | |
| " \n", | |
| "def service():\n", | |
| " print('%f start service' % time.time())\n", | |
| " time.sleep(3)\n", | |
| " print('%f end service' % time.time())\n", | |
| "\n", | |
| "def worker():\n", | |
| " print('%f start worker' % time.time())\n", | |
| " time.sleep(2)\n", | |
| " print('%f end worker' % time.time())\n", | |
| "\n", | |
| "t1 = threading.Thread(name='t1', target=service)\n", | |
| "t2 = threading.Thread(name='t2', target=worker)\n", | |
| "\n", | |
| "t1.start()\n", | |
| "t2.start()\n", | |
| "\n", | |
| "\n", | |
| "# check if thread is running\n", | |
| "time.sleep(1)\n", | |
| "print('%f worker alive %s' % (time.time(), t1.is_alive()))\n", | |
| "\n", | |
| "# wait for threads to complete\n", | |
| "t2.join()\n", | |
| "t1.join()\n", | |
| "\n", | |
| "print('%f worker alive %s' % (time.time(), t1.is_alive()))\n", | |
| "\n", | |
| "# in a script, without join, the process will wait until the threads exit, this can be avoided with t1.setDaemon(True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Basic Synchronization using Lock\n", | |
| "\n", | |
| "- Avoides or solves race conditions\n", | |
| "- Does what is also know as Mutex (MUTual EXclusion)\n", | |
| "- Allows only one thread at a time in the read-modify-write section of the code" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 178, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1\n", | |
| "1777197046.827752 start worker\n", | |
| "1777197046.827812 lock acquired via aquire and releas\n", | |
| "1777197046.828221 star worker_with\n", | |
| "1777197047.827997 increment x in worker\n", | |
| "1777197047.828524 end worker\n", | |
| "1777197047.828668 lock acquired via context manager\n", | |
| "1777197047.828801 increment x in worker_with\n", | |
| "1777197047.828854 end worker_with\n", | |
| "4\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "class SomeClass:\n", | |
| " def __init__(self):\n", | |
| " self.x = 1\n", | |
| " self.lock = threading.Lock()\n", | |
| " \n", | |
| " self._print_lock = threading.Lock()\n", | |
| " \n", | |
| " def _print(self, msg):\n", | |
| " with self._print_lock:\n", | |
| " print(msg)\n", | |
| "\n", | |
| " def worker(self, v):\n", | |
| " self._print('%f start worker' % (time.time()))\n", | |
| " self.lock.acquire()\n", | |
| " try:\n", | |
| " self._print('%f lock acquired via aquire and releas' % (time.time()))\n", | |
| " time.sleep(1)\n", | |
| " self._print('%f increment x in worker' % (time.time()))\n", | |
| " self.x += v\n", | |
| " finally:\n", | |
| " self.lock.release()\n", | |
| " self._print('%f end worker' % (time.time()))\n", | |
| "\n", | |
| " def worker_with(self, v):\n", | |
| " self._print('%f star worker_with' % (time.time()))\n", | |
| " with self.lock:\n", | |
| " self._print('%f lock acquired via context manager' % (time.time()))\n", | |
| " self._print('%f increment x in worker_with' % (time.time()))\n", | |
| " self.x += v\n", | |
| " self._print('%f end worker_with' % (time.time()))\n", | |
| " \n", | |
| " def run(self):\n", | |
| " print(self.x)\n", | |
| " \n", | |
| " t1 = threading.Thread(target=self.worker, args=(2,))\n", | |
| " t2 = threading.Thread(target=self.worker_with, args=(1,))\n", | |
| "\n", | |
| " t1.start()\n", | |
| " t2.start()\n", | |
| "\n", | |
| " t1.join()\n", | |
| " t2.join()\n", | |
| " \n", | |
| " print(self.x)\n", | |
| "\n", | |
| "some_instance = SomeClass()\n", | |
| "some_instance.run()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Deadlocks are situation in which a thread waits forever because a lock is never released" | |
| ] | |
| }, | |
| { | |
| "cell_type": "raw", | |
| "metadata": {}, | |
| "source": [ | |
| "l = threading.Lock()\n", | |
| "l.acquire()\n", | |
| "l.acquire() # deadlock, the same thread will never call l.release()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Multiprocessing\n", | |
| "\n", | |
| "- Tasks run on different CPUs (in comparison to threading)\n", | |
| "- The code that is executed gets pickled (serialized)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 179, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1777197052.997683 process ['a', '2']\twaiting 2 seconds1777197052.998061 process ['b', '4']\twaiting 2 seconds\n", | |
| "\n", | |
| "1777197055.005293 process ['a', '2']\tDONE1777197055.006021 process ['b', '4']\tDONE\n", | |
| "\n", | |
| "1777197055.011022 process ['c', '6']\twaiting 2 seconds1777197055.011357 process ['d', '8']\twaiting 2 seconds\n", | |
| "\n", | |
| "1777197057.014329 process ['c', '6']\tDONE1777197057.014633 process ['d', '8']\tDONE\n", | |
| "\n", | |
| "1777197057.019019 process ['e', '1']\twaiting 2 seconds\n", | |
| "1777197057.020462 process ['f', '3']\twaiting 2 seconds\n", | |
| "1777197059.022301 process ['e', '1']\tDONE\n", | |
| "1777197059.025883 process ['f', '3']\tDONE1777197059.026381 process ['g', '5']\twaiting 2 seconds\n", | |
| "\n", | |
| "1777197059.029497 process ['h', '7']\twaiting 2 seconds\n", | |
| "1777197061.030156 process ['g', '5']\tDONE1777197061.032430 process ['h', '7']\tDONE\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "16" | |
| ] | |
| }, | |
| "execution_count": 179, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import multiprocessing\n", | |
| "import time\n", | |
| "\n", | |
| "data = (\n", | |
| " ['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],\n", | |
| " ['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']\n", | |
| ")\n", | |
| "\n", | |
| "def mp_worker(inputs):\n", | |
| " t = 2\n", | |
| " print('%f process %s\\twaiting %s seconds' % (time.time(), inputs, t))\n", | |
| " time.sleep(int(t))\n", | |
| " print('%f process %s\\tDONE' % (time.time(), inputs))\n", | |
| "\n", | |
| "def mp_handler():\n", | |
| " p = multiprocessing.Pool(2)\n", | |
| " p.map(mp_worker, data)\n", | |
| "\n", | |
| "mp_handler()\n", | |
| "\n", | |
| "multiprocessing.cpu_count()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Profiling\n", | |
| "\n", | |
| "A profile is a set of statistics that describes how long and how often certain parts of the code are executed." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 180, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| " 546 function calls (540 primitive calls) in 0.501 seconds\n", | |
| "\n", | |
| " Ordered by: cumulative time\n", | |
| "\n", | |
| " ncalls tottime percall cumtime percall filename:lineno(function)\n", | |
| " 1 0.000 0.000 0.498 0.498 876735232.py:4(do_everything_you_want)\n", | |
| " 5 0.498 0.100 0.498 0.100 {built-in method time.sleep}\n", | |
| " 1 0.000 0.000 0.001 0.001 history.py:1057(writeout_cache)\n", | |
| " 1 0.000 0.000 0.001 0.001 history.py:1041(_writeout_input_cache)\n", | |
| " 2 0.001 0.000 0.001 0.000 {method '__exit__' of 'sqlite3.Connection' objects}\n", | |
| " 1 0.000 0.000 0.001 0.001 base_events.py:1910(_run_once)\n", | |
| " 2 0.000 0.000 0.000 0.000 zmqstream.py:653(_rebuild_io_state)\n", | |
| " 1 0.000 0.000 0.000 0.000 events.py:86(_run)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'run' of '_contextvars.Context' objects}\n", | |
| " 1 0.000 0.000 0.000 0.000 ioloop.py:750(_run_callback)\n", | |
| " 1 0.000 0.000 0.000 0.000 zmqstream.py:684(<lambda>)\n", | |
| " 1 0.000 0.000 0.000 0.000 zmqstream.py:573(_handle_events)\n", | |
| " 2 0.000 0.000 0.000 0.000 zmqstream.py:676(_update_handler)\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:348(<lambda>)\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:350(_really_send)\n", | |
| " 13 0.000 0.000 0.000 0.000 enum.py:1551(__or__)\n", | |
| " 1 0.000 0.000 0.000 0.000 socket.py:700(send_multipart)\n", | |
| " 1 0.000 0.000 0.000 0.000 876735232.py:1(<module>)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", | |
| " 3 0.000 0.000 0.000 0.000 attrsettr.py:43(__getattr__)\n", | |
| " 13 0.000 0.000 0.000 0.000 socket.py:623(send)\n", | |
| " 54 0.000 0.000 0.000 0.000 enum.py:1544(_get_value)\n", | |
| " 2 0.000 0.000 0.000 0.000 codeop.py:121(__call__)\n", | |
| " 114/110 0.000 0.000 0.000 0.000 {built-in method builtins.isinstance}\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method builtins.compile}\n", | |
| " 5 0.000 0.000 0.000 0.000 enum.py:1562(__and__)\n", | |
| " 1 0.000 0.000 0.000 0.000 zmqstream.py:614(_handle_recv)\n", | |
| " 3 0.000 0.000 0.000 0.000 attrsettr.py:66(_get_attr_opt)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:708(__set__)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:3631(set)\n", | |
| " 22 0.000 0.000 0.000 0.000 enum.py:726(__call__)\n", | |
| " 1 0.000 0.000 0.000 0.000 asyncio.py:231(add_callback)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:689(set)\n", | |
| " 1 0.000 0.000 0.000 0.000 decorator.py:200(fix)\n", | |
| " 1 0.000 0.000 0.000 0.000 functools.py:518(decorating_function)\n", | |
| " 1 0.000 0.000 0.000 0.000 socket.py:771(recv_multipart)\n", | |
| " 2 0.000 0.000 0.000 0.000 typing.py:1194(__instancecheck__)\n", | |
| " 1 0.000 0.000 0.000 0.000 base_events.py:785(call_soon)\n", | |
| " 2 0.000 0.000 0.000 0.000 typing.py:1465(__subclasscheck__)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:718(_validate)\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:3237(bind)\n", | |
| " 1 0.000 0.000 0.000 0.000 functools.py:35(update_wrapper)\n", | |
| " 1 0.000 0.000 0.000 0.000 selectors.py:451(select)\n", | |
| " 1 0.000 0.000 0.000 0.000 base_events.py:814(_call_soon)\n", | |
| " 6 0.000 0.000 0.000 0.000 traitlets.py:676(__get__)\n", | |
| " 3 0.000 0.000 0.000 0.000 typing.py:392(inner)\n", | |
| " 13 0.000 0.000 0.000 0.000 {built-in method builtins.next}\n", | |
| " 3 0.000 0.000 0.000 0.000 contextlib.py:299(helper)\n", | |
| " 22 0.000 0.000 0.000 0.000 enum.py:1129(__new__)\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:3102(_bind)\n", | |
| " 16 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}\n", | |
| " 4 0.000 0.000 0.000 0.000 contextlib.py:141(__exit__)\n", | |
| " 1 0.000 0.000 0.000 0.000 zmqstream.py:546(_run_callback)\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method builtins.issubclass}\n", | |
| " 2 0.000 0.000 0.000 0.000 zmqstream.py:532(sending)\n", | |
| " 1 0.000 0.000 0.000 0.000 traitlets.py:1512(_notify_trait)\n", | |
| " 3 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:1390(_handle_fromlist)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'execute' of 'sqlite3.Connection' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:3474(validate)\n", | |
| " 2 0.000 0.000 0.000 0.000 queue.py:97(empty)\n", | |
| " 3 0.000 0.000 0.000 0.000 contextlib.py:132(__enter__)\n", | |
| " 1 0.000 0.000 0.000 0.000 traitlets.py:1523(notify_change)\n", | |
| " 3 0.000 0.000 0.000 0.000 contextlib.py:104(__init__)\n", | |
| " 6 0.000 0.000 0.000 0.000 traitlets.py:629(get)\n", | |
| " 2 0.000 0.000 0.000 0.000 typing.py:1258(__hash__)\n", | |
| " 1 0.000 0.000 0.000 0.000 functools.py:479(lru_cache)\n", | |
| " 1 0.000 0.000 0.000 0.000 events.py:36(__init__)\n", | |
| " 2 0.000 0.000 0.000 0.000 <frozen abc>:121(__subclasscheck__)\n", | |
| " 1 0.000 0.000 0.000 0.000 traitlets.py:1527(_notify_observers)\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:288(_check_mp_mode)\n", | |
| " 1 0.000 0.000 0.000 0.000 threading.py:637(wait)\n", | |
| " 8 0.000 0.000 0.000 0.000 {built-in method builtins.hasattr}\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:2918(apply_defaults)\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:285(_is_master_process)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:727(_cross_validate)\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:3624(validate_elements)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'poll' of 'select.epoll' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 base_events.py:734(time)\n", | |
| " 3 0.000 0.000 0.000 0.000 {built-in method builtins.max}\n", | |
| " 2 0.000 0.000 0.000 0.000 queue.py:209(_qsize)\n", | |
| " 4 0.000 0.000 0.000 0.000 compilerop.py:180(extra_flags)\n", | |
| " 10 0.000 0.000 0.000 0.000 {built-in method builtins.len}\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:229(_handle_event)\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:2865(args)\n", | |
| " 3 0.000 0.000 0.000 0.000 threading.py:299(__enter__)\n", | |
| " 1 0.000 0.000 0.000 0.000 history.py:1049(_writeout_output_cache)\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method builtins.setattr}\n", | |
| " 23 0.000 0.000 0.000 0.000 typing.py:2154(cast)\n", | |
| " 3 0.000 0.000 0.000 0.000 {method 'upper' of 'str' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method _abc._abc_subclasscheck}\n", | |
| " 6 0.000 0.000 0.000 0.000 {method 'get' of 'dict' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:2304(validate)\n", | |
| " 6 0.000 0.000 0.000 0.000 {method '__exit__' of '_thread.lock' objects}\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'values' of 'mappingproxy' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 threading.py:302(__exit__)\n", | |
| " 1 0.000 0.000 0.000 0.000 threading.py:627(clear)\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:2888(kwargs)\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method posix.getpid}\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method builtins.hash}\n", | |
| " 1 0.000 0.000 0.000 0.000 threading.py:314(_is_owned)\n", | |
| " 2 0.000 0.000 0.000 0.000 interactiveshell.py:3663(compare)\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method _asyncio.get_running_loop}\n", | |
| " 1 0.000 0.000 0.000 0.000 threading.py:308(_release_save)\n", | |
| " 2 0.000 0.000 0.000 0.000 {method 'append' of 'collections.deque' objects}\n", | |
| " 3 0.000 0.000 0.000 0.000 {method '__enter__' of '_thread.lock' objects}\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method builtins.min}\n", | |
| " 2 0.000 0.000 0.000 0.000 {method 'popleft' of 'collections.deque' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 traitlets.py:3486(validate_elements)\n", | |
| " 2 0.000 0.000 0.000 0.000 interactiveshell.py:1322(user_global_ns)\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method time.monotonic}\n", | |
| " 3 0.000 0.000 0.000 0.000 history.py:1102(hold)\n", | |
| " 10 0.000 0.000 0.000 0.000 inspect.py:2777(kind)\n", | |
| " 3 0.000 0.000 0.000 0.000 zmqstream.py:528(receiving)\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method _contextvars.copy_context}\n", | |
| " 3 0.000 0.000 0.000 0.000 {method 'items' of 'mappingproxy' objects}\n", | |
| " 4 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method math.ceil}\n", | |
| " 1 0.000 0.000 0.000 0.000 {built-in method _thread.allocate_lock}\n", | |
| " 2 0.000 0.000 0.000 0.000 {method 'acquire' of '_thread.lock' objects}\n", | |
| " 1 0.000 0.000 0.000 0.000 base_events.py:539(_check_closed)\n", | |
| " 1 0.000 0.000 0.000 0.000 base_events.py:2005(get_debug)\n", | |
| " 2 0.000 0.000 0.000 0.000 interactiveshell.py:696(user_ns)\n", | |
| " 2 0.000 0.000 0.000 0.000 {method 'extend' of 'list' objects}\n", | |
| " 4 0.000 0.000 0.000 0.000 inspect.py:3058(parameters)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'update' of 'dict' objects}\n", | |
| " 2 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n", | |
| " 1 0.000 0.000 0.000 0.000 iostream.py:327(closed)\n", | |
| " 1 0.000 0.000 0.000 0.000 selector_events.py:750(_process_events)\n", | |
| " 1 0.000 0.000 0.000 0.000 {method 'release' of '_thread.lock' objects}\n", | |
| " 4 0.000 0.000 0.000 0.000 inspect.py:2765(name)\n", | |
| " 1 0.000 0.000 0.000 0.000 inspect.py:2857(__init__)\n", | |
| " 1/0 0.000 0.000 0.000 {built-in method builtins.exec}\n", | |
| " 1/0 0.000 0.000 0.000 interactiveshell.py:3711(run_code)\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import cProfile\n", | |
| "import time\n", | |
| "\n", | |
| "def do_everything_you_want():\n", | |
| " for i in range(5):\n", | |
| " k = i*3\n", | |
| " time.sleep(0.1)\n", | |
| "\n", | |
| "p = cProfile.Profile()\n", | |
| "p.enable()\n", | |
| "\n", | |
| "do_everything_you_want()\n", | |
| "\n", | |
| "p.disable()\n", | |
| "\n", | |
| "#p.print_stats(sort='time')\n", | |
| "p.print_stats(sort='cumulative')\n", | |
| "#p.print_stats(sort='name')\n", | |
| "\n", | |
| "# from command line\n", | |
| "# python -m cProfile script.py" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Measuring execution time" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 181, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0.003798053599894047" | |
| ] | |
| }, | |
| "execution_count": 181, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import timeit\n", | |
| "\n", | |
| "timeit.timeit('for i in range(1000): a = i*2', number=20)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Measuring execution time in Jupyter Notebook" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 182, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CPU times: user 330 μs, sys: 72 μs, total: 402 μs\n", | |
| "Wall time: 442 μs\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%time for i in range(1000): a = i*2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 183, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "45.2 μs ± 315 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%timeit\n", | |
| "for i in range(1000): a = i*2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# PIP\n", | |
| "\n", | |
| "PIP is a package manager for Python modules" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 184, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\n", | |
| "Usage: \n", | |
| " pip <command> [options]\n", | |
| "\n", | |
| "Commands:\n", | |
| " install Install packages.\n", | |
| " download Download packages.\n", | |
| " uninstall Uninstall packages.\n", | |
| " freeze Output installed packages in requirements format.\n", | |
| " inspect Inspect the python environment.\n", | |
| " list List installed packages.\n", | |
| " show Show information about installed packages.\n", | |
| " check Verify installed packages have compatible dependencies.\n", | |
| " config Manage local and global configuration.\n", | |
| " search Search PyPI for packages.\n", | |
| " cache Inspect and manage pip's wheel cache.\n", | |
| " index Inspect information available from package indexes.\n", | |
| " wheel Build wheels from your requirements.\n", | |
| " hash Compute hashes of package archives.\n", | |
| " completion A helper command used for command completion.\n", | |
| " debug Show information useful for debugging.\n", | |
| " help Show help for commands.\n", | |
| "\n", | |
| "General Options:\n", | |
| " -h, --help Show help.\n", | |
| " --debug Let unhandled exceptions propagate outside the\n", | |
| " main subroutine, instead of logging them to\n", | |
| " stderr.\n", | |
| " --isolated Run pip in an isolated mode, ignoring\n", | |
| " environment variables and user configuration.\n", | |
| " --require-virtualenv Allow pip to only run in a virtual environment;\n", | |
| " exit with an error otherwise.\n", | |
| " --python <python> Run pip with the specified Python interpreter.\n", | |
| " -v, --verbose Give more output. Option is additive, and can be\n", | |
| " used up to 3 times.\n", | |
| " -V, --version Show version and exit.\n", | |
| " -q, --quiet Give less output. Option is additive, and can be\n", | |
| " used up to 3 times (corresponding to WARNING,\n", | |
| " ERROR, and CRITICAL logging levels).\n", | |
| " --log <path> Path to a verbose appending log.\n", | |
| " --no-input Disable prompting for input.\n", | |
| " --keyring-provider <keyring_provider>\n", | |
| " Enable the credential lookup via the keyring\n", | |
| " library if user input is allowed. Specify which\n", | |
| " mechanism to use [disabled, import, subprocess].\n", | |
| " (default: disabled)\n", | |
| " --proxy <proxy> Specify a proxy in the form\n", | |
| " scheme://[user:passwd@]proxy.server:port.\n", | |
| " --retries <retries> Maximum number of retries each connection should\n", | |
| " attempt (default 5 times).\n", | |
| " --timeout <sec> Set the socket timeout (default 15 seconds).\n", | |
| " --exists-action <action> Default action when a path already exists:\n", | |
| " (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.\n", | |
| " --trusted-host <hostname> Mark this host or host:port pair as trusted,\n", | |
| " even though it does not have valid or any HTTPS.\n", | |
| " --cert <path> Path to PEM-encoded CA certificate bundle. If\n", | |
| " provided, overrides the default. See 'SSL\n", | |
| " Certificate Verification' in pip documentation\n", | |
| " for more information.\n", | |
| " --client-cert <path> Path to SSL client certificate, a single file\n", | |
| " containing the private key and the certificate\n", | |
| " in PEM format.\n", | |
| " --cache-dir <dir> Store the cache data in <dir>.\n", | |
| " --no-cache-dir Disable the cache.\n", | |
| " --disable-pip-version-check\n", | |
| " Don't periodically check PyPI to determine\n", | |
| " whether a new version of pip is available for\n", | |
| " download. Implied with --no-index.\n", | |
| " --no-color Suppress colored output.\n", | |
| " --no-python-version-warning\n", | |
| " Silence deprecation warnings for upcoming\n", | |
| " unsupported Pythons.\n", | |
| " --use-feature <feature> Enable new functionality, that may be backward\n", | |
| " incompatible.\n", | |
| " --use-deprecated <feature> Enable deprecated functionality, that will be\n", | |
| " removed in the future.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!pip --help" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 185, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Requirement already satisfied: numpy in /home/mvoe/venv/lib/python3.12/site-packages (2.4.4)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "!pip install -U numpy" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 186, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'2.4.4'" | |
| ] | |
| }, | |
| "execution_count": 186, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import numpy as np\n", | |
| "\n", | |
| "np.__version__" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.14.4" | |
| }, | |
| "toc": { | |
| "base_numbering": 1, | |
| "nav_menu": {}, | |
| "number_sections": true, | |
| "sideBar": true, | |
| "skip_h1_title": false, | |
| "title_cell": "Table of Contents", | |
| "title_sidebar": "Contents", | |
| "toc_cell": false, | |
| "toc_position": { | |
| "height": "calc(100% - 180px)", | |
| "left": "10px", | |
| "top": "150px", | |
| "width": "264.2px" | |
| }, | |
| "toc_section_display": true, | |
| "toc_window_display": true | |
| }, | |
| "varInspector": { | |
| "cols": { | |
| "lenName": 16, | |
| "lenType": 16, | |
| "lenVar": 40 | |
| }, | |
| "kernels_config": { | |
| "python": { | |
| "delete_cmd_postfix": "", | |
| "delete_cmd_prefix": "del ", | |
| "library": "var_list.py", | |
| "varRefreshCmd": "print(var_dic_list())" | |
| }, | |
| "r": { | |
| "delete_cmd_postfix": ") ", | |
| "delete_cmd_prefix": "rm(", | |
| "library": "var_list.r", | |
| "varRefreshCmd": "cat(var_dic_list()) " | |
| } | |
| }, | |
| "oldHeight": 353.85, | |
| "position": { | |
| "height": "633.85px", | |
| "left": "937px", | |
| "right": "20px", | |
| "top": "173px", | |
| "width": "546px" | |
| }, | |
| "types_to_exclude": [ | |
| "module", | |
| "function", | |
| "builtin_function_or_method", | |
| "instance", | |
| "_Feature" | |
| ], | |
| "varInspector_section_display": "block", | |
| "window_display": false | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment