{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 6 Grundlegende Sprachelemente" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.1 Syntax und Semantik" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zur Syntax mit der Einrückung ..." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo super\n", "noch besser\n" ] } ], "source": [ "variable = 666\n", "if variable == 666:\n", " print('jo super')\n", " print('noch besser')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In C zum Beispiel geht das so (wobei hier die Einrückung irrelevant ist):\n", "\n", " void f() {\n", " int variable = 666;\n", " /* ... */\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In Python können Variablen ihren Typ ändern" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n" ] } ], "source": [ "variable = 666\n", "print(type(variable))\n", "variable = 3.14\n", "print(type(variable))\n", "variable = 'Ein depperter String'\n", "print(type(variable))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sidestep: selbst Funktionen sind Objekte, halt eben vom Typ \"Function\" und nicht z.B. ``int``" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(print)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``print`` ist nur ein Name für ein Funktionsobjekt. Eine Variable sozusagen. Diese kann man genauso zuweisen und verwenden (aufrufen, in diesem Fall)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "variable = print\n", "variable('Hello World!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Kommentare" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "initialwert ist 0\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "fertig\n" ] } ], "source": [ "zaehler = 0\n", "print('initialwert ist', zaehler)\n", "# jetzt zaehlen wir von 0 bis 9\n", "while zaehler < 10:\n", " print(zaehler)\n", " # hier bereiten wir den naechsten schleifendurchlauf vor\n", " zaehler = zaehler + 1 # oder auch: zaehler += 1\n", "print('fertig')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.2 Grundlegende Elemente einer Sprache" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.3 Standarddatentypen (elementare Datentypen)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integers sind beliebig groß" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dieser hier passt locker in ein 64 Bit CPU Register" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1024\n" ] } ], "source": [ "mein_integer = 2**10\n", "print(mein_integer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dieser hier gerade noch. Um eins mehr und er wäre dort wieder 0." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64-1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python schützt mich vor solchen *Overflow-Bugs*, indem Zahlen, die nicht in die CPU passen, in Software gehandhabt werden." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2 hoch 1000 passt in keine CPU." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vergleichsoperatoren" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 == 42" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eh klar\n" ] } ], "source": [ "if 42 == 42:\n", " print('eh klar')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 <= 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 != 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operator Präzedenz: Punkt vor Strichrechnung" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 * 3" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 + 2) * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Floor Division" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1/2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In C ist aber ``3/2 == 1`` !!! Rechnen tut dort ausschliesslich die CPU, und ``3/2`` ist eine Ganzzahldivision mit einem ganzzahligen Resultat." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modulo Operator: %" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rest einer ganzzahligen Division" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9%2" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11%2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9%3" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9%5" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42 ist gerade\n" ] } ], "source": [ "variable = 42\n", "if variable % 2 == 0:\n", " print(variable, 'ist gerade')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (Unärer) Negations Operator" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "variable = 42\n", "variable = -variable" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-42" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operator Präzedenz erzwingen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* und % haben gleichen Vorrang" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 9 % 5" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * (9 % 5)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 * 9) % 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings gibts auch (Zeichenketten)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "s = \"abc\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "s = 'abc'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "# das da geht nicht: s = \"abc\"def\"" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\"def\n" ] } ], "source": [ "s = 'abc\"def'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc'def\n" ] } ], "source": [ "s = \"abc'def\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\"def'ghi\n" ] } ], "source": [ "s = \"abc\\\"def'ghi\"\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiline Strings" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo\n", "Welt,\n", "wie geht's denn so?\n", "Hier ist eine vierte Zeile auch noch.\n", "\n" ] } ], "source": [ "s = '''Hallo\n", "Welt,\n", "wie geht's denn so?\n", "Hier ist eine vierte Zeile auch noch.\n", "'''\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo\n", "Welt,\n", "wie geht's denn so?\n", "\n" ] } ], "source": [ "s = 'Hallo\\nWelt,\\nwie geht\\'s denn so?\\n'\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Raw Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``\\n`` ist ein Linefeed. ``\\P`` und ``\\M`` haben keine spezielle Bedeutung und bleiben unangetastet." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programme\\Mein Glump\n", "ebenlaeufigkeitsdemo\n" ] } ], "source": [ "path = 'C:\\Programme\\Mein Glump\\nebenlaeufigkeitsdemo'\n", "print(path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Raw Strings sind dafür die Lösung." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programme\\Mein Glump\\nebenlaeufigkeitsdemo\n" ] } ], "source": [ "path = r'C:\\Programme\\Mein Glump\\nebenlaeufigkeitsdemo'\n", "print(path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### f-Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "f-Strings gibts ab Python 3.6: sehr feine Formatierungsmöglichkeit." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "var1 = 'hallo'\n", "var2 = 'hello'\n", "pfx_var1 = 'var1='\n", "pfx_var2 = 'var2='" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=hallo, var2=hello\n" ] } ], "source": [ "ausgabe = pfx_var1+var1+', '+pfx_var2+var2\n", "print(ausgabe)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "var1 = 42\n", "var2 = 666\n", "pfx_var1 = 'var1='\n", "pfx_var2 = 'var2='" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=42, var2=666\n" ] } ], "source": [ "ausgabe = pfx_var1+str(var1)+', '+pfx_var2+str(var2)\n", "print(ausgabe)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bessere Formatierung: nicht potschert, mit f-String (seit Python 3.6) " ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=hallo, var2=hello\n" ] } ], "source": [ "var1 = 'hallo'\n", "var2 = 'hello'\n", "ausgabe = f'var1={var1}, var2={var2}'\n", "print(ausgabe)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=42, var2=666\n" ] } ], "source": [ "var1 = 42\n", "var2 = 666\n", "print(f'var1={var1}, var2={var2}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Konvertierungen" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '666'\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "i = int(s)\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: 'abc'\n" ] } ], "source": [ "try: # notwendig, um jupyter notebook nicht abzubrechen. bitte ignorieren\n", " s = 'abc'\n", " i = int(s)\n", "except ValueError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.666\n" ] } ], "source": [ "s = '42.666'\n", "f = float(s)\n", "print(f)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n" ] } ], "source": [ "s = '10'\n", "i = int(s, 16)\n", "print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 < 1" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "b = True\n", "print(type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zum Beispiel:" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "liste = [3,2,1,4]\n", "gefunden = False\n", "for wert in liste:\n", " if wert == 4:\n", " gefunden = True\n", "print(gefunden)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Präzendenzregeln: ``and`` vor ``or``" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False and True" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False and True" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False and True or False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ist das gleiche wie:" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(False and True) or False" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False or True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ist das gleiche wie:" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(True and False) or True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.4 Literale für primitive Datentypen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.5 Variablen und Konstanten" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.6 Operatoren" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.7 Ausdrücke und Operatorrangfolgen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.8 Übungen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Zwei Variablen vertauschen" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "var1 = 42\n", "var2 = 666" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "tmp = var1\n", "var1 = var2\n", "var2 = tmp" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1: 666 var2: 42\n" ] } ], "source": [ "print('var1:', var1, 'var2:', var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Zwei Variablen vertauschen mit *Tuple Unpacking*" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "var1 = 42\n", "var2 = 666" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "var1, var2 = var2, var1" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1: 666 var2: 42\n" ] } ], "source": [ "print('var1:', var1, 'var2:', var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Übung 1, 5." ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "value1 = 501\n", "value2 = 799\n", "value3 = 400" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(value1 > 500) or (value2 < 800) and not (value3 == 400)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(value1 > 500) or ((value2 < 800) and not (value3 == 400))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 7 Kontrollstrukturen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.1 Anweisungen und Folgen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.2 Bedingungen und Kontrollstrukturen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.3 Grundlagen zu Verzweigungen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.4 Bedingte Anweisung" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.5 Verzweigung" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.6 Geschachtelte Verzweigung" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.7 Mehrfache Verzweigung (Fallauswahl)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.8 Schleifen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.9 Zählergesteuerte Schleifen (Iteration)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.10 Kopfgesteuerte bedingte Schleife" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.11 Fußgesteuerte bedingte Schleife" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) ``break`` und ``continue``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Sequential Datatypes" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "s = \"Hello World\"" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python beginnt man bei 0. In C, Java und C# (und vielen anderen Sprachen) auch." ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Negative Indizes erscheinen komisch, sind aber logisch." ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[len(s)-1]" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1]" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "l = ['erstes element', 2, 'drei']" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'erstes element'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'drei'" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) ``for`` Loops" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "s = 'Hello World'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bequem:" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n", "e\n", "l\n", "l\n", "o\n", " \n", "W\n", "o\n", "r\n", "l\n", "d\n" ] } ], "source": [ "for character in s:\n", " print(character)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n", "e\n", "l\n", "l\n", "o\n", " \n", "W\n", "o\n", "r\n", "l\n", "d\n" ] } ], "source": [ "i = 0\n", "while i < len(s):\n", " print(s[i])\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Umständlich:" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "erstes element\n", "2\n", "drei\n" ] } ], "source": [ "l = ['erstes element', 2, 'drei']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration mit Dictionaries" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "mapping_table = {\n", " 'null': 'zero',\n", " 'eins': 'one',\n", " 'zwei': 'two',\n", " 'drei': 'three',\n", " 'vier': 'four',\n", " 'fünf': 'five',\n", " 'sechs': 'six',\n", " 'sieben': 'seven',\n", " 'acht': 'eight',\n", " 'neun': 'nine',\n", "}" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'seven'" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping_table['sieben']" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "mapping_table['zehn'] = 'ten'" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ten'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping_table['zehn']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über ein Dictionary and sich iteriert über die Keys." ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "null\n", "eins\n", "zwei\n", "drei\n", "vier\n", "fünf\n", "sechs\n", "sieben\n", "acht\n", "neun\n", "zehn\n" ] } ], "source": [ "for e in mapping_table:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explicit is better than implicit." ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "null\n", "eins\n", "zwei\n", "drei\n", "vier\n", "fünf\n", "sechs\n", "sieben\n", "acht\n", "neun\n", "zehn\n" ] } ], "source": [ "for e in mapping_table.keys():\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über die Values." ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero\n", "one\n", "two\n", "three\n", "four\n", "five\n", "six\n", "seven\n", "eight\n", "nine\n", "ten\n" ] } ], "source": [ "for e in mapping_table.values():\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über Key/Value Paare: Tuple" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('null', 'zero')\n", "('eins', 'one')\n", "('zwei', 'two')\n", "('drei', 'three')\n", "('vier', 'four')\n", "('fünf', 'five')\n", "('sechs', 'six')\n", "('sieben', 'seven')\n", "('acht', 'eight')\n", "('neun', 'nine')\n", "('zehn', 'ten')\n" ] } ], "source": [ "for e in mapping_table.items():\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Manuelles Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key=null, value=zero\n", "key=eins, value=one\n", "key=zwei, value=two\n", "key=drei, value=three\n", "key=vier, value=four\n", "key=fünf, value=five\n", "key=sechs, value=six\n", "key=sieben, value=seven\n", "key=acht, value=eight\n", "key=neun, value=nine\n", "key=zehn, value=ten\n" ] } ], "source": [ "for e in mapping_table.items():\n", " key = e[0]\n", " value = e[1]\n", " print(f'key={key}, value={value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple Unpacking mit Hilfe der Sprache" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key=null, value=zero\n", "key=eins, value=one\n", "key=zwei, value=two\n", "key=drei, value=three\n", "key=vier, value=four\n", "key=fünf, value=five\n", "key=sechs, value=six\n", "key=sieben, value=seven\n", "key=acht, value=eight\n", "key=neun, value=nine\n", "key=zehn, value=ten\n" ] } ], "source": [ "for key, value in mapping_table.items():\n", " print(f'key={key}, value={value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ausflug: Vertauschen von zwei Variablen" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "var1 = 666\n", "var2 = 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Potschert:" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=42, var2=666\n" ] } ], "source": [ "tmp = var1\n", "var1 = var2\n", "var2 = tmp\n", "print(f'var1={var1}, var2={var2}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mit Tuple Unpacking:" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "var1=666, var2=42\n" ] } ], "source": [ "var1, var2 = var2, var1\n", "print(f'var1={var1}, var2={var2}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) The ``range()`` Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Primzahlen checken, der Algorithmus:\n", "1 ist keine Primzahl\n", "Sonst:\n", "von 2 bis eins unter der zu checkenden Zahl, probiere alle durch, ob die net gach ein Teiler der zu checkenden Zahl ist." ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nicht prim\n" ] } ], "source": [ "zahl = 15\n", "teiler_kandidaten = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n", "\n", "for n in teiler_kandidaten:\n", " if zahl % n == 0:\n", " print('nicht prim')\n", " break\n", "else:\n", " print('prim')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wenn ich jetzt zum Beispiel die Zahl 7, und nicht 15, checken will, muss ich gleichemaßen die ``teiler_kandidaten`` ändern." ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "prim\n" ] } ], "source": [ "zahl = 7\n", "teiler_kandidaten = [2, 3, 4, 5, 6]\n", "\n", "for n in teiler_kandidaten:\n", " if zahl % n == 0:\n", " print('nicht prim')\n", " break\n", "else:\n", " print('prim')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Raymond Hettinger: \"There must be a better way\"" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nicht prim\n" ] } ], "source": [ "zahl = 15\n", "\n", "for n in range(2, zahl):\n", " if zahl % n == 0:\n", " print('nicht prim')\n", " break\n", "else:\n", " print('prim')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der Vollständigkeit halber (steht in der Doku auf python.org): was kann ``range()`` noch?" ] }, { "cell_type": "code", "execution_count": 105, "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": "markdown", "metadata": {}, "source": [ "Das gleiche, explizit mit 0 startend" ] }, { "cell_type": "code", "execution_count": 106, "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": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(2, 10):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Von inklusive 2 bis exklusive 15, Schrittweite 3" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "5\n", "8\n", "11\n", "14\n" ] } ], "source": [ "for i in range(2, 15, 3):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.12 Schnellübersicht" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.13 Sprunganweisungen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.14 Endlosschleifen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.15 Übungen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 8 Elementare Datenstrukturen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.1 Warum werden Datenstrukturen benötigt?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) References, (Im)mutability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variable ``a`` referenziert ein Objekt vom Typ ``int`` mit Wert 42" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f24f2ac0e50'" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``b`` zeigt auf das **selbe** Objekt (gleiche ID)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f24f2ac0e50'" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``int`` is immutable, deswegen muss ``+=`` ein neues Objekt ablegen" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "b += 1" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f24f2ac0e70'" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(b))" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'0x7f24f2ac0e70'" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(b))" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f24f2ac0e50'" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Laufzeitverhalten bei der Suche (der ``in`` Operator)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suche in einer Liste kann nur sequentielle Suche sein. Arschlangsam." ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [], "source": [ "l = [4, 7, 1, 2, 89, 1, 5]" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gefunden\n", "num_comparisons=7\n" ] } ], "source": [ "key = 5\n", "num_comparisons = 0\n", "for element in l:\n", " num_comparisons += 1\n", " if element == key:\n", " print('gefunden')\n", " break\n", "else:\n", " print('nicht gefunden')\n", "print(f'num_comparisons={num_comparisons}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der ``in`` Operator macht das bequemer. Aber *Vorsicht* mit Listen, wie gesagt." ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``set`` ist eine Datenstruktur, die für schnelles Suchen, Einfügen und Löschen optimiert ist. Perfekt für den ``in`` Operator." ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {4, 7, 1, 2, 89, 1, 5}\n", "5 in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.2 Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.3 Eindimensionale Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.4 Records" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.5 Zeichenketten" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.6 Tupel und Listen" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "l = [1, 'zwei', 3]" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3, 'vier']" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append('vier')\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Einitreten zwischen 'zwei' und 3:" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 'einitreten', 3, 'vier']" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(2, 'einitreten')\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple mit einem Element (Jo Himmi!)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ein element'" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ('ein element')\n", "t" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "t = ('ein element',)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der Hintergrund (Operator Präzedenz) " ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 * 3" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 + 2) * 3" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sorted, reversed" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reversed(l)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "1\n" ] } ], "source": [ "for elem in reversed(l):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "l = [3, 2, 4, 6, 1]" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 6]" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "6\n" ] } ], "source": [ "for elem in sorted(l):\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Comprehension" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n" ] } ], "source": [ "def squares(l):\n", " result = []\n", " for elem in l:\n", " result.append(elem**2)\n", " return result\n", "\n", "meine_liste = [1, 2, 3, 4, 5, 6, 7, 8]\n", "for elem in squares(meine_liste):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n" ] } ], "source": [ "meine_liste = [1, 2, 3, 4, 5, 6, 7, 8]\n", "for elem in [elem**2 for elem in meine_liste]:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "16\n", "36\n", "64\n" ] } ], "source": [ "meine_liste = [1, 2, 3, 4, 5, 6, 7, 8]\n", "for elem in [elem**2 for elem in meine_liste if elem % 2 == 0]:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.7 Dictionaries" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "mapping_table = {\n", " 'null': 'zero',\n", " 'eins': 'one',\n", " 'zwei': 'two',\n", " 'drei': 'three',\n", " 'vier': 'four',\n", " 'fünf': 'five',\n", " 'sechs': 'six',\n", " 'sieben': 'seven',\n", " 'acht': 'eight',\n", " 'neun': 'nine',\n", "}" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping_table['eins']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``[]`` bricht beinhart mit einer Exception (``KeyError``) ab, wenn das ELement nicht existiert." ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'elf'\n" ] } ], "source": [ "try: # necessary to keep jupyter notebook going\n", " mapping_table['elf']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``get()`` hingegen liefert ``None``, wenn das ELement nicht existiert. Je nachdem, wie man es lieber hat." ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gefunden, wert nine\n" ] } ], "source": [ "value = mapping_table.get('neun')\n", "if value is None:\n", " print('nicht gefunden')\n", "else:\n", " print(f'gefunden, wert {value}')" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nicht gefunden\n" ] } ], "source": [ "value = mapping_table.get('zehn')\n", "if value is None:\n", " print('nicht gefunden')\n", "else:\n", " print(f'gefunden, wert {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der zweite Parameter wird returned, wenn das Element nicht existiert." ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'jessas'" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = mapping_table.get('zehn', 'jessas')\n", "value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Records in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.8 Mengen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.9 Besondere Datenstrukturen anhand von Stapel (Stack) und Schlangen (Queue)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.10 Übungen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Übung 1" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [], "source": [ "numbers = (0, 10, 12, 4, 7, 20, 21, 13)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Versuch 1: ``in`` und ``index()``" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "zahl = 20" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20 gefunden, position 5\n" ] } ], "source": [ "if zahl in numbers:\n", " pos = numbers.index(zahl)\n", " print(f'{zahl} gefunden, position {pos}')\n", "else:\n", " print('nicht gefunden')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Versuch 2: Zurück zu ``while`` ..." ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gefunden, position 5\n" ] } ], "source": [ "i = 0\n", "while i < len(numbers):\n", " if numbers[i] == zahl:\n", " print(f'gefunden, position {i}')\n", " break\n", " i += 1\n", "else:\n", " print('nicht gefunden')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Versuch 3: hybrid potschert mit ``for``" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gefunden, position 5\n" ] } ], "source": [ "pos = 0\n", "for elem in numbers:\n", " if elem == zahl:\n", " print(f'gefunden, position {pos}')\n", " break\n", " pos += 1\n", "else:\n", " print('nicht gefunden')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Versuch 4: supergscheit mit ``for`` und ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gefunden, position 5\n" ] } ], "source": [ "for pos, elem in enumerate(numbers):\n", " if elem == zahl:\n", " print(f'gefunden, position {pos}')\n", " break\n", "else:\n", " print('nicht gefunden')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 9 Methoden, Prozeduren und Funktionen" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print(maximum(42, 666))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Funktionspointer (so heissts in C): Funktionsobjekte (Python)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Funktionen sind in Wirklichkeit nur Namen für *Funktionsobjekte*. Ordinäre Variablen, die man zuweisen etc. kann." ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(maximum))" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "blah = maximum" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(blah))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aufruf des Funktionsobjektes, auf das auch ``maximum`` zeigt. Nur halt über den alternativen Namen ``blah``." ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print(blah(42, 666))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Primzahlenbeispiel mit Funktion" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 prime: False\n", "2 prime: True\n", "3 prime: True\n", "4 prime: False\n", "11 prime: True\n" ] } ], "source": [ "def is_prime(num):\n", " # low hanging fruit\n", " if num == 1:\n", " return False\n", "\n", " # gemmas an\n", " for divisor_candidate in range(2, num//2+1):\n", " if num % divisor_candidate == 0:\n", " return False\n", " else:\n", " return True\n", " \n", "\n", "n = 1\n", "print(f'{n} prime: {is_prime(n)}')\n", "n = 2\n", "print(f'{n} prime: {is_prime(n)}')\n", "n = 3\n", "print(f'{n} prime: {is_prime(n)}')\n", "n = 4\n", "print(f'{n} prime: {is_prime(n)}')\n", "n = 11\n", "print(f'{n} prime: {is_prime(n)}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (jf) Local vs. Global Scope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Laufzeitfehler: Variable existiert nicht" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "def f():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'x' is not defined\n" ] } ], "source": [ "try: # wegen jupyter notebook\n", " f()\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lokale Variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dogma**: wenn an einen Namen zugewiesen wird, der im aktuellen Context (\"wir sind in Funktion ``f``\") noch nicht existiert, wird in eben diesem Context dieser Name (diese Variable) erzeugt." ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "35\n" ] } ], "source": [ "def f(param):\n", " meine_variable = param * 7\n", " print(meine_variable)\n", "f(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Globale Variable (lesen)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "def f():\n", " print(eine_variable)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "eine_variable = 666\n", "f()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Globale Variable (schreiben/zuweisen)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hier wird eine **lokale** Variable angelegt, die **nichts** mit der globalen zu tun hat. Streng nach obigem Dogma." ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local: 666\n", "global: 7\n" ] } ], "source": [ "eine_variable = 7\n", "def f():\n", " eine_variable = 666\n", " print(f'local: {eine_variable}')\n", "\n", "f()\n", "print(f'global: {eine_variable}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wenn man eine globale Variable will, muss man es extra dazusagen. Nicht wie in Javascript." ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local: 666\n", "global: 666\n" ] } ], "source": [ "eine_variable = 7\n", "def f():\n", " global eine_variable\n", " eine_variable = 666\n", " print(f'local: {eine_variable}')\n", "\n", "f()\n", "print(f'global: {eine_variable}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fehlerfall von Peter" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "noch_eine_globale_variable = 42" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "def increment_diese_variable():\n", " noch_eine_globale_variable = noch_eine_globale_variable + 1" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local variable 'noch_eine_globale_variable' referenced before assignment\n" ] } ], "source": [ "try: # wegen jupyter notebook schon wieder\n", " increment_diese_variable()\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.1 Unterprogramme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.2 Parameterübergabe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.3 Parameterübergabe als Wert" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.4 Parameterübergabe über Referenzen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.5 Rückgabewerte von Funktionen oder Methoden" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.6 Innere Funktionen - Closures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.7 Standardbibliotheken und Built-in-Funktionalitäten" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.8 Übungen" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 4 }