Add notebooks for day 1 part 1

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-05-05 17:36:25 +02:00
parent bbfee09be4
commit 2fec50720d
2 changed files with 1005 additions and 0 deletions

420
notebooks/basictypes.ipynb Normal file
View File

@ -0,0 +1,420 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Grundlegende Datentypen und Operationen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Numerische Typen"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42\n",
"23\n",
"65\n"
]
}
],
"source": [
"# Ganzzahlen (Integers)\n",
"\n",
"a = 42\n",
"b = 23\n",
"print(a)\n",
"print(b)\n",
"print(a + b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.14159\n",
"2.71828\n",
"8.539721265199999\n"
]
}
],
"source": [
"# Fließkommazahlen (Floats)\n",
"\n",
"pi = 3.14159\n",
"e = 2.71828\n",
"print(pi)\n",
"print(type(pi))\n",
"print(e)\n",
"print(pi * e)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 1.5 4.5\n",
"<class 'float'>\n",
"0.16666666666666666\n"
]
}
],
"source": [
"# Implizite Umwandlung von numerischen Typen\n",
"\n",
"i = 3\n",
"f = 1.5\n",
"summe = a + f\n",
"print(i, f, summe)\n",
"print(type(e))\n",
"z = 1\n",
"n = 6\n",
"print(z / n)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"999\n",
"144\n",
"8.0\n",
"3.8\n",
"1.0\n",
"23.799999999999997\n",
"0.3333333333333333\n"
]
}
],
"source": [
"# Grundrechenarten\n",
"\n",
"# Integer\n",
"print(1 + 1)\n",
"print(1_000 - 1)\n",
"print(12 * 12) # Vorsicht: Asterisk (\"Sternchen\"), nicht x)\n",
"print(64 / 8)\n",
"\n",
"# Floats\n",
"print(0.5 + 3.3)\n",
"print(1.99 - 0.99)\n",
"print(20.0 * 1.19)\n",
"print(1.0 / 3.0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"64\n",
"512\n",
"512\n",
"11585.237502960395\n"
]
}
],
"source": [
"# X hoch Y\n",
"\n",
"x = 8\n",
"x2 = x ** 2\n",
"print(x2)\n",
"print(x ** 3)\n",
"print(pow(x, 3))\n",
"\n",
"print(x ** e)\n",
"print(pow(x, e)) # pow akzeptiert integer und floats"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"<class 'int'>\n"
]
}
],
"source": [
"# Modulo\n",
"\n",
"x = 10\n",
"y = 3\n",
"rest = x % 3\n",
"print(rest)\n",
"print(type(rest))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Strings"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist ein String!\n",
"Man kann einfache oder doppelte Anführungszeichen verwenden.\n",
"Innerhalb der Anführungszeichen kann man den jeweils \"anderen Typ\" verwenden\n",
"Oder 'andersherum'.\n",
"Alternativ kann man eine Backslash als \"Escape\"-Zeichen verwenden\n",
"String-Literale, die direkt hintereinander, durch Leerzeichen getrennt, stehenwerden zusammengefügt.\n",
"Mit Klammerung klapt das auch über mehrere Zeilen hinweg\n"
]
}
],
"source": [
"s = \"Dies ist ein String!\"\n",
"print(s)\n",
"s2 = 'Man kann einfache oder doppelte Anführungszeichen verwenden.'\n",
"print(s2)\n",
"s3a = 'Innerhalb der Anführungszeichen kann man den jeweils \"anderen Typ\" verwenden'\n",
"print(s3a)\n",
"s3b = \"Oder 'andersherum'.\"\n",
"print(s3b)\n",
"s4 = \"Alternativ kann man eine Backslash als \\\"Escape\\\"-Zeichen verwenden\"\n",
"print(s4)\n",
"s5 = \"String-Literale\" \", die direkt hintereinander\" \", durch Leerzeichen getrennt, stehen\" \"werden zusammengefügt.\"\n",
"print(s5)\n",
"s6 = (\"Mit Klammerung klappt das auch \"\n",
" \"über mehrere Zeilen \"\n",
" \"hinweg\")\n",
"print(s6)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist ein langer String (\"triple quoted string\").\n",
"Er kann Zeilenumbrüche enthalten.\n",
"\n",
"Und Leerzeilen.\n",
"\n",
"Und auch 'einfache' oder \"doppelte\" Anführungszeichen.\n",
"\n",
"Er endet mit drei weiteren Anführungszeichen des gleichen Typs.\n",
"\n",
"'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'\n"
]
}
],
"source": [
"# Längere Strings\n",
"\n",
"long_string = \"\"\"Dies ist ein langer String (\"triple quoted string\").\n",
"Er kann Zeilenumbrüche enthalten.\n",
"\n",
"Und Leerzeilen.\n",
"\n",
"Und auch 'einfache' oder \"doppelte\" Anführungszeichen.\n",
"\n",
"Er endet mit drei weiteren Anführungszeichen des gleichen Typs.\n",
"\"\"\"\n",
"print(long_string)\n",
"print(repr(long_string))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Erste Zeile\n",
"Zweite Zeile\n",
"Eine Zeile\n",
"Noch eine Zeile\n",
"Eins\tZwei\tDrei\n",
"1\t2\t3\n",
"'Dieser String enthält ein\\x00Null-Zeichen'\n",
"Hexadezimal-Codes können wir druckbare (e.g. '@') und nicht druckbare Zeichen (e.g. ) verwendet werden.\n",
"❤\n"
]
}
],
"source": [
"# Escape Sequenzen\n",
"\n",
"# Zeilenumbrüche und Tabs\n",
"print(\"Erste Zeile\\r\\nZweite Zeile\") # Zeilenumbruch nach Windows-Art \n",
"print(\"Eine Zeile\\nNoch eine Zeile\") # Rest der Welt ;-)\n",
"print(\"Eins\\tZwei\\tDrei\")\n",
"print(\"1\\t2\\t3\")\n",
"\n",
"# Null und Hexadezimal- und Unicode-Sequenzen\n",
"print(repr(\"Dieser String enthält ein\\0Null-Zeichen\"))\n",
"print(\"Hexadezimal-Codes können wir druckbare (e.g. '\\x40') und nicht druckbare Zeichen (e.g. \\x7F) verwendet werden.\")\n",
"print('\\u2764') # Unicode"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dies ist der erste String.Und dies der zweite.\n",
"Ich habe es dir schon dreimal gesagt!Ich habe es dir schon dreimal gesagt!Ich habe es dir schon dreimal gesagt!\n"
]
}
],
"source": [
"# String-Operationen\n",
"\n",
"s1 = \"Dies ist der erste String.\"\n",
"s2 = \"Und dies der zweite.\"\n",
"s3 = s1 + s2\n",
"print(s3)\n",
"s4 = \"Ich habe es dir schon dreimal gesagt! \"\n",
"print(s4 * 3)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SCHREI NICHT SO LAUT!\n",
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"klein = \"schrei nicht so laut!\"\n",
"print(klein.upper())\n",
"print(\"hallo\" == \"HALLO\".lower())\n",
"print(\"hallo\" == \"Hallo\".lower())\n",
"print(\"straße\".upper() == \"STRASSE\")\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo\n",
"a\n",
"Hello\n"
]
}
],
"source": [
"hallo = \"Hallo\"\n",
"print(hallo)\n",
"print(hallo[1])\n",
"print(hallo[0] + 'e' + hallo[2:])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'str' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb Cell 16\u001b[0m line \u001b[0;36m3\n\u001b[1;32m <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X23sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39m# Aber das geht nicht:\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/basictypes.ipynb#X23sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m hallo[\u001b[39m1\u001b[39;49m] \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39me\u001b[39m\u001b[39m'\u001b[39m\n",
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
]
}
],
"source": [
"# Aber das geht nicht!:\n",
"\n",
"hallo[1] = 'e'"
]
}
],
"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
}

585
notebooks/controlflow.ipynb Normal file
View File

@ -0,0 +1,585 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Kontrollstrukturen und Vergleichsoperatoren"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Verzweigung (Branching)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hmm, ob das so stimmt?\n"
]
}
],
"source": [
"# if / else / elif\n",
"\n",
"alter = -1\n",
"\n",
"if alter >= 16:\n",
" print(\"Nicht vergessen: am 9.6.2024 ist Europa-Wahl!\")\n",
"elif alter > 0:\n",
" print(\"Schön, dass du geboren bist!\")\n",
"else:\n",
" print(\"Hmm, ob das so stimmt?\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nach Süden, nach Süden!\n"
]
}
],
"source": [
"richtung = \"s\"\n",
"\n",
"if richtung == \"n\":\n",
" print(\"Auf in den hohen Norden!\")\n",
"elif richtung == \"s\":\n",
" print(\"Nach Süden, nach Süden!\")\n",
"elif richtung == \"w\":\n",
" print(\"Go west!\")\n",
"elif richtung == \"o\":\n",
" print(\"Bin schon da.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Auf in den hohen Norden!\n"
]
}
],
"source": [
"# match\n",
"\n",
"# richtung = input(\"Wo soll's denn lang gehen? \")\n",
"richtung = \"n\"\n",
"\n",
"match richtung:\n",
" case \"n\" | \"norden\":\n",
" print(\"Auf in den hohen Norden!\")\n",
" case \"s\" | \"süden\":\n",
" print(\"Nach Süden, nach Süden!\")\n",
" case \"w\" | \"westen\":\n",
" print(\"Go west!\")\n",
" case \"o\" | \"osten\":\n",
" print(\"Bin schon da.\")\n",
" case _:\n",
" print(\"Das Glück liegt manchmal abseits des direkten Wegs!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Schleifen"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"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",
"for i in range(5):\n",
" print(\"Schleifendurchlauf Nr. \", i)\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Summer der Zahlen 1-10: 55\n"
]
}
],
"source": [
"result = 0\n",
"for i in range(1, 11):\n",
" result += i\n",
"print(\"Summe der Zahlen 1-10:\", result)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 passt in die Reihe\n"
]
}
],
"source": [
"for i in (2, 4, 6, 8, 9, 10):\n",
" if i % 2:\n",
" print(i, \"passt nicht in die Reihe\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 ... 9 ... 8 ... 7 ... 6 ... 5 ... 4 ... 3 ... 2 ... 1 ... Lift off!\n"
]
}
],
"source": [
"# while\n",
"\n",
"i = 10\n",
"while i > 0: # or: while True:\n",
" print(i, \"... \", end=\"\")\n",
" i -= 1\n",
"print(\"Lift off!\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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",
"for i in range(10, 20):\n",
" if i == 13:\n",
" continue\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration: 142\n"
]
}
],
"source": [
"# break\n",
"\n",
"i = 0\n",
"result = 0\n",
"maximum = 10_000\n",
"\n",
"while True:\n",
" result += i\n",
"\n",
" if result >= maximum:\n",
" print(\"Iteration: \", i+1)\n",
" break\n",
"\n",
" i += 1\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
]
}
],
"source": [
"alphabet = \"abcdefghifklmnopqrstuvwxyz\"\n",
"buchstabe = \"q\"\n",
"i = 0\n",
"for char in alphabet:\n",
" i += 1\n",
" if char == buchstabe:\n",
" print(buchstabe, \"ist Buchstabe Nr.\", i, \"des dt. Alphabets.\")\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
]
}
],
"source": [
"# einfacher:\n",
"\n",
"for i, char in enumerate(alphabet):\n",
" if char == buchstabe:\n",
" print(buchstabe, \"ist Buchstabe Nr.\", i + 1, \"des dt. Alphabets.\")\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Funktionen"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"256 256 100\n"
]
}
],
"source": [
"# Aufruf eingebauter Funktionen\n",
"\n",
"x = pow(2, 8)\n",
"y = max(x, 100)\n",
"z = min(max(0, x), 100)\n",
"print(x, y, z)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n"
]
}
],
"source": [
"# optionale Parameter\n",
"\n",
"print()\n",
"print(\"\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"# eigene Funktionen\n",
"\n",
"def add(a, b):\n",
" return a + b\n",
"\n",
"print(add(2, 3))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"24\n"
]
}
],
"source": [
"# Keyword arguments\n",
"\n",
"print(add(a=5, b=10))\n",
"print(add(4, b=20))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "positional argument follows keyword argument (1525390589.py, line 2)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[21], 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))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vergleichsausdrücke"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"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",
"c = 2\n",
"d = 5 * a\n",
"\n",
"print(a == b)\n",
"print(a != d)\n",
"print(a > b)\n",
"print(a > c)\n",
"print(a >= c)\n",
"print(c > a)\n",
"print(c < b)\n",
"print(c >= 2)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"False\n"
]
}
],
"source": [
"# Negation\n",
"\n",
"print(not c > a)\n",
"print(not a == b) # klarer: a != b\n",
"print(not b != d) # klarer: b == d\n",
"print(not a > c)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"# logische Kombination\n",
"\n",
"print(b > a and a > c)\n",
"print(a > b or a > c)\n",
"print(not a < c and b >= d)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"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"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"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",
"print(bool(-1))\n",
"print(bool(0.0))\n",
"print(bool(0.1))\n",
"print(bool(-0.1))\n",
"print(bool(\"True\"))\n",
"print(bool(\"False\")) # !!!\n",
"print(bool(\"\"))\n",
"print(bool(\"\\0\"))\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
}