{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Advanced" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.9.2 (default, Feb 20 2021, 00:00:00) \\n[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)]'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "nameerror bei unbekannten variablen" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'a' is not defined\n" ] } ], "source": [ "try:\n", " print(a)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "lookup_table = {\n", " 1: 'one',\n", " 2: 'two',\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(lookup_table)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "try:\n", " lookup_table[9]\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'two'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table[2]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "scope = {\n", " 'a': 1,\n", " 'b': 'hallo',\n", " '12345': 3.12,\n", " 3: 'drei',\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 'hallo', '12345': 3.12, 3: 'drei'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope['a']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope['b']" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.12" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope['12345']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'drei'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scope[3]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 'hallo', '12345': 3.12, 3: 'drei'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()['scope']" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "del scope['b']" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 1, '12345': 3.12, 3: 'drei'}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()['scope']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "now for something completely different" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Types Recap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sequential Datatypes" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "liste = [1, 'zwei', 3.0]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste[2]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list index out of range\n" ] } ], "source": [ "try:\n", " liste[3]\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "noch_eine_liste = ['noch', 'was']" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, 'noch', 'was']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste = liste + noch_eine_liste\n", "gesamte_liste" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "gesamte_liste.append('das vorläufig letze element')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, 'noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,\n", " 'zwei',\n", " 3.0,\n", " 'noch',\n", " 'was',\n", " 'das vorläufig letze element',\n", " 1,\n", " 'zwei',\n", " 3.0,\n", " 'noch',\n", " 'was',\n", " 'das vorläufig letze element']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste*2" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' blah'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indentation = 2\n", "text = 'blah'\n", "indented_text = 2*' '+text\n", "indented_text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Slicing etc." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, 'noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3.0, 'noch']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[2:4]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(gesamte_liste)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list index out of range\n" ] } ], "source": [ "try:\n", " gesamte_liste[6]\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das vorläufig letze element'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[len(gesamte_liste)-1] # das letzte element der liste" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das vorläufig letze element'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[-1]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[3:len(gesamte_liste)]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[3:]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste[:3]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, 'noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3.0, 'noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del gesamte_liste[1]\n", "gesamte_liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mapping Types (Dictionary, und mehr)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "lookup_table[3] = 'three'" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two', 3: 'three'}" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'three'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table[3]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "del lookup_table[3]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_table # weg is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "nebenbei: Variablen" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eine_zum_sterben_verurteilte_variable = 666\n", "eine_zum_sterben_verurteilte_variable" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'eine_zum_sterben_verurteilte_variable' is not defined\n" ] } ], "source": [ "del eine_zum_sterben_verurteilte_variable\n", "try:\n", " eine_zum_sterben_verurteilte_variable\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sets" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "ein_set = {'eins', 2, 3}" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3, 'eins', 'vier'}" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ein_set.add('vier')\n", "ein_set" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "ein_set.remove('eins')" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eins' in ein_set" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in ein_set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nebenbei: ``in`` geht auch in Sequenzen, **aber grottenlangsam**" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3.0, 'noch', 'was', 'das vorläufig letze element']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamte_liste" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'was' in gesamte_liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``for`` Schleife, ganz kurz" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3.0\n", "noch\n", "was\n", "das vorläufig letze element\n" ] } ], "source": [ "for element in gesamte_liste:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mit ``while`` geht das auch, nur sehr potschert:" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3.0\n", "noch\n", "was\n", "das vorläufig letze element\n" ] } ], "source": [ "i = 0\n", "while i < len(gesamte_liste):\n", " print(gesamte_liste[i])\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "t = (1)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not subscriptable\n" ] } ], "source": [ "try:\n", " t[0]\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and (False or True)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "t = (1,)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Funktionen mit mehreren Returnwerten (bzw. Tuple Unpacking)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "def multiple_return_values():\n", " return 1, 'eins'" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 eins\n" ] } ], "source": [ "a, b = multiple_return_values()\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "rv = multiple_return_values()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(rv)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "a, b = rv" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "too many values to unpack (expected 1)\n" ] } ], "source": [ "try:\n", " a, = rv\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ist das gleiche wie:" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "def multiple_return_values():\n", " return (1, 'eins')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "a, b = [1, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iteration über Containerdatentypen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Listen" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "l = [1, 2, 3]\n", "for el in l:\n", " print(el)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sets" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "s = {1, 2, 3}\n", "for el in s:\n", " print(el)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über Keys" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "for k in d:\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über Values" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über beides" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n" ] } ], "source": [ "for xyz in d.items():\n", " print(xyz)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one, value: 1\n", "key: two, value: 2\n" ] } ], "source": [ "for k, v in d.items():\n", " mein_formatierter_string = 'key: '\n", " mein_formatierter_string += k\n", " mein_formatierter_string += ', value: '\n", " mein_formatierter_string += str(v)\n", " print(mein_formatierter_string)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one value: 1\n", "key: two value: 2\n" ] } ], "source": [ "for k, v in d.items():\n", " print('key:', k, 'value:', v)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one, value 1\n", "key: two, value 2\n" ] } ], "source": [ "for k, v in d.items():\n", " print('key: {}, value {}'.format(k, v))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "f-Strings" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one, value 1\n", "key: two, value 2\n" ] } ], "source": [ "for k, v in d.items():\n", " print(f'key: {k}, value {v}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Was heisst \"Untypisiert\"?" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " return a" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1, 2)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'zwei'" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('eins', 'zwei')" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " maximum(1, 'zwei')\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wir löschen den Integer" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(maximum)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "a = maximum" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a(1, 2)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42')" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "int = maximum" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(1,2)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "maximum() missing 1 required positional argument: 'b'\n" ] } ], "source": [ "try:\n", " int('42')\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(42)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "int = type(42)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42')" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "int = 17" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterator Protocol, ``for`` Loops" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "r = range(3, 7)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for el in r:\n", " print(el)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "r = range(3, 7)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range_iterator" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(it)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration as e:\n", " pass" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "def my_range(start, end):\n", " retlist = []\n", " while start < end:\n", " retlist.append(start)\n", " start += 1\n", " return retlist" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zu iterieren: [3, 4, 5, 6]\n", "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "r = my_range(3, 7)\n", "print('zu iterieren:', r)\n", "for el in r:\n", " print(el)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "def my_range(start, end):\n", " while start < end:\n", " yield start\n", " start += 1" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "r = my_range(3, 7)\n", "for el in r:\n", " print(el)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iterable: Der Begriff" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = [3,4,2,3]\n", "s = set(liste)\n", "len(s)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3, 4}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\n", "a\n", "l\n", "l\n", "o\n", " \n", "j\n", "o\n", "e\n", "r\n", "g\n" ] } ], "source": [ "zeichenkette = 'hallo joerg'\n", "for elem in zeichenkette:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{' ', 'a', 'e', 'g', 'h', 'j', 'l', 'o', 'r'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(zeichenkette)\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Listen funktionieren ähnlich:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['h', 'a', 'l', 'l', 'o', ' ', 'j', 'o', 'e', 'r', 'g']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list(zeichenkette)\n", "l" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable\n" ] } ], "source": [ "try:\n", " list(42)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['x']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('x')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable\n" ] } ], "source": [ "try:\n", " for element in 42:\n", " print(element)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable\n" ] } ], "source": [ "try:\n", " iter(42)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "r = range(10)\n", "my_liste = []\n", "for elem in r:\n", " my_liste.append(elem)\n", "print(my_liste)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "r = range(10)\n", "print(list(r))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Zufall" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Regular Expressions" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "filename = 'AIRMS_ETH.CSV'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simplizistisches manuelles String-Parsen ist immer blöd" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "phase = filename[0]\n", "if phase in ('A', 'B', 'C'):\n", " pass # eh ok\n", "i_oder_v = filename[1]\n", "if i_oder_v in ('I', 'V'):\n", " pass # eh ok" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A I RMS_ETH\n" ] } ], "source": [ "match = re.search('^([ABC])([IV])(.*)\\\\.CSV$', filename)\n", "if match:\n", " phase = match.group(1)\n", " i_oder_v = match.group(2)\n", " anlage = match.group(3)\n", "print(phase, i_oder_v, anlage)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " * Vorcompilieren der Regex\n", " * Raw Strings verwenden\n", " * In der ersten Gruppe 2 Characters" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AC I RMS_ETH\n" ] } ], "source": [ "filename = 'ACIRMS_ETH.CSV'\n", "my_regex = re.compile(r'^([ABC]{2})([IV])(.*)\\.CSV$')\n", "match = my_regex.search(filename)\n", "if match:\n", " phase = match.group(1)\n", " i_oder_v = match.group(2)\n", " anlage = match.group(3)\n", "print(phase, i_oder_v, anlage)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n", "alle\n", "und\n", "noch\n", "was\n" ] } ], "source": [ "liste = ['hallo', 'alle', 'und', 'noch', 'was']\n", "for element in liste:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wir wollen aber die Zeilennummer dazuhaben:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'hallo')\n", "(1, 'alle')\n", "(2, 'und')\n", "(3, 'noch')\n", "(4, 'was')\n" ] } ], "source": [ "for element in enumerate(liste):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "position: 0 , element: hallo\n", "position: 1 , element: alle\n", "position: 2 , element: und\n", "position: 3 , element: noch\n", "position: 4 , element: was\n" ] } ], "source": [ "for nr, element in enumerate(liste):\n", " print('position:', nr, ', element:', element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# JSON" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "meine_zu_sendenden_daten = ('A', 'I', 'RMS_ETH', 12.345, 220.234)\n", "meine_zu_sendenden_daten_in_string_form = json.dumps(meine_zu_sendenden_daten)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[\"A\", \"I\", \"RMS_ETH\", 12.345, 220.234]'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "meine_zu_sendenden_daten_in_string_form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Diese Daten in String Form werden ueblicherweise uebers Internet verschickt, und auf der anderen Seite im allgemeinen Fall von einem Programm empfangen, das *nicht* in Python geschrieben ist. Deswegen JSON und nicht einfach str().\n", "\n", "Der Empfaenger muss die empfangenen Daten natuerlich wieder zurueckkonvertieren:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['A', 'I', 'RMS_ETH', 12.345, 220.234]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "meine_empfangenen_daten_python_form = json.loads(meine_zu_sendenden_daten_in_string_form)\n", "meine_empfangenen_daten_python_form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Die Information, dass es ein Tupel war, ist verloren gegangen. Aber das ist mir wurscht." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }