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 { -
+
@@ -7543,15 +7543,36 @@ a.anchor-link { a = 42 b = 23 +c = 0x20 +d = 0b1110101 +#e = calculate() +f = 1_000_000 print(a) print(b) print(a + b) +print(a - b)
-
+
+ + +
+
@@ -7566,13 +7587,32 @@ a.anchor-link { print(pi) print(type(pi)) print(e) -print(pi * e) +x = pi * e +print(x) +print(type(x))
-
+
+ + +
+
@@ -7584,8 +7624,9 @@ a.anchor-link { i = 3 f = 1.5 -summe = a + f +summe = i + f print(i, f, summe) +print(type(summe)) print(type(e)) z = 1 n = 6 @@ -7595,7 +7636,23 @@ a.anchor-link {
-
+
+ + +
+
@@ -7621,7 +7678,27 @@ a.anchor-link {
-
+
+ + +
+
@@ -7644,7 +7721,24 @@ a.anchor-link {
-
+
+ + +
+
@@ -7659,11 +7753,38 @@ a.anchor-link { rest = x % 3 print(rest) print(type(rest)) +f = 1.2345 +i = int(f) +print(f, i) +print(f % 1) +print(int(-2.3456)) +print(round(2.3455, 2)) +import math +print(math.floor(2.345))
+
+
+
+
+ +
+
@@ -7675,7 +7796,7 @@ a.anchor-link {
-
+
@@ -7691,7 +7812,7 @@ a.anchor-link { print(s3a) s3b = "Oder 'andersherum'." print(s3b) -s4 = "Alternativ kann man eine Backslash als \"Escape\"-Zeichen verwenden" +s4 = "Alternativ kann man eine Backslash \'als\' \"Escape\"-Zeichen verwenden" print(s4) s5 = "String-Literale" ", die direkt hintereinander" ", durch Leerzeichen getrennt, stehen" "werden zusammengefügt." print(s5) @@ -7699,12 +7820,45 @@ a.anchor-link { "über mehrere Zeilen " "hinweg") print(s6) +l = [ + "joe", + "bob", + "alice", + "jenny" +] +print(l) +print(l) +print(l) +print(l)
-
+
+ + +
+
@@ -7714,23 +7868,46 @@ a.anchor-link {
# Längere Strings
 
-long_string = """Dies ist ein langer String ("triple quoted string").
-Er kann Zeilenumbrüche enthalten.
+long_string = '''Dies ist ein langer String ("triple quoted string").
+Er kann Zeilenumbrüche enthalten.
 
-Und Leerzeilen.
+Und Leerzeilen.
 
-Und auch 'einfache' oder "doppelte" Anführungszeichen.
+Und auch 'einfache' oder "doppelte" Anführungszeichen.
 
-Er endet mit drei weiteren Anführungszeichen des gleichen Typs.
-"""
+Er endet mit drei weiteren Anführungszeichen des gleichen Typs.
+'''
 print(long_string)
 print(repr(long_string))
+s = 'Dies ist ein langer String ("triple quoted string").\nEr kann Zeilenumbrüche enthalten.\n\nUnd Leerzeilen.\n\nUnd auch \'einfache\' oder "doppelte" Anführungszeichen.\n\nEr endet mit drei weiteren Anführungszeichen des gleichen Typs.\n'
 
-
+
+ + +
+
@@ -7755,7 +7932,28 @@ a.anchor-link {
-
+
+ + +
+
@@ -7776,7 +7974,21 @@ a.anchor-link {
-
+
+ + +
+
@@ -7794,7 +8006,23 @@ a.anchor-link {
-
+
+ + +
+
@@ -7805,12 +8033,68 @@ a.anchor-link {
hallo = "Hallo"
 print(hallo)
 print(hallo[1])
-print(hallo[0] + 'e' + hallo[2:])
+hello = hallo[0] + 'e' + hallo[2:]
+print(hello)
+print("hello" * 3)
 
+
+
+
+
+ +
+
+
+
+ + +
+
+ + +
-
+
@@ -7558,7 +7558,24 @@ a.anchor-link {
-
+
+ + +
+
@@ -7585,7 +7602,24 @@ a.anchor-link {
-
+
+ + +
+
@@ -7607,7 +7641,22 @@ a.anchor-link {
-
+
+ + +
+
@@ -7623,6 +7672,20 @@ a.anchor-link {
+
+
+
+
+ +
+
@@ -7634,7 +7697,7 @@ a.anchor-link {
-
+
@@ -7652,7 +7715,23 @@ a.anchor-link {
-
+
+ + +
+
@@ -7667,6 +7746,25 @@ a.anchor-link {
+
+
+
+
+ +
+
+
+
+ + +
+
+ +
@@ -8047,7 +8169,7 @@ a.anchor-link {
In [ ]:
-
# Keys müsen keine Strings, aber immutable sein
+
# Keys müssen keine Strings, aber immutable sein
 # => Integers, Floats, Tuples sind ok
 
 numbers = {1: "one", 2: "two", 3: "three"}
@@ -8063,10 +8185,8 @@ a.anchor-link {
 
 for row in range(6):
     for col in range(10):
-        if (row, col) in pixels:
-            print(pixels[(row, col)], end="")
-        else:
-            print(".", end="")
+        value = pixels.get((row, col), ".")
+        print(value, end="")
     print()
 
diff --git a/notebooks/containertypes.ipynb b/notebooks/containertypes.ipynb index 35161eb..b0757f1 100644 --- a/notebooks/containertypes.ipynb +++ b/notebooks/containertypes.ipynb @@ -16,9 +16,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 10, 20, 30, 50]\n", + "[0.5, 0.6, 0.8, 1.0, 1.2]\n", + "['Joe', 'Alice', 'Bob', 'Charly']\n", + "[1, 'Joe', 1.4, 'Alice', 100]\n", + "['one'] [] []\n" + ] + } + ], "source": [ "# Listenliterale\n", "\n", @@ -38,9 +50,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(5, 10, 20, 30, 50)\n", + "(0.5, 0.6, 0.8, 1.0, 1.2)\n", + "('Joe', 'Alice', 'Bob', 'Charly')\n", + "(1, 'Joe', 1.4, 'Alice', 100)\n", + "('one',) ('one',) () ()\n" + ] + } + ], "source": [ "# Tuplelliterale\n", "values = (5, 10, 20, 30, 50)\n", @@ -60,9 +84,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5] (1, 2, 3, 4, 5)\n", + "[10, 20, 30, 40, 50] (1, 2, 3, 4, 5)\n", + "[1, 2, 3, 4, 5]\n" + ] + } + ], "source": [ "# Umwandlung\n", "\n", @@ -77,9 +111,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['h', 'a', 'l', 'l', 'o']\n", + "('h', 'a', 'l', 'l', 'o')\n" + ] + } + ], "source": [ "s = \"hallo\"\n", "print(list(s))\n", @@ -95,9 +138,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Frühling\n", + "Herbst\n", + "Winter\n", + "Sommer\n" + ] + } + ], "source": [ "seasons = [\"Frühling\", \"Sommer\", \"Herbst\", \"Winter\"]\n", "print(seasons[0]) # Erstes Element hat Index 0!\n", @@ -108,9 +162,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb Cell 9\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 1\u001b[0m \u001b[39m# Ungültige Indizes führen zu einem Fehler (IndexError Exception)\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mprint\u001b[39m(seasons[\u001b[39m4\u001b[39;49m]) \u001b[39m# 4 Elemente => gültige Indizes 0..3\u001b[39;00m\n", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ], "source": [ "# Ungültige Indizes führen zu einem Fehler (IndexError Exception)\n", "print(seasons[4]) # 4 Elemente => gültige Indizes 0..3" @@ -179,6 +245,7 @@ "outputs": [], "source": [ "l = list(range(10)) # l = [0, 1, ..., 9]\n", + "print(l)\n", "print(l[2:4]) # Start ist inklusiv, Ende nicht!" ] }, @@ -214,11 +281,13 @@ "source": [ "# Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples\n", "\n", - "l = [1, 2, 3]\n", - "l2 = l[:]\n", - "l[0] = 999\n", - "print(l)\n", - "print(l2)" + "l1 = [1, 2, 3]\n", + "l2 = l1[:]\n", + "l3 = l1\n", + "l1[0] = 999\n", + "print(l1)\n", + "print(l2)\n", + "print(l3)" ] }, { @@ -280,7 +349,22 @@ "t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie!\n", "print(t)\n", "print(t2)\n", - "print(t is t2)\n" + "print(t is t2)\n", + "s = \"ein string\"\n", + "s += \" und noch mehr\"\n", + "i = 0\n", + "i += 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "l = list(range(10))\n", + "import sys\n", + "sys.getsizeof(l)" ] }, { @@ -341,7 +425,8 @@ "\n", "scores[\"white team\"] = 45\n", "scores[\"rainbow team\"] = 50\n", - "print(scores)" + "print(scores)\n", + "del scores[\"blue team\"]" ] }, { @@ -391,7 +476,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Keys müsen keine Strings, aber immutable sein\n", + "# Keys müssen keine Strings, aber immutable sein\n", "# => Integers, Floats, Tuples sind ok\n", "\n", "numbers = {1: \"one\", 2: \"two\", 3: \"three\"}\n", @@ -407,10 +492,8 @@ "\n", "for row in range(6):\n", " for col in range(10):\n", - " if (row, col) in pixels:\n", - " print(pixels[(row, col)], end=\"\")\n", - " else:\n", - " print(\".\", end=\"\")\n", + " value = pixels.get((row, col), \".\")\n", + " print(value, end=\"\")\n", " print()\n" ] }, diff --git a/notebooks/containertypes.py b/notebooks/containertypes.py index 75fb3a6..18e8753 100644 --- a/notebooks/containertypes.py +++ b/notebooks/containertypes.py @@ -97,6 +97,7 @@ t[2] = "drei" # %% l = list(range(10)) # l = [0, 1, ..., 9] +print(l) print(l[2:4]) # Start ist inklusiv, Ende nicht! # %% @@ -114,11 +115,13 @@ print(l[:3]) # auch hier ist der Ende-Index nicht-inklusiv! # %% # Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples -l = [1, 2, 3] -l2 = l[:] -l[0] = 999 -print(l) +l1 = [1, 2, 3] +l2 = l1[:] +l3 = l1 +l1[0] = 999 +print(l1) print(l2) +print(l3) # %% # Zuweisung an Listenslices ist möglich! @@ -158,8 +161,17 @@ t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie! print(t) print(t2) print(t is t2) +s = "ein string" +s += " und noch mehr" +i = 0 +i += 1 +# %% +l = list(range(10)) +import sys +sys.getsizeof(l) + # %% [markdown] # ## Dictionaries @@ -192,6 +204,7 @@ print("The red team scored:", scores["red team"]) scores["white team"] = 45 scores["rainbow team"] = 50 print(scores) +del scores["blue team"] # %% # Umwandlung @@ -217,7 +230,7 @@ for key, value in scores.items(): print("Das", key, "hat", value, "Punkte.") # %% -# Keys müsen keine Strings, aber immutable sein +# Keys müssen keine Strings, aber immutable sein # => Integers, Floats, Tuples sind ok numbers = {1: "one", 2: "two", 3: "three"} @@ -233,10 +246,8 @@ pixels = { for row in range(6): for col in range(10): - if (row, col) in pixels: - print(pixels[(row, col)], end="") - else: - print(".", end="") + value = pixels.get((row, col), ".") + print(value, end="") print() diff --git a/notebooks/controlflow.html b/notebooks/controlflow.html index 0f17ba4..ad2f846 100644 --- a/notebooks/controlflow.html +++ b/notebooks/controlflow.html @@ -7531,7 +7531,7 @@ a.anchor-link {
-
+
@@ -7554,7 +7554,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7577,7 +7590,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7606,6 +7632,19 @@ a.anchor-link {
+
+
+
+
+ +
+
@@ -7617,7 +7656,7 @@ a.anchor-link {
-
+
@@ -7634,7 +7673,24 @@ a.anchor-link {
-
+
+ + +
+
@@ -7651,7 +7707,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7667,7 +7736,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7687,7 +7769,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7706,7 +7801,28 @@ a.anchor-link {
-
+
+ + +
+
@@ -7733,7 +7849,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7754,7 +7883,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7773,6 +7915,19 @@ a.anchor-link {
+
+
+
+
+ +
+
@@ -7784,7 +7939,7 @@ a.anchor-link {
-
+
@@ -7803,7 +7958,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7820,7 +7988,21 @@ a.anchor-link {
-
+
+ + +
+
@@ -7839,7 +8021,20 @@ a.anchor-link {
-
+
+ + +
+
@@ -7856,7 +8051,21 @@ a.anchor-link {
-
+
+ + +
+
@@ -7871,6 +8080,23 @@ a.anchor-link {
+
+
+
+
+ +
+
@@ -7882,7 +8108,7 @@ a.anchor-link {
-
+
@@ -7908,7 +8134,27 @@ a.anchor-link {
-
+
+ + +
+
@@ -7927,7 +8173,23 @@ a.anchor-link {
-
+
+ + +
+
@@ -7945,7 +8207,22 @@ a.anchor-link {
-
+
+ + +
+
@@ -7964,7 +8241,23 @@ a.anchor-link {
-
+
+ + +
+
@@ -7987,6 +8280,28 @@ 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") + + +