{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 11: 'Simmering',\n", " 1: 'Innere Stadt',\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Simmering'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[11]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[5] = 'Keine Ahnung'\n", "len(d)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Keine Ahnung'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[5]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11 in d" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Simmering' in d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{11: 'Simmering', 1: 'Innere Stadt', 5: 'Keine Ahnung'}" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n", "1\n", "5\n" ] } ], "source": [ "for element in d:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n", "1\n", "5\n" ] } ], "source": [ "for key in d.keys():\n", " print(key)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Simmering\n", "Innere Stadt\n", "Keine Ahnung\n" ] } ], "source": [ "for value in d.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(11, 'Simmering')\n", "(1, 'Innere Stadt')\n", "(5, 'Keine Ahnung')\n" ] } ], "source": [ "for item in d.items():\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: 11, value: Simmering\n", "key: 1, value: Innere Stadt\n", "key: 5, value: Keine Ahnung\n" ] } ], "source": [ "for item in d.items():\n", " key = item[0]\n", " value = item [1]\n", " print(f'key: {key}, value: {value}')" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: 11, value: Simmering\n", "key: 1, value: Innere Stadt\n", "key: 5, value: Keine Ahnung\n" ] } ], "source": [ "for key, value in d.items():\n", " print(f'key: {key}, value: {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set()\n", "len(s)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.add(1)\n", "s.add('Joerg')\n", "len(s)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Joerg' in s" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Christopher' in s" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "s.remove('Joerg')" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Joerg' in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "s = { 1, 2, 3, 4, 5}" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "for element in s:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 'vier']" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in l" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'vier' in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iterable?" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "vier\n" ] } ], "source": [ "l = [1, 2, 3, 'vier']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'vier'}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(l)\n", "s" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "j\n", "o\n", "e\n", "r\n", "g\n" ] } ], "source": [ "mein_string = 'joerg'\n", "for element in mein_string:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'e', 'g', 'j', 'o', 'r'}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(mein_string)\n", "s" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "g\n", "j\n", "e\n", "o\n", "r\n" ] } ], "source": [ "for element in s:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'g' in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The ``range()`` Function" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "i = 1\n", "while i <=10:\n", " print(i)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for element in range(1, 11):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "for element in range(0, 10, 2):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for element in range(10):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for element in r:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range_iterator" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(it)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for element in range(3):\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List Comprehension, Generator Expression" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "def squares(iterable):\n", " sq = []\n", " for elem in iterable:\n", " sq.append(elem**2)\n", " return sq\n", "for elem in squares([0,1,2,3,4]):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in [i**2 for i in [0,1,2,3,4]]:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in [i**2 for i in range(5)]:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generator expression:" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in (i**2 for i in range(5)):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "4\n", "16\n", "36\n", "64\n" ] } ], "source": [ "for elem in (i**2 for i in range(10) if i%2==0):\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Literals" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Quoting?" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Hello 'World'\"" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello \\'World\\''" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Hello 'World'\"" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello 'World'\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Embedded Newline Character**" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Das ist eine \n", "mehrzeilige Dokumentation\n", "von irgendwas\n" ] } ], "source": [ "docu = 'Das ist eine \\nmehrzeilige Dokumentation\\nvon irgendwas'\n", "print(docu)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiline Strings" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Das ist eine\n", "mehrzeilige Dokumentation\n", "von irgendwas\n" ] } ], "source": [ "docu = '''Das ist eine\n", "mehrzeilige Dokumentation\n", "von irgendwas'''\n", "print(docu)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Raw Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Man hat Files mit folgendem deppertem Inhalt:" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "line = 'irgendein irrelevanter quargel, Joerg, Faschingbauer, 190666'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lösung: regular expressions" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "import re\n", "pattern = re.compile(r'^.*, (\\w+), (\\w+), (\\d+)')" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "match = pattern.search(line)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vorname: Joerg Nachname: Faschingbauer Geburtsdatum: 190666\n" ] } ], "source": [ "if match is None:\n", " print('Inkorrekte Zeile')\n", "else:\n", " print('Vorname:', match.group(1), 'Nachname:', match.group(2), 'Geburtsdatum:', match.group(3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functionality" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "s = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "s = s.capitalize()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' Hello world '" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(50)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.count('ss')" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'ein-excel-export.csv'.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('ss')" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('sss')" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.index('ss')" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "not found\n" ] } ], "source": [ "try:\n", " 'mississippi'.index('sss')\n", "except ValueError:\n", " print('not found')" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isalpha()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'123'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'123'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.islower()" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'ABC'.islower()" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello-World'" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'-'.join(['Hello', 'World'])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['arg1', 'arg2', 'arg3']" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'arg1,arg2,arg3'.split(',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``strip()`` and friends" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah blah'" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' blah blah \\t\\n '.strip()" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lah blah'" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abcblah blahabcbcabcc'.strip('abc')" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc'" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' abc '.rstrip()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc '" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' abc '.lstrip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String Formatting" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 666'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {0} {1}'.format('World', 666)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 666'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {was} {wieviel}'.format(wieviel=666, was='World')" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 666'" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {was:<10} {wieviel}'.format(wieviel=666, was='World')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### f-Strings (since Python 3.6)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "wieviel = 666\n", "was = 'Hello'" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello Hello 666'" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'Hello {was} {wieviel}'" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello Hello 666'" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'Hello {was:<10} {wieviel}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``datetime``" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 7, 11, 23, 11, 33)" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now = datetime.datetime(2021, 7, 11, 23, 11, 33)\n", "now" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(days=3, seconds=39)" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "delta = datetime.timedelta(days=3, seconds=39)\n", "delta" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 7, 14, 23, 12, 12)" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now + delta" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 7, 8, 23, 10, 54)" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now - delta" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 8, 25, 15, 3, 10, 995237)" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now = datetime.datetime.now()\n", "now" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parsing date formats" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "datestr = '11.07.2021'" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 7, 11, 0, 0)" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt = datetime.datetime.strptime(datestr, '%d.%m.%Y')\n", "dt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# JSON" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "import json\n", "import uuid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a message object to, for example, transfer it via MQTT" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "msg_aussi = {\n", " 'message': 'seas oida',\n", " 'msgid': uuid.uuid4().hex,\n", " 'timestamp': 345654325678,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert it to JSON for the transfer" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"message\": \"seas oida\", \"msgid\": \"6563515ef0824d42b459389cefd09127\", \"timestamp\": 345654325678}'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json_aussi = json.dumps(msg_aussi)\n", "json_aussi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Transfer it" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "json_eini = json_aussi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert JSON back to dictionary that the sender had, originally" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'message': 'seas oida',\n", " 'msgid': '6563515ef0824d42b459389cefd09127',\n", " 'timestamp': 345654325678}" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "msg_eini = json.loads(json_eini)\n", "msg_eini" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }