From d38d917e716822302315eb40a965757926fc97ee Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Wed, 8 May 2024 17:10:40 +0200 Subject: [PATCH] Updated notebooks Signed-off-by: Christopher Arndt --- notebooks/basictypes.html | 333 +- notebooks/basictypes.ipynb | 284 +- notebooks/basictypes.py | 47 +- notebooks/containertypes.html | 150 +- notebooks/containertypes.ipynb | 131 +- notebooks/containertypes.py | 29 +- notebooks/controlflow.html | 357 +- notebooks/controlflow.ipynb | 295 +- notebooks/controlflow.py | 2 - notebooks/data.json | 1 + notebooks/dateien.html | 7922 +++++++++++++++++++++++++++++++ notebooks/dateien.ipynb | 313 ++ notebooks/dateien.py | 99 + notebooks/installation.ipynb | 0 notebooks/objects.html | 7923 ++++++++++++++++++++++++++++++++ notebooks/objects.ipynb | 337 ++ notebooks/objects.py | 160 + 17 files changed, 18200 insertions(+), 183 deletions(-) create mode 100644 notebooks/data.json create mode 100644 notebooks/dateien.html create mode 100644 notebooks/dateien.ipynb create mode 100644 notebooks/dateien.py delete mode 100644 notebooks/installation.ipynb create mode 100644 notebooks/objects.html create mode 100644 notebooks/objects.ipynb create mode 100644 notebooks/objects.py diff --git a/notebooks/basictypes.html b/notebooks/basictypes.html index bf399d8..c0de9bd 100644 --- a/notebooks/basictypes.html +++ b/notebooks/basictypes.html @@ -7531,7 +7531,7 @@ a.anchor-link { - - - - - - + - - - - - - + + - - - + - - + + + + @@ -8047,7 +8169,7 @@ a.anchor-link { - - - + - - - - - - - - + - - - - - + - - - - - + diff --git a/notebooks/controlflow.ipynb b/notebooks/controlflow.ipynb index 25f8eee..1db9637 100644 --- a/notebooks/controlflow.ipynb +++ b/notebooks/controlflow.ipynb @@ -16,9 +16,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hmm, ob das so stimmt?\n" + ] + } + ], "source": [ "# if / else / elif\n", "\n", @@ -34,9 +42,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nach Süden, nach Süden!\n" + ] + } + ], "source": [ "richtung = \"s\"\n", "\n", @@ -52,9 +68,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Auf in den hohen Norden!\n" + ] + } + ], "source": [ "# match\n", "\n", @@ -83,9 +107,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Schleifendurchlauf Nr. 0\n", + "Schleifendurchlauf Nr. 1\n", + "Schleifendurchlauf Nr. 2\n", + "Schleifendurchlauf Nr. 3\n", + "Schleifendurchlauf Nr. 4\n" + ] + } + ], "source": [ "# for\n", "\n", @@ -95,9 +131,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Summe der Zahlen 1-10: 55\n" + ] + } + ], "source": [ "result = 0\n", "for i in range(1, 11):\n", @@ -107,9 +151,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 passt nicht in die Reihe\n" + ] + } + ], "source": [ "for i in (2, 4, 6, 8, 9, 10):\n", " if i % 2:\n", @@ -118,9 +170,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 ... 9 ... 8 ... 7 ... 6 ... 5 ... 4 ... 3 ... 2 ... 1 ... Lift off!\n" + ] + } + ], "source": [ "# while\n", "\n", @@ -133,9 +193,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "11\n", + "12\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n" + ] + } + ], "source": [ "# continue\n", "\n", @@ -147,9 +223,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Iteration: 142\n" + ] + } + ], "source": [ "# break\n", "\n", @@ -169,9 +253,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "q ist Buchstabe Nr. 17 des dt. Alphabets.\n" + ] + } + ], "source": [ "alphabet = \"abcdefghifklmnopqrstuvwxyz\"\n", "buchstabe = \"q\"\n", @@ -185,9 +277,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "q ist Buchstabe Nr. 17 des dt. Alphabets.\n" + ] + } + ], "source": [ "# einfacher:\n", "\n", @@ -206,9 +306,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "256 256 100\n" + ] + } + ], "source": [ "# Aufruf eingebauter Funktionen\n", "\n", @@ -220,9 +328,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], "source": [ "# optionale Parameter\n", "\n", @@ -232,9 +349,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], "source": [ "# eigene Funktionen\n", "\n", @@ -246,9 +371,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "24\n" + ] + } + ], "source": [ "# Keyword arguments\n", "\n", @@ -258,9 +392,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "positional argument follows keyword argument (1525390589.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[32], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m print(add(a=6, 10))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" + ] + } + ], "source": [ "# Das geht nicht:\n", "print(add(a=6, 10))" @@ -275,9 +418,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n" + ] + } + ], "source": [ "a = 10\n", "b = 50\n", @@ -296,9 +454,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n", + "False\n" + ] + } + ], "source": [ "# Negation\n", "\n", @@ -310,9 +479,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n" + ] + } + ], "source": [ "# logische Kombination\n", "\n", @@ -323,23 +502,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n", + "False\n" + ] + } + ], "source": [ "# Bool'scher (True/False) Wert von Integers, Floats und Strings \n", "print(a > c)\n", "print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen Wert haben\n", "# Aber:\n", "print(bool(a))\n", - "print(not a) # auch ein bool'scher Ausdruck\n" + "print(not a) # auch ein bool'scher Ausdruck" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "True\n", + "True\n", + "False\n", + "True\n" + ] + } + ], "source": [ "print(bool(0))\n", "print(bool(1))\n", @@ -350,7 +557,7 @@ "print(bool(\"True\"))\n", "print(bool(\"False\")) # !!!\n", "print(bool(\"\"))\n", - "print(bool(\"\\0\"))\n" + "print(bool(\"\\0\"))" ] } ], diff --git a/notebooks/controlflow.py b/notebooks/controlflow.py index c8b442c..61a10eb 100644 --- a/notebooks/controlflow.py +++ b/notebooks/controlflow.py @@ -197,7 +197,6 @@ print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen print(bool(a)) print(not a) # auch ein bool'scher Ausdruck - # %% print(bool(0)) print(bool(1)) @@ -211,4 +210,3 @@ print(bool("")) print(bool("\0")) - diff --git a/notebooks/data.json b/notebooks/data.json new file mode 100644 index 0000000..bfdb802 --- /dev/null +++ b/notebooks/data.json @@ -0,0 +1 @@ +{"foo": 42, "bar": 23, "baz": [1, 2, 3]} \ No newline at end of file diff --git a/notebooks/dateien.html b/notebooks/dateien.html new file mode 100644 index 0000000..e5ceb5a --- /dev/null +++ b/notebooks/dateien.html @@ -0,0 +1,7922 @@ + + + + + +dateien + + + + + + + + + + + + +
+ +
+ + diff --git a/notebooks/dateien.ipynb b/notebooks/dateien.ipynb new file mode 100644 index 0000000..bcdd13a --- /dev/null +++ b/notebooks/dateien.ipynb @@ -0,0 +1,313 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dateien" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\")\n", + "content = fp.read()\n", + "print(type(content))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", \"rb\")\n", + "content = fp.read()\n", + "print(type(content))\n", + "text = content.decode(\"utf-8\")\n", + "print(type(text))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'Anna: 2000\\n'\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", mode=\"r\")\n", + "line = fp.readline()\n", + "print(repr(line))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Anna: 2000\n", + "Mark: 3000\n", + "Judith: 3500\n", + "Thomas: 2500\n", + "Barbara: 3000\n", + "Elke: 3300\n", + "Michael: 2800\n", + "Johann: 2000\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n", + "while True:\n", + " line = fp.readline()\n", + " if not line:\n", + " break\n", + " print(line, end=\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "['Anna: 2000\\n', 'Mark: 3000\\n', 'Judith: 3500\\n', 'Thomas: 2500\\n', 'Barbara: 3000\\n', 'Elke: 3300\\n', 'Michael: 2800\\n', 'Johann: 2000\\n']\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n", + "lines = fp.readlines()\n", + "print(type(lines))\n", + "print(lines)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Anna: 2000\n", + "Mark: 3000\n", + "Judith: 3500\n", + "Thomas: 2500\n", + "Barbara: 3000\n", + "Elke: 3300\n", + "Michael: 2800\n", + "Johann: 2000\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n", + "for line in fp:\n", + " print(line, end=\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Anna: 2000\n", + "Mark: 3000\n", + "Judith: 3500\n", + "Thomas: 2500\n", + "Barbara: 3000\n", + "Elke: 3300\n", + "Michael: 2800\n", + "Johann: 2000\n" + ] + } + ], + "source": [ + "fp = open(\"../uebungen/salaries.txt\", mode=\"r\", encoding=\"utf-8\")\n", + "for line in fp:\n", + " print(line, end=\"\")\n", + "\n", + "fp.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "lines = open(\"../uebungen/salaries.txt\", encoding=\"utf-8\").readlines()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Anna: 2000\n", + "Mark: 3000\n", + "Judith: 3500\n", + "Thomas: 2500\n", + "Barbara: 3000\n", + "Elke: 3300\n", + "Michael: 2800\n", + "Johann: 2000\n" + ] + } + ], + "source": [ + "with open(\"../uebungen/salaries.txt\") as fp:\n", + " for line in fp:\n", + " print(line, end=\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Pseudo Code! Nicht ausführbar\n", + "fp = open(\"../uebungen/salaries.txt\")\n", + "ctx = new_context(fp)\n", + "fp.open_context(ctx)\n", + "# code block\n", + "fp.end_context(ctx)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Pseudo Code! Nicht ausführbar\n", + "\n", + "try:\n", + " fp = open(\"datei\")\n", + "except FileNotFoundError:\n", + " # code\n", + "except OSError:\n", + " # code" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "{\"foo\": 42, \"bar\": 23}\n", + "{\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n", + "[{\"foo\": 42, \"bar\": 23}, {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}]\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "data = json.loads(\"[1, 2, 3]\")\n", + "print(data)\n", + "d1 = {\"foo\": 42, \"bar\": 23}\n", + "print(json.dumps(d1))\n", + "d2 = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n", + "print(json.dumps(d2))\n", + "l = [d1, d2]\n", + "print(json.dumps(l))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "data = {\"foo\": 42, \"bar\": 23, \"baz\": [1, 2, 3]}\n", + "with open(\"data.json\", \"w\") as fp:\n", + " json.dump(data, fp)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"data.json\") as fp:\n", + " data2 = json.load(fp)\n", + "\n", + "assert data == data2, \"Not equal\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "softed", + "language": "python", + "name": "softed" + }, + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/dateien.py b/notebooks/dateien.py new file mode 100644 index 0000000..08e3125 --- /dev/null +++ b/notebooks/dateien.py @@ -0,0 +1,99 @@ +# %% [markdown] +# # Dateien + +# %% +fp = open("../uebungen/salaries.txt", encoding="utf-8") +content = fp.read() +print(type(content)) + + +# %% +fp = open("../uebungen/salaries.txt", "rb") +content = fp.read() +print(type(content)) +text = content.decode("utf-8") +print(type(text)) + +# %% +fp = open("../uebungen/salaries.txt", mode="r") +line = fp.readline() +print(repr(line)) + +# %% +fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") +while True: + line = fp.readline() + if not line: + break + print(line, end="") + +# %% +fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") +lines = fp.readlines() +print(type(lines)) +print(lines) + +# %% +fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") +for line in fp: + print(line, end="") + +# %% +fp = open("../uebungen/salaries.txt", mode="r", encoding="utf-8") +for line in fp: + print(line, end="") + +fp.close() + +# %% +lines = open("../uebungen/salaries.txt", encoding="utf-8").readlines() + +# %% +with open("../uebungen/salaries.txt") as fp: + for line in fp: + print(line, end="") + +# %% +# Pseudo Code! Nicht ausführbar +fp = open("../uebungen/salaries.txt") +ctx = new_context(fp) +fp.open_context(ctx) +# code block +fp.end_context(ctx) + +# %% +# Pseudo Code! Nicht ausführbar + +try: + fp = open("datei") +except FileNotFoundError: + # code +except OSError: + # code + +# %% +import json + +data = json.loads("[1, 2, 3]") +print(data) +d1 = {"foo": 42, "bar": 23} +print(json.dumps(d1)) +d2 = {"foo": 42, "bar": 23, "baz": [1, 2, 3]} +print(json.dumps(d2)) +l = [d1, d2] +print(json.dumps(l)) + + +# %% +data = {"foo": 42, "bar": 23, "baz": [1, 2, 3]} +with open("data.json", "w") as fp: + json.dump(data, fp) + + +# %% +with open("data.json") as fp: + data2 = json.load(fp) + +assert data == data2, "Not equal" + + diff --git a/notebooks/installation.ipynb b/notebooks/installation.ipynb deleted file mode 100644 index e69de29..0000000 diff --git a/notebooks/objects.html b/notebooks/objects.html new file mode 100644 index 0000000..a862bd6 --- /dev/null +++ b/notebooks/objects.html @@ -0,0 +1,7923 @@ + + + + + +objects + + + + + + + + + + + + +
+ + +
+ + diff --git a/notebooks/objects.ipynb b/notebooks/objects.ipynb new file mode 100644 index 0000000..947db91 --- /dev/null +++ b/notebooks/objects.ipynb @@ -0,0 +1,337 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Objekte und OO-Programmierung" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```py\n", + "# Ausgangslage\n", + "employees = {\n", + " \"name 1\": {\n", + " \"salary\": ...,\n", + " \"skills\": [...],\n", + " },\n", + " \"name 2\": {\n", + " \"salary\": ...,\n", + " \"skills\": [...],\n", + " },\n", + " ...\n", + "}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unknown\n", + "0\n", + "[]\n", + "Unknown\n", + "0\n", + "[]\n" + ] + } + ], + "source": [ + "class Employee:\n", + " name = \"Unknown\"\n", + " gehalt = 0\n", + " skills = []\n", + "\n", + "employee1 = Employee()\n", + "print(employee1.name)\n", + "print(employee1.gehalt)\n", + "print(employee1.skills)\n", + "\n", + "employee2 = Employee()\n", + "print(employee2.name)\n", + "print(employee2.gehalt)\n", + "print(employee2.skills)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Joe Doe\n", + "Unknown\n" + ] + } + ], + "source": [ + "employee1.name = \"Joe Doe\"\n", + "print(employee1.name)\n", + "print(employee2.name)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Init!\n" + ] + } + ], + "source": [ + "class Employee:\n", + " def __init__(self):\n", + " print(\"Init!\")\n", + " \n", + "employee3 = Employee()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Joe Doe\n", + "2000\n", + "[]\n", + "Jane Oh\n", + "3000\n", + "['marketing']\n" + ] + } + ], + "source": [ + "class Employee:\n", + " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", + " self.name = name\n", + " self.gehalt = gehalt\n", + "\n", + " if skills is None:\n", + " self.skills = []\n", + " else:\n", + " self.skills = skills\n", + "\n", + "employee4 = Employee(\"Joe Doe\", 2000, [])\n", + "print(type(employee4))\n", + "print(employee4.name)\n", + "print(employee4.gehalt)\n", + "print(employee4.skills)#\n", + "\n", + "employee5 = Employee(\"Jane Oh\", 3000, [\"marketing\"])\n", + "print(employee5.name)\n", + "print(employee5.gehalt)\n", + "print(employee5.skills)\n", + "\n", + "# Geht nicht, weil die Klasse kein Attribut \"name\" definiert\n", + "#print(Employee.name)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "0\n", + "[]\n" + ] + } + ], + "source": [ + "employee6 = Employee()\n", + "print(employee6.name)\n", + "print(employee6.gehalt)\n", + "print(employee6.skills)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Employee 'Bob Smith'\n", + " gehalt: 0\n", + " skills:\n", + " - software development\n", + " - it support\n" + ] + } + ], + "source": [ + "class Employee:\n", + " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", + " self.name = name\n", + " self.gehalt = gehalt\n", + "\n", + " if skills is None:\n", + " self.skills = []\n", + " else:\n", + " self.skills = skills\n", + "\n", + " def __str__(self):\n", + " s = [f\"Employee '{self.name}'\"]\n", + " s.append(f\" gehalt: {self.gehalt}\")\n", + " s.append(f\" skills:\")\n", + " for skill in self.skills:\n", + " s.append(f\" - {skill}\")\n", + " return \"\\n\".join(s)\n", + "\n", + "\"\"\"\n", + "Employee 'name':\n", + " gehalt: XXX\n", + " skills:\n", + " - skill 1\n", + " - skill 2\n", + "\"\"\"\n", + "employee7 = Employee(name=\"Bob Smith\", skills=[\"software development\", \"it support\"])\n", + "print(employee7)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hallo, Welt\n", + "Guten Tag, Welt\n", + "Welt, Guten Tag\n", + "Welt, Guten Tag\n", + "Guten Tag, Welt\n" + ] + } + ], + "source": [ + "# Exkurs: Strings und `format()`\n", + "\n", + "s = \"Hallo, {}\"\n", + "print(s.format(\"Welt\"))\n", + "\n", + "s = \"{}, {}\"\n", + "print(s.format(\"Guten Tag\", \"Welt\"))\n", + "\n", + "s = \"{name}, {grussformel}\"\n", + "print(s.format(grussformel=\"Guten Tag\", name=\"Welt\"))\n", + "\n", + "grussformel=\"Guten Tag\"\n", + "name=\"Welt\"\n", + "print(f\"{name}, {grussformel}\")\n", + "\n", + "\n", + "def sagehallo(name):\n", + " # 'grussformel' ist eine globale variable!\n", + " print(f\"{grussformel}, {name}\")\n", + "\n", + "sagehallo(\"Welt\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5000\n" + ] + }, + { + "ename": "ValueError", + "evalue": "Zu hohes gehalt!", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 22\u001b[0m \u001b[39mprint\u001b[39m(employee8\u001b[39m.\u001b[39mgehalt)\n\u001b[1;32m 23\u001b[0m \u001b[39m# ValueError!\u001b[39;00m\n\u001b[0;32m---> 24\u001b[0m employee8\u001b[39m.\u001b[39;49mset_gehalt(\u001b[39m4000\u001b[39;49m)\n\u001b[1;32m 25\u001b[0m \u001b[39m# TypeError!\u001b[39;00m\n\u001b[1;32m 26\u001b[0m employee8\u001b[39m.\u001b[39mset_gehalt(\u001b[39m\"\u001b[39m\u001b[39mein sack mehl\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m1\n\u001b[1;32m 15\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mgehalt \u001b[39m=\u001b[39m neues_gehalt\n\u001b[1;32m 16\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m---> 17\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mZu hohes gehalt!\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[0;31mValueError\u001b[0m: Zu hohes gehalt!" + ] + } + ], + "source": [ + "class Employee:\n", + " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", + " self.name = name\n", + " self.gehalt = gehalt\n", + "\n", + " if skills is None:\n", + " self.skills = []\n", + " else:\n", + " self.skills = skills\n", + "\n", + " def set_gehalt(self, neues_gehalt):\n", + " if not isinstance(neues_gehalt, int):\n", + " raise TypeError(\"gehalt muss ein Integer sein\")\n", + " if neues_gehalt < 4000:\n", + " self.gehalt = neues_gehalt\n", + " else:\n", + " raise ValueError(\"Zu hohes gehalt!\")\n", + "\n", + "employee8 = Employee()\n", + "# Ok\n", + "employee8.gehalt = 5000\n", + "print(employee8.gehalt)\n", + "# ValueError!\n", + "employee8.set_gehalt(4000)\n", + "# TypeError!\n", + "employee8.set_gehalt(\"ein sack mehl\")\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "softed", + "language": "python", + "name": "softed" + }, + "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.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/objects.py b/notebooks/objects.py new file mode 100644 index 0000000..46a1d98 --- /dev/null +++ b/notebooks/objects.py @@ -0,0 +1,160 @@ +# %% [markdown] +# # Objekte und OO-Programmierung + +# %% [markdown] +# ```py +# # Ausgangslage +# employees = { +# "name 1": { +# "salary": ..., +# "skills": [...], +# }, +# "name 2": { +# "salary": ..., +# "skills": [...], +# }, +# ... +# } +# ``` + +# %% +class Employee: + name = "Unknown" + gehalt = 0 + skills = [] + +employee1 = Employee() +print(employee1.name) +print(employee1.gehalt) +print(employee1.skills) + +employee2 = Employee() +print(employee2.name) +print(employee2.gehalt) +print(employee2.skills) + +# %% +employee1.name = "Joe Doe" +print(employee1.name) +print(employee2.name) + +# %% +class Employee: + def __init__(self): + print("Init!") + +employee3 = Employee() + +# %% +class Employee: + def __init__(self, name="", gehalt=0, skills=[]): + self.name = name + self.gehalt = gehalt + + if skills is None: + self.skills = [] + else: + self.skills = skills + +employee4 = Employee("Joe Doe", 2000, []) +print(type(employee4)) +print(employee4.name) +print(employee4.gehalt) +print(employee4.skills)# + +employee5 = Employee("Jane Oh", 3000, ["marketing"]) +print(employee5.name) +print(employee5.gehalt) +print(employee5.skills) + +# Geht nicht, weil die Klasse kein Attribut "name" definiert +#print(Employee.name) + +# %% +employee6 = Employee() +print(employee6.name) +print(employee6.gehalt) +print(employee6.skills) + +# %% +class Employee: + def __init__(self, name="", gehalt=0, skills=[]): + self.name = name + self.gehalt = gehalt + + if skills is None: + self.skills = [] + else: + self.skills = skills + + def __str__(self): + s = [f"Employee '{self.name}'"] + s.append(f" gehalt: {self.gehalt}") + s.append(f" skills:") + for skill in self.skills: + s.append(f" - {skill}") + return "\n".join(s) + +""" +Employee 'name': + gehalt: XXX + skills: + - skill 1 + - skill 2 +""" +employee7 = Employee(name="Bob Smith", skills=["software development", "it support"]) +print(employee7) + +# %% +# Exkurs: Strings und `format()` + +s = "Hallo, {}" +print(s.format("Welt")) + +s = "{}, {}" +print(s.format("Guten Tag", "Welt")) + +s = "{name}, {grussformel}" +print(s.format(grussformel="Guten Tag", name="Welt")) + +grussformel="Guten Tag" +name="Welt" +print(f"{name}, {grussformel}") + + +def sagehallo(name): + # 'grussformel' ist eine globale variable! + print(f"{grussformel}, {name}") + +sagehallo("Welt") + +# %% +class Employee: + def __init__(self, name="", gehalt=0, skills=[]): + self.name = name + self.gehalt = gehalt + + if skills is None: + self.skills = [] + else: + self.skills = skills + + def set_gehalt(self, neues_gehalt): + if not isinstance(neues_gehalt, int): + raise TypeError("gehalt muss ein Integer sein") + if neues_gehalt < 4000: + self.gehalt = neues_gehalt + else: + raise ValueError("Zu hohes gehalt!") + +employee8 = Employee() +# Ok +employee8.gehalt = 5000 +print(employee8.gehalt) +# ValueError! +employee8.set_gehalt(4000) +# TypeError! +employee8.set_gehalt("ein sack mehl") + + +