{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Syntax etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments vs. Documentation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "das ist eine zahl zwischen 0 und 9 (inklusive): 0\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 1\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 2\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 3\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 4\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 5\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 6\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 7\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 8\n", "das ist eine zahl zwischen 0 und 9 (inklusive): 9\n" ] } ], "source": [ "# das ist ein voellig sinnloser kommentar, weil der \n", "# code sollte ja wohl selbsterklaerend sein\n", "i = 0\n", "while i < 10:\n", " print('das ist eine zahl zwischen 0 und 9 (inklusive):', i)\n", " i = i + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comment, zum Unterschied von Docstrings" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "def f(a):\n", " 'das ist eine funktion mit einem parameter a. der sollte int typ haben. diese funktion printet den parameter auf stdout'\n", " print(a)\n", "f(666)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das ist eine funktion mit einem parameter a. der sollte int typ haben. diese funktion printet den parameter auf stdout'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__doc__" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function f in module __main__:\n", "\n", "f(a)\n", " das ist eine funktion mit einem parameter a. der sollte int typ haben. diese funktion printet den parameter auf stdout\n", "\n" ] } ], "source": [ "help(f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class X in module __main__:\n", "\n", "class X(builtins.object)\n", " | X(param)\n", " | \n", " | das ist eine depperte klasse\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, param)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "class X:\n", " 'das ist eine depperte klasse'\n", " def __init__(self, param):\n", " self.param = param\n", " \n", "help(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "a = 3.14" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "a = [42, 'blah']\n", "print(type(a))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "_abc = 2\n", "_1 = 5\n", "_ = 5\n", "a123 = 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assignment Fun" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = 666" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42 666\n" ] } ], "source": [ "print(a, b)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "a, b = 42, 666" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42 666\n" ] } ], "source": [ "print(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vertauschen von zwei Variablen" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666 42\n" ] } ], "source": [ "tmp = a\n", "a = b\n", "b = tmp\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "a, b = 42, 666" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pythonic Swap:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666 42\n" ] } ], "source": [ "a, b = b, a\n", "print(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assignment Details" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variable vernichten zur Laufzeit " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "del a\n", "del b" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140032702361168" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140032702361168" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "del b" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "del a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Refcount von 42 ist auf 0 -> automatic Memory management by refcounting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dezimal ..." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1234" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1234" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1234" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1*10**3 + 2*10**2 + 3*10**1 + 4*10**0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oktal ..." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "493" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0o755" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "493" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7*(8**2) + 5*(8**1) + 5*(8**0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Binär ..." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b11" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1*2**1 + 1*2**0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Small int und Big int? (Aus SQL-Datenbanken)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "i = 0" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b11111111" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64-1" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "die welt ist noch in ordnung\n" ] } ], "source": [ "if 1 < 2:\n", " print('die welt ist noch in ordnung')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "es ist vormittag\n" ] } ], "source": [ "uhrzeit = 10\n", "# ist das zwischen 8 und 12?\n", "if 8 <= uhrzeit <= 12:\n", " print('es ist vormittag')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integer Arithmetic" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6666666666666666" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floor Division: Division ohne Rest" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2//3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modulo: Rest der Division" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6%4" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "15%4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hallo'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hallo\"" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc'def\"" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc\\'def'" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc'def\"" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc'def\"" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'hello'\n" ] } ], "source": [ "print('\\'hello\\'')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'hello'\n" ] } ], "source": [ "print(\"'hello'\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Das ist die erste Zeile\\nDas ist die zweite Zeile'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"Das ist die erste Zeile\n", "Das ist die zweite Zeile\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Raw Strings ..." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Users\\Jan\\...\n" ] } ], "source": [ "python_dir = \"C:\\\\Users\\\\Jan\\\\...\"\n", "print(python_dir)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Users\\Jan\\...\n" ] } ], "source": [ "python_dir = r\"C:\\Users\\Jan\\...\"\n", "print(python_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String Methods" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'HALLO'\n", "s.lower()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HALLO'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wie lowercased man einen String in-place?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = s.lower()\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '42'\n", "i = int(s)\n", "i" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '10'\n", "i = int(s, 16)\n", "i" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 42\n", "s = str(i)\n", "s" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.666" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('42.666')" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(42.666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings können alles!" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HALLO'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hallo'\n", "s.upper()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hallo'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ll')" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.count('ss')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'def', '666']" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc;def;666'.split(';')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complex Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List (mutable)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [] # leere Liste\n", "len(l)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list() # leere liste\n", "len(l)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 62, "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": 63, "metadata": {}, "outputs": [], "source": [ "l = list()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [42, 'blah']\n", "len(l)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140032636418048" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list index out of range\n" ] } ], "source": [ "try:\n", " l[2]\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'blah', 666]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(666)\n", "l" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140032636418048" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ``extend()``? Iterable?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Was ist ein Iterable? Etwas, über das man iterieren kann:" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "zwei\n", "3.0\n" ] } ], "source": [ "ein_iterierbares_ding = [1, 'zwei', 3.0]\n", "for element in ein_iterierbares_ding:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``extend()`` nimmt *Iterable* als Parameter:" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'blah', 666, 1, 'zwei', 3.0]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(ein_iterierbares_ding)\n", "l" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable\n" ] } ], "source": [ "try:\n", " l.extend(1)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "zwei\n", "3.0\n" ] } ], "source": [ "t = (1, 'zwei', 3.0)\n", "for element in t:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n", "d\n" ] } ], "source": [ "s = 'abcd'\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'blah', 666, 1, 'zwei', 3.0, 'a', 'b', 'c', 'd']" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(s)\n", "l" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140032636418048" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]\n", "l2 = [4,5,6]" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = l1 + l2\n", "l" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3]" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3] * 2" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2]" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "del l[2]" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple (wie Liste, nur immutable)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = () # leeres tuple\n", "len(t)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (42, 'blah')\n", "len(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Immutable:*" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(666)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Syntaktisches Extrawürschtl: Tuple mit nur einem Element" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(666)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+2)*3" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(666,)" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(666,)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(666)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print((666))" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(42, 666)" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(42, 666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``print()`` mit **einem** Parameter" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(42, 666)\n" ] } ], "source": [ "print((42, 666))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``print()`` mit **zwei** Parametern" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42 666\n" ] } ], "source": [ "print(42, 666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``in`` Operator" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "divers not implemented\n" ] } ], "source": [ "sex = 'd'\n", "if sex != 'm' and sex != 'f':\n", " print('divers not implemented')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "divers not implemented\n" ] } ], "source": [ "if sex not in ('m', 'f'):\n", " print('divers not implemented')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leeres Dictionary ..." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {}\n", "type(d)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = dict()\n", "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nicht-leeres Dictionary ..." ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "translation_table = {\n", " 'one': 1,\n", " 'zero': 0,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table['one']" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "translation_table['bullshit'] = [666, 42, 'hallo']" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[666, 42, 'hallo']" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table['bullshit']" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "translation_table = {\n", " 'one': [1, 2, 3],\n", " 'zero': 0,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table['one']" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "zero\n", "two\n" ] } ], "source": [ "for k in translation_table.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "translation_table['bullshit'] = (1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "zero\n", "two\n", "bullshit\n" ] } ], "source": [ "for k in translation_table.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo, is eh drin\n" ] } ], "source": [ "if 'bullshit' in translation_table:\n", " print('jo, is eh drin')" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'xyz' in translation_table" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "del translation_table['bullshit']" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'bullshit' in translation_table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leeres Set ..." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {} # ATTENTION!!\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set()\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nicht-leeres set ..." ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bag = {'red', 'green', 'blue'}\n", "len(bag)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blue', 'green', 'red'}" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bag" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'red' in bag" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "bag.add('black')" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'black', 'blue', 'green', 'red'}" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bag" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "bag.add(666)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{666, 'black', 'blue', 'green', 'red'}" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bag" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mengenoperationen ..." ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "set1 = {1, 2, 3, 4}\n", "set2 = {3, 4, 2, 5}" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "union = set1 | set2\n", "union" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1}" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff = set1 - set2\n", "diff" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3, 4}" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intersection = set1 & set2\n", "intersection" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 5}" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "symm_diff = set1 ^ set2\n", "symm_diff" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {} # leeres set? nein!\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set()\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = dict()\n", "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iteration über Daten: mittels ``for``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While gibts auch" ] }, { "cell_type": "code", "execution_count": 13, "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": [ "i = 0\n", "while i < 10:\n", " print(i)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 14, "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 i in range(0, 10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 16, "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 i in range(10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "for i in range(0, 10, 2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List iteration" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa\n", "Eugenie\n", "Jan\n", "Okan\n", "Joerg\n" ] } ], "source": [ "l = ['Lisa', 'Eugenie', 'Jan', 'Okan', 'Joerg']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set iteration" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Okan\n", "Joerg\n", "Lisa\n", "Eugenie\n", "Jan\n" ] } ], "source": [ "s = {'Lisa', 'Eugenie', 'Jan', 'Okan', 'Joerg'}\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'McGuire'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'Lisa': 'McGuire',\n", " 'Eugenie': 'Sinner'\n", " }\n", "d['Lisa']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary iteration" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa\n", "Eugenie\n" ] } ], "source": [ "for element in d: # implicitly meaning: iteration over keys\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa\n", "Eugenie\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "McGuire\n", "Sinner\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Lisa', 'McGuire')\n", "('Eugenie', 'Sinner')\n" ] } ], "source": [ "for element in d.items():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vorname: Lisa\n", "Nachname: McGuire\n", "Vorname: Eugenie\n", "Nachname: Sinner\n" ] } ], "source": [ "for element in d.items():\n", " vorname = element[0]\n", " nachname = element[1]\n", " print('Vorname:', vorname)\n", " print('Nachname:', nachname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking**" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vorname: Lisa\n", "Nachname: McGuire\n", "Vorname: Eugenie\n", "Nachname: Sinner\n" ] } ], "source": [ "for vorname, nachname in d.items():\n", " print('Vorname:', vorname)\n", " print('Nachname:', nachname)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PersonalNummer: 1\n", "Email: eugenie@sinner.com\n", "PersonalNummer: 2\n", "Email: lisa@mcguire.com\n" ] } ], "source": [ "l = [(1, 'Eugenie', 'Sinner', 'eugenie@sinner.com'),\n", " (2, 'Lisa', 'McGuire', 'lisa@mcguire.com')\n", " ]\n", "for persnr, vorname, nachname, email in l:\n", " print('PersonalNummer:', persnr)\n", " print('Email:', email)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``enumerate()``, ``sum()``, ``map()``, und so weiter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Lisa'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['Lisa', 'Eugenie', 'Jan', 'Okan', 'Joerg']\n", "l[0]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Problem:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa\n", "Eugenie\n", "Jan\n", "Okan\n", "Joerg\n" ] } ], "source": [ "for person in l:\n", " print(person)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa ist an position 0\n", "Eugenie ist an position 1\n", "Jan ist an position 2\n", "Okan ist an position 3\n", "Joerg ist an position 4\n" ] } ], "source": [ "position = 0\n", "for person in l:\n", " print(person, 'ist an position', position)\n", " position += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lösung: verwende doch ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Lisa')\n", "(1, 'Eugenie')\n", "(2, 'Jan')\n", "(3, 'Okan')\n", "(4, 'Joerg')\n" ] } ], "source": [ "for element in enumerate(l):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lisa ist an position 0\n", "Eugenie ist an position 1\n", "Jan ist an position 2\n", "Okan ist an position 3\n", "Joerg ist an position 4\n" ] } ], "source": [ "for position, person in enumerate(l):\n", " print(person, 'ist an position', position)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``sum()``" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15000" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [5000, 4000, 3000, 2000, 1000]\n", "sum(l)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.55" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 1.55]\n", "sum(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``map()``" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "7\n", "3\n", "4\n", "5\n" ] } ], "source": [ "l = ['Lisa', 'Eugenie', 'Jan', 'Okan', 'Joerg']\n", "for element in l:\n", " print(len(element))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 7, 3, 4, 5]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lengths = []\n", "for element in l:\n", " lengths.append(len(element))\n", "lengths" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l[0])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "7\n", "3\n", "4\n", "5\n" ] } ], "source": [ "for element in map(len, l):\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Transformation in uppercase Liste ..." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LISA\n", "EUGENIE\n", "JAN\n", "OKAN\n", "JOERG\n" ] } ], "source": [ "def upper(s):\n", " return s.upper()\n", "for element in map(upper, l):\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``list()`` function" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n", "d\n" ] } ], "source": [ "s = 'abcd'\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list(s)\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " else:\n", " return a\n", "max = maximum(42, 666)\n", "max" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(maximum)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = maximum\n", "a(1,2)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Frage: sind a und b nun auch verfuegbar im restlichen Programm?" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "def summe(l):\n", " x = 0\n", " for element in l:\n", " x += element\n", " return x\n", "\n", "gehaelter = [5000, 4000, 3000, 2000, 1000]\n", "budget = summe(gehaelter)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15000" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "budget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``x`` in ``summe()`` ist eine lokale Variable:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'x' is not defined\n" ] } ], "source": [ "try:\n", " x\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Performance: ``in`` Operator, und ``list`` und ``set``" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "l = [42, 1, 42, 666, 1, 5, 666]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... **ein** Vergleich" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... **sechs** Vergleiche" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "53 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... **sieben** Vergleiche" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Lineare Laufzeit**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zum Unterschied von ``set``" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 5, 42, 53, 56, 666}" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {1,56,42,666,1,5,53}\n", "s" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 5, 42, 53, 56, 666}" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.add(56)\n", "s" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "53 in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Konstante Laufzeit** -> optimiert für Suche (Hash Table)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dynamische Evaluierung: ``eval()``" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('[1,2,3]')" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "unexpected EOF while parsing (, line 1)\n" ] } ], "source": [ "try:\n", " eval('[1,2,')\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('666')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo joerg\n" ] } ], "source": [ "eval('print(\"hallo joerg\")')" ] } ], "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 }