Export notebooks to HTML and Python scripts
This commit is contained in:
parent
d5d776522b
commit
19c2c91fe1
File diff suppressed because one or more lines are too long
|
@ -16,19 +16,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"42\n",
|
||||
"23\n",
|
||||
"65\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Ganzzahlen (Integers)\n",
|
||||
"\n",
|
||||
|
@ -41,19 +31,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3.14159\n",
|
||||
"2.71828\n",
|
||||
"8.539721265199999\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Fließkommazahlen (Floats)\n",
|
||||
"\n",
|
||||
|
@ -67,19 +47,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3 1.5 4.5\n",
|
||||
"<class 'float'>\n",
|
||||
"0.16666666666666666\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Implizite Umwandlung von numerischen Typen\n",
|
||||
"\n",
|
||||
|
@ -95,24 +65,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Grundrechenarten\n",
|
||||
"\n",
|
||||
|
@ -131,20 +86,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"64\n",
|
||||
"512\n",
|
||||
"512\n",
|
||||
"11585.237502960395\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# X hoch Y\n",
|
||||
"\n",
|
||||
|
@ -160,18 +104,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"<class 'int'>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Modulo\n",
|
||||
"\n",
|
||||
|
@ -191,23 +126,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"s = \"Dies ist ein String!\"\n",
|
||||
"print(s)\n",
|
||||
|
@ -229,26 +150,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Längere Strings\n",
|
||||
"\n",
|
||||
|
@ -267,25 +171,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Escape Sequenzen\n",
|
||||
"\n",
|
||||
|
@ -303,18 +191,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# String-Operationen\n",
|
||||
"\n",
|
||||
|
@ -328,20 +207,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SCHREI NICHT SO LAUT!\n",
|
||||
"True\n",
|
||||
"True\n",
|
||||
"True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"klein = \"schrei nicht so laut!\"\n",
|
||||
"print(klein.upper())\n",
|
||||
|
@ -352,19 +220,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hallo\n",
|
||||
"a\n",
|
||||
"Hello\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"hallo = \"Hallo\"\n",
|
||||
"print(hallo)\n",
|
||||
|
@ -374,21 +232,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Aber das geht nicht!:\n",
|
||||
"\n",
|
||||
|
|
|
@ -0,0 +1,153 @@
|
|||
# %% [markdown]
|
||||
# # Grundlegende Datentypen und Operationen
|
||||
|
||||
# %% [markdown]
|
||||
# ## Numerische Typen
|
||||
|
||||
# %%
|
||||
# Ganzzahlen (Integers)
|
||||
|
||||
a = 42
|
||||
b = 23
|
||||
print(a)
|
||||
print(b)
|
||||
print(a + b)
|
||||
|
||||
# %%
|
||||
# Fließkommazahlen (Floats)
|
||||
|
||||
pi = 3.14159
|
||||
e = 2.71828
|
||||
print(pi)
|
||||
print(type(pi))
|
||||
print(e)
|
||||
print(pi * e)
|
||||
|
||||
# %%
|
||||
# Implizite Umwandlung von numerischen Typen
|
||||
|
||||
i = 3
|
||||
f = 1.5
|
||||
summe = a + f
|
||||
print(i, f, summe)
|
||||
print(type(e))
|
||||
z = 1
|
||||
n = 6
|
||||
print(z / n)
|
||||
|
||||
# %%
|
||||
# Grundrechenarten
|
||||
|
||||
# Integer
|
||||
print(1 + 1)
|
||||
print(1_000 - 1)
|
||||
print(12 * 12) # Vorsicht: Asterisk ("Sternchen"), nicht x)
|
||||
print(64 / 8)
|
||||
|
||||
# Floats
|
||||
print(0.5 + 3.3)
|
||||
print(1.99 - 0.99)
|
||||
print(20.0 * 1.19)
|
||||
print(1.0 / 3.0)
|
||||
|
||||
# %%
|
||||
# X hoch Y
|
||||
|
||||
x = 8
|
||||
x2 = x ** 2
|
||||
print(x2)
|
||||
print(x ** 3)
|
||||
print(pow(x, 3))
|
||||
|
||||
print(x ** e)
|
||||
print(pow(x, e)) # pow akzeptiert integer und floats
|
||||
|
||||
# %%
|
||||
# Modulo
|
||||
|
||||
x = 10
|
||||
y = 3
|
||||
rest = x % 3
|
||||
print(rest)
|
||||
print(type(rest))
|
||||
|
||||
# %% [markdown]
|
||||
# # Strings
|
||||
|
||||
# %%
|
||||
s = "Dies ist ein String!"
|
||||
print(s)
|
||||
s2 = 'Man kann einfache oder doppelte Anführungszeichen verwenden.'
|
||||
print(s2)
|
||||
s3a = 'Innerhalb der Anführungszeichen kann man den jeweils "anderen Typ" verwenden'
|
||||
print(s3a)
|
||||
s3b = "Oder 'andersherum'."
|
||||
print(s3b)
|
||||
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)
|
||||
s6 = ("Mit Klammerung klappt das auch "
|
||||
"über mehrere Zeilen "
|
||||
"hinweg")
|
||||
print(s6)
|
||||
|
||||
# %%
|
||||
# Längere Strings
|
||||
|
||||
long_string = """Dies ist ein langer String ("triple quoted string").
|
||||
Er kann Zeilenumbrüche enthalten.
|
||||
|
||||
Und Leerzeilen.
|
||||
|
||||
Und auch 'einfache' oder "doppelte" Anführungszeichen.
|
||||
|
||||
Er endet mit drei weiteren Anführungszeichen des gleichen Typs.
|
||||
"""
|
||||
print(long_string)
|
||||
print(repr(long_string))
|
||||
|
||||
# %%
|
||||
# Escape Sequenzen
|
||||
|
||||
# Zeilenumbrüche und Tabs
|
||||
print("Erste Zeile\r\nZweite Zeile") # Zeilenumbruch nach Windows-Art
|
||||
print("Eine Zeile\nNoch eine Zeile") # Rest der Welt ;-)
|
||||
print("Eins\tZwei\tDrei")
|
||||
print("1\t2\t3")
|
||||
|
||||
# Null und Hexadezimal- und Unicode-Sequenzen
|
||||
print(repr("Dieser String enthält ein\0Null-Zeichen"))
|
||||
print("Hexadezimal-Codes können wir druckbare (e.g. '\x40') und nicht druckbare Zeichen (e.g. \x7F) verwendet werden.")
|
||||
print('\u2764') # Unicode
|
||||
|
||||
# %%
|
||||
# String-Operationen
|
||||
|
||||
s1 = "Dies ist der erste String."
|
||||
s2 = "Und dies der zweite."
|
||||
s3 = s1 + s2
|
||||
print(s3)
|
||||
s4 = "Ich habe es dir schon dreimal gesagt! "
|
||||
print(s4 * 3)
|
||||
|
||||
# %%
|
||||
klein = "schrei nicht so laut!"
|
||||
print(klein.upper())
|
||||
print("hallo" == "HALLO".lower())
|
||||
print("hallo" == "Hallo".lower())
|
||||
print("straße".upper() == "STRASSE")
|
||||
|
||||
|
||||
# %%
|
||||
hallo = "Hallo"
|
||||
print(hallo)
|
||||
print(hallo[1])
|
||||
print(hallo[0] + 'e' + hallo[2:])
|
||||
|
||||
# %%
|
||||
# Aber das geht nicht!:
|
||||
|
||||
hallo[1] = 'e'
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
|
@ -11,26 +11,14 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Listen und Tupel"
|
||||
"## Listen und Tupel"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Listenliterale\n",
|
||||
"\n",
|
||||
|
@ -50,21 +38,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Tuplelliterale\n",
|
||||
"values = (5, 10, 20, 30, 50)\n",
|
||||
|
@ -78,7 +54,8 @@
|
|||
"t_single = (\"one\",)\n",
|
||||
"t_auch_single = \"one\",\n",
|
||||
"t_leer = ()\n",
|
||||
"print(t_single, t_auch_single, t_leer)\n"
|
||||
"t_auch_leer = tuple()\n",
|
||||
"print(t_single, t_auch_single, t_leer, t_auch_leer)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -90,10 +67,414 @@
|
|||
"# Umwandlung\n",
|
||||
"\n",
|
||||
"l = [1, 2, 3, 4, 5]\n",
|
||||
"t= tuple(t)\n",
|
||||
"t = tuple(l)\n",
|
||||
"print(l, t)\n",
|
||||
"l = [10, 20, 30, 40, 50]\n",
|
||||
"print(l, t)\n"
|
||||
"print(l, t)\n",
|
||||
"l2 = list(t)\n",
|
||||
"print(l2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"s = \"hallo\"\n",
|
||||
"print(list(s))\n",
|
||||
"print(tuple(s))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Indizierung"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"seasons = [\"Frühling\", \"Sommer\", \"Herbst\", \"Winter\"]\n",
|
||||
"print(seasons[0]) # Erstes Element hat Index 0!\n",
|
||||
"print(seasons[2])\n",
|
||||
"print(seasons[-1]) # Negative Indices zählen vom letzten Element rückwärts\n",
|
||||
"print(seasons[-3])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Ungültige Indizes führen zu einem Fehler (IndexError Exception)\n",
|
||||
"print(seasons[4]) # 4 Elemente => gültige Indizes 0..3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Ebenso\n",
|
||||
"print(seasons[-5])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Zuweisung an einzelne Listenelemente\n",
|
||||
"\n",
|
||||
"l = [1, 2, 3, 4, 5]\n",
|
||||
"l[1] = -1\n",
|
||||
"print(l)\n",
|
||||
"l[3] = \"vier\"\n",
|
||||
"print(l)\n",
|
||||
"l[-1] = 5.0\n",
|
||||
"print(l)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Indizierung funktioniert auch mit Tupeln mit der selben Syntax\n",
|
||||
"t = (1, 2, 3, 4, 5)\n",
|
||||
"print(t[0], t[-2])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Aber Tuple sind nicht-veränderbar (immutable)\n",
|
||||
"\n",
|
||||
"t[2] = \"drei\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Slicing"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"l = list(range(10)) # l = [0, 1, ..., 9]\n",
|
||||
"print(l[2:4]) # Start ist inklusiv, Ende nicht!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Auch wenn kein oder nur ein Element \"übrig bleibt\", ist das Ergebnis eine Liste\n",
|
||||
"\n",
|
||||
"print(l[2:3])\n",
|
||||
"print(l[9:-1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Start und End können leer, also \"offen\", sein\n",
|
||||
"\n",
|
||||
"print(l[5:])\n",
|
||||
"print(l[:3]) # auch hier ist der Ende-Index nicht-inklusiv!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Zuweisung an Listenslices ist möglich!\n",
|
||||
"\n",
|
||||
"seasons = [\"Winter\", \"Frühling\", \"Sommer\", \"Herbst\"]\n",
|
||||
"seasons[1:1] = [\"Karneval\"]\n",
|
||||
"print(seasons)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Listen oder Tuples können mit + zu einer/m neuen Liste / Tupel zusamengefügt werden \n",
|
||||
"\n",
|
||||
"items = [1, 2, 3]\n",
|
||||
"more_items = items + [\"one\", \"two\", \"three\"]\n",
|
||||
"print(more_items)\n",
|
||||
"items[1:2] = []\n",
|
||||
"print(items)\n",
|
||||
"print(more_items)\n",
|
||||
"more_items += [4, 6, 6]\n",
|
||||
"print(more_items)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"t = (1, 2, 3)\n",
|
||||
"t2 = t + (4, 5, 6)\n",
|
||||
"print(t)\n",
|
||||
"print(t2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Achtung: \n",
|
||||
"\n",
|
||||
"t = (1, 2, 3)\n",
|
||||
"t2 = t\n",
|
||||
"print(t)\n",
|
||||
"print(t2)\n",
|
||||
"print(t is t2)\n",
|
||||
"t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie!\n",
|
||||
"print(t)\n",
|
||||
"print(t2)\n",
|
||||
"print(t is t2)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Dictionaries"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Auch Hashtables, Lookup Tables usw. genannt\n",
|
||||
"# Tabellen mit Zurdnung von Schlüsseln zu werten (Key-value pairs)\n",
|
||||
"\n",
|
||||
"d = {}\n",
|
||||
"# oder:\n",
|
||||
"d = dict()\n",
|
||||
"print(d)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"scores = {\n",
|
||||
" \"blue team\": 40,\n",
|
||||
" \"green team\": 45,\n",
|
||||
" \"red team\": 30,\n",
|
||||
" \"black team\": 60\n",
|
||||
"}\n",
|
||||
"print(scores)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Look up\n",
|
||||
"\n",
|
||||
"print(\"The red team scored:\", scores[\"red team\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Neue Key/Value Paare hinzufügen (Dictionaries sind veränderbar!)\n",
|
||||
"\n",
|
||||
"scores[\"white team\"] = 45\n",
|
||||
"scores[\"rainbow team\"] = 50\n",
|
||||
"print(scores)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Umwandlung\n",
|
||||
"\n",
|
||||
"print(tuple(scores)) # Tupel der Keys\n",
|
||||
"\n",
|
||||
"print(list(scores)) # Liste der Keys\n",
|
||||
"\n",
|
||||
"print(tuple(scores.keys()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Iteration\n",
|
||||
"\n",
|
||||
"for key in scores:\n",
|
||||
" print(\"Das\", key, \"hat\", scores[key], \"Punkte.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Iteration über Key/Value Paare\n",
|
||||
"\n",
|
||||
"print(scores.items())\n",
|
||||
"\n",
|
||||
"for key, value in scores.items():\n",
|
||||
" print(\"Das\", key, \"hat\", value, \"Punkte.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Keys müsen keine Strings, aber immutable sein\n",
|
||||
"# => Integers, Floats, Tuples sind ok\n",
|
||||
"\n",
|
||||
"numbers = {1: \"one\", 2: \"two\", 3: \"three\"}\n",
|
||||
"print(numbers[2]) # Achtung: nicht mit Listenindex verwechseln!\n",
|
||||
"\n",
|
||||
"pixels = {\n",
|
||||
" (0, 5): 'r',\n",
|
||||
" (2, 7): 'b', \n",
|
||||
" (3, 10): 'r',\n",
|
||||
" (5, 2): 'g',\n",
|
||||
" (5, 5): 'b', \n",
|
||||
"}\n",
|
||||
"\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",
|
||||
" print()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Sets (Mengen)\n",
|
||||
"\n",
|
||||
"Mengen sind wie unsortierte Tupel, in denen jedes Element nur einmal vorkommen kann."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"s = set([])\n",
|
||||
"print(s) # leeres Set wird repräsentiert mit 'set()', nicht '{}' (leeres Dictionary!)\n",
|
||||
"\n",
|
||||
"s2 = set([2,3,5])\n",
|
||||
"s2.add(4)\n",
|
||||
"s2.add(5) # Schon im Set enthalten\n",
|
||||
"print(s2)\n",
|
||||
"\n",
|
||||
"for i in s2:\n",
|
||||
" print(i)\n",
|
||||
"\n",
|
||||
"print()\n",
|
||||
"\n",
|
||||
"s2.add(1)\n",
|
||||
"s2.remove(4)\n",
|
||||
"\n",
|
||||
"for i in s2:\n",
|
||||
" print(i) # Reihenfolge nach Änderung nicht garantiert gleich!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Kombination\n",
|
||||
"\n",
|
||||
"s1 = set([1, 2, 3, 5])\n",
|
||||
"s2 = set([4, 5, 6, 7])\n",
|
||||
"\n",
|
||||
"# Union (alle Elemente aus beiden Sets)\n",
|
||||
"print(s1 | s2)\n",
|
||||
"\n",
|
||||
"# Intersection (alle Elemente, die sowohl in s1 als auch s2 sind)\n",
|
||||
"print(s1 & s2)\n",
|
||||
"\n",
|
||||
"# Difference (alle Elemente in s1, die nicht auch in s2 sin)\n",
|
||||
"print(s1 - s2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Siehe: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
|
@ -0,0 +1,286 @@
|
|||
# %% [markdown]
|
||||
# # Erweiterte Datentypen: Container
|
||||
|
||||
# %% [markdown]
|
||||
# ## Listen und Tupel
|
||||
|
||||
# %%
|
||||
# Listenliterale
|
||||
|
||||
values = [5, 10, 20, 30, 50]
|
||||
print(values)
|
||||
fractions = [0.5, 0.6, 0.8, 1.0, 1.2]
|
||||
print(fractions)
|
||||
names = ["Joe", "Alice", "Bob", "Charly"]
|
||||
print(names)
|
||||
heap = [1, "Joe", 1.4, "Alice", 100]
|
||||
print(heap)
|
||||
single = ["one"]
|
||||
leer = []
|
||||
auch_leer = list()
|
||||
print(single, leer, auch_leer)
|
||||
|
||||
# %%
|
||||
# Tuplelliterale
|
||||
values = (5, 10, 20, 30, 50)
|
||||
print(values)
|
||||
fractions = (0.5, 0.6, 0.8, 1.0, 1.2)
|
||||
print(fractions)
|
||||
names = ("Joe", "Alice", "Bob", "Charly")
|
||||
print(names)
|
||||
heap = (1, "Joe", 1.4, "Alice", 100)
|
||||
print(heap)
|
||||
t_single = ("one",)
|
||||
t_auch_single = "one",
|
||||
t_leer = ()
|
||||
t_auch_leer = tuple()
|
||||
print(t_single, t_auch_single, t_leer, t_auch_leer)
|
||||
|
||||
|
||||
# %%
|
||||
# Umwandlung
|
||||
|
||||
l = [1, 2, 3, 4, 5]
|
||||
t = tuple(l)
|
||||
print(l, t)
|
||||
l = [10, 20, 30, 40, 50]
|
||||
print(l, t)
|
||||
l2 = list(t)
|
||||
print(l2)
|
||||
|
||||
# %%
|
||||
s = "hallo"
|
||||
print(list(s))
|
||||
print(tuple(s))
|
||||
|
||||
# %% [markdown]
|
||||
# ### Indizierung
|
||||
|
||||
# %%
|
||||
seasons = ["Frühling", "Sommer", "Herbst", "Winter"]
|
||||
print(seasons[0]) # Erstes Element hat Index 0!
|
||||
print(seasons[2])
|
||||
print(seasons[-1]) # Negative Indices zählen vom letzten Element rückwärts
|
||||
print(seasons[-3])
|
||||
|
||||
# %%
|
||||
# Ungültige Indizes führen zu einem Fehler (IndexError Exception)
|
||||
print(seasons[4]) # 4 Elemente => gültige Indizes 0..3
|
||||
|
||||
# %%
|
||||
# Ebenso
|
||||
print(seasons[-5])
|
||||
|
||||
# %%
|
||||
# Zuweisung an einzelne Listenelemente
|
||||
|
||||
l = [1, 2, 3, 4, 5]
|
||||
l[1] = -1
|
||||
print(l)
|
||||
l[3] = "vier"
|
||||
print(l)
|
||||
l[-1] = 5.0
|
||||
print(l)
|
||||
|
||||
# %%
|
||||
# Indizierung funktioniert auch mit Tupeln mit der selben Syntax
|
||||
t = (1, 2, 3, 4, 5)
|
||||
print(t[0], t[-2])
|
||||
|
||||
# %%
|
||||
# Aber Tuple sind nicht-veränderbar (immutable)
|
||||
|
||||
t[2] = "drei"
|
||||
|
||||
# %% [markdown]
|
||||
# ### Slicing
|
||||
|
||||
# %%
|
||||
l = list(range(10)) # l = [0, 1, ..., 9]
|
||||
print(l[2:4]) # Start ist inklusiv, Ende nicht!
|
||||
|
||||
# %%
|
||||
# Auch wenn kein oder nur ein Element "übrig bleibt", ist das Ergebnis eine Liste
|
||||
|
||||
print(l[2:3])
|
||||
print(l[9:-1])
|
||||
|
||||
# %%
|
||||
# Start und End können leer, also "offen", sein
|
||||
|
||||
print(l[5:])
|
||||
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)
|
||||
print(l2)
|
||||
|
||||
# %%
|
||||
# Zuweisung an Listenslices ist möglich!
|
||||
|
||||
seasons = ["Winter", "Frühling", "Sommer", "Herbst"]
|
||||
seasons[1:1] = ["Karneval"]
|
||||
print(seasons)
|
||||
|
||||
# %%
|
||||
# Listen oder Tuples können mit + zu einer/m neuen Liste / Tupel zusamengefügt werden
|
||||
|
||||
items = [1, 2, 3]
|
||||
more_items = items + ["one", "two", "three"]
|
||||
print(more_items)
|
||||
items[1:2] = []
|
||||
print(items)
|
||||
print(more_items)
|
||||
more_items += [4, 6, 6]
|
||||
print(more_items)
|
||||
|
||||
|
||||
# %%
|
||||
t = (1, 2, 3)
|
||||
t2 = t + (4, 5, 6)
|
||||
print(t)
|
||||
print(t2)
|
||||
|
||||
# %%
|
||||
# Achtung:
|
||||
|
||||
t = (1, 2, 3)
|
||||
t2 = t
|
||||
print(t)
|
||||
print(t2)
|
||||
print(t is t2)
|
||||
t += (1, 2, 3) # Tuples sind immutable, t ist eine neue Kopie!
|
||||
print(t)
|
||||
print(t2)
|
||||
print(t is t2)
|
||||
|
||||
|
||||
# %% [markdown]
|
||||
# ## Dictionaries
|
||||
|
||||
# %%
|
||||
# Auch Hashtables, Lookup Tables usw. genannt
|
||||
# Tabellen mit Zurdnung von Schlüsseln zu werten (Key-value pairs)
|
||||
|
||||
d = {}
|
||||
# oder:
|
||||
d = dict()
|
||||
print(d)
|
||||
|
||||
# %%
|
||||
scores = {
|
||||
"blue team": 40,
|
||||
"green team": 45,
|
||||
"red team": 30,
|
||||
"black team": 60
|
||||
}
|
||||
print(scores)
|
||||
|
||||
# %%
|
||||
# Look up
|
||||
|
||||
print("The red team scored:", scores["red team"])
|
||||
|
||||
# %%
|
||||
# Neue Key/Value Paare hinzufügen (Dictionaries sind veränderbar!)
|
||||
|
||||
scores["white team"] = 45
|
||||
scores["rainbow team"] = 50
|
||||
print(scores)
|
||||
|
||||
# %%
|
||||
# Umwandlung
|
||||
|
||||
print(tuple(scores)) # Tupel der Keys
|
||||
|
||||
print(list(scores)) # Liste der Keys
|
||||
|
||||
print(tuple(scores.keys()))
|
||||
|
||||
# %%
|
||||
# Iteration
|
||||
|
||||
for key in scores:
|
||||
print("Das", key, "hat", scores[key], "Punkte.")
|
||||
|
||||
# %%
|
||||
# Iteration über Key/Value Paare
|
||||
|
||||
print(scores.items())
|
||||
|
||||
for key, value in scores.items():
|
||||
print("Das", key, "hat", value, "Punkte.")
|
||||
|
||||
# %%
|
||||
# Keys müsen keine Strings, aber immutable sein
|
||||
# => Integers, Floats, Tuples sind ok
|
||||
|
||||
numbers = {1: "one", 2: "two", 3: "three"}
|
||||
print(numbers[2]) # Achtung: nicht mit Listenindex verwechseln!
|
||||
|
||||
pixels = {
|
||||
(0, 5): 'r',
|
||||
(2, 7): 'b',
|
||||
(3, 10): 'r',
|
||||
(5, 2): 'g',
|
||||
(5, 5): 'b',
|
||||
}
|
||||
|
||||
for row in range(6):
|
||||
for col in range(10):
|
||||
if (row, col) in pixels:
|
||||
print(pixels[(row, col)], end="")
|
||||
else:
|
||||
print(".", end="")
|
||||
print()
|
||||
|
||||
|
||||
# %% [markdown]
|
||||
# ## Sets (Mengen)
|
||||
#
|
||||
# Mengen sind wie unsortierte Tupel, in denen jedes Element nur einmal vorkommen kann.
|
||||
|
||||
# %%
|
||||
s = set([])
|
||||
print(s) # leeres Set wird repräsentiert mit 'set()', nicht '{}' (leeres Dictionary!)
|
||||
|
||||
s2 = set([2,3,5])
|
||||
s2.add(4)
|
||||
s2.add(5) # Schon im Set enthalten
|
||||
print(s2)
|
||||
|
||||
for i in s2:
|
||||
print(i)
|
||||
|
||||
print()
|
||||
|
||||
s2.add(1)
|
||||
s2.remove(4)
|
||||
|
||||
for i in s2:
|
||||
print(i) # Reihenfolge nach Änderung nicht garantiert gleich!
|
||||
|
||||
# %%
|
||||
# Kombination
|
||||
|
||||
s1 = set([1, 2, 3, 5])
|
||||
s2 = set([4, 5, 6, 7])
|
||||
|
||||
# Union (alle Elemente aus beiden Sets)
|
||||
print(s1 | s2)
|
||||
|
||||
# Intersection (alle Elemente, die sowohl in s1 als auch s2 sind)
|
||||
print(s1 & s2)
|
||||
|
||||
# Difference (alle Elemente in s1, die nicht auch in s2 sin)
|
||||
print(s1 - s2)
|
||||
|
||||
# %% [markdown]
|
||||
# Siehe: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
|
@ -16,17 +16,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hmm, ob das so stimmt?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# if / else / elif\n",
|
||||
"\n",
|
||||
|
@ -42,17 +34,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Nach Süden, nach Süden!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"richtung = \"s\"\n",
|
||||
"\n",
|
||||
|
@ -68,17 +52,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Auf in den hohen Norden!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# match\n",
|
||||
"\n",
|
||||
|
@ -107,21 +83,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# for\n",
|
||||
"\n",
|
||||
|
@ -131,17 +95,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Summer der Zahlen 1-10: 55\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"result = 0\n",
|
||||
"for i in range(1, 11):\n",
|
||||
|
@ -151,17 +107,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"9 passt in die Reihe\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for i in (2, 4, 6, 8, 9, 10):\n",
|
||||
" if i % 2:\n",
|
||||
|
@ -170,17 +118,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"10 ... 9 ... 8 ... 7 ... 6 ... 5 ... 4 ... 3 ... 2 ... 1 ... Lift off!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# while\n",
|
||||
"\n",
|
||||
|
@ -193,25 +133,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# continue\n",
|
||||
"\n",
|
||||
|
@ -223,17 +147,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Iteration: 142\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# break\n",
|
||||
"\n",
|
||||
|
@ -253,17 +169,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"alphabet = \"abcdefghifklmnopqrstuvwxyz\"\n",
|
||||
"buchstabe = \"q\"\n",
|
||||
|
@ -277,17 +185,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"q ist Buchstabe Nr. 17 des dt. Alphabets.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# einfacher:\n",
|
||||
"\n",
|
||||
|
@ -306,17 +206,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"256 256 100\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Aufruf eingebauter Funktionen\n",
|
||||
"\n",
|
||||
|
@ -328,18 +220,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# optionale Parameter\n",
|
||||
"\n",
|
||||
|
@ -349,17 +232,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# eigene Funktionen\n",
|
||||
"\n",
|
||||
|
@ -371,18 +246,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"15\n",
|
||||
"24\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Keyword arguments\n",
|
||||
"\n",
|
||||
|
@ -392,18 +258,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Das geht nicht:\n",
|
||||
"print(add(a=6, 10))"
|
||||
|
@ -418,24 +275,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"False\n",
|
||||
"True\n",
|
||||
"False\n",
|
||||
"True\n",
|
||||
"True\n",
|
||||
"False\n",
|
||||
"True\n",
|
||||
"True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a = 10\n",
|
||||
"b = 50\n",
|
||||
|
@ -454,20 +296,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"True\n",
|
||||
"True\n",
|
||||
"True\n",
|
||||
"False\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Negation\n",
|
||||
"\n",
|
||||
|
@ -479,19 +310,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"True\n",
|
||||
"True\n",
|
||||
"True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# logische Kombination\n",
|
||||
"\n",
|
||||
|
@ -502,20 +323,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 41,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"True\n",
|
||||
"True\n",
|
||||
"True\n",
|
||||
"False\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Bool'scher (True/False) Wert von Integers, Floats und Strings \n",
|
||||
"print(a > c)\n",
|
||||
|
@ -527,26 +337,9 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"execution_count": null,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(bool(0))\n",
|
||||
"print(bool(1))\n",
|
||||
|
|
|
@ -0,0 +1,214 @@
|
|||
# %% [markdown]
|
||||
# # Kontrollstrukturen und Vergleichsoperatoren
|
||||
|
||||
# %% [markdown]
|
||||
# ## Verzweigung (Branching)
|
||||
|
||||
# %%
|
||||
# if / else / elif
|
||||
|
||||
alter = -1
|
||||
|
||||
if alter >= 16:
|
||||
print("Nicht vergessen: am 9.6.2024 ist Europa-Wahl!")
|
||||
elif alter > 0:
|
||||
print("Schön, dass du geboren bist!")
|
||||
else:
|
||||
print("Hmm, ob das so stimmt?")
|
||||
|
||||
# %%
|
||||
richtung = "s"
|
||||
|
||||
if richtung == "n":
|
||||
print("Auf in den hohen Norden!")
|
||||
elif richtung == "s":
|
||||
print("Nach Süden, nach Süden!")
|
||||
elif richtung == "w":
|
||||
print("Go west!")
|
||||
elif richtung == "o":
|
||||
print("Bin schon da.")
|
||||
|
||||
|
||||
# %%
|
||||
# match
|
||||
|
||||
# richtung = input("Wo soll's denn lang gehen? ")
|
||||
richtung = "n"
|
||||
|
||||
match richtung:
|
||||
case "n" | "norden":
|
||||
print("Auf in den hohen Norden!")
|
||||
case "s" | "süden":
|
||||
print("Nach Süden, nach Süden!")
|
||||
case "w" | "westen":
|
||||
print("Go west!")
|
||||
case "o" | "osten":
|
||||
print("Bin schon da.")
|
||||
case _:
|
||||
print("Das Glück liegt manchmal abseits des direkten Wegs!")
|
||||
|
||||
# %% [markdown]
|
||||
# ## Schleifen
|
||||
|
||||
# %%
|
||||
# for
|
||||
|
||||
for i in range(5):
|
||||
print("Schleifendurchlauf Nr. ", i)
|
||||
|
||||
|
||||
# %%
|
||||
result = 0
|
||||
for i in range(1, 11):
|
||||
result += i
|
||||
print("Summe der Zahlen 1-10:", result)
|
||||
|
||||
# %%
|
||||
for i in (2, 4, 6, 8, 9, 10):
|
||||
if i % 2:
|
||||
print(i, "passt nicht in die Reihe")
|
||||
|
||||
# %%
|
||||
# while
|
||||
|
||||
i = 10
|
||||
while i > 0: # or: while True:
|
||||
print(i, "... ", end="")
|
||||
i -= 1
|
||||
print("Lift off!")
|
||||
|
||||
# %%
|
||||
# continue
|
||||
|
||||
for i in range(10, 20):
|
||||
if i == 13:
|
||||
continue
|
||||
print(i)
|
||||
|
||||
# %%
|
||||
# break
|
||||
|
||||
i = 0
|
||||
result = 0
|
||||
maximum = 10_000
|
||||
|
||||
while True:
|
||||
result += i
|
||||
|
||||
if result >= maximum:
|
||||
print("Iteration: ", i+1)
|
||||
break
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
# %%
|
||||
alphabet = "abcdefghifklmnopqrstuvwxyz"
|
||||
buchstabe = "q"
|
||||
i = 0
|
||||
for char in alphabet:
|
||||
i += 1
|
||||
if char == buchstabe:
|
||||
print(buchstabe, "ist Buchstabe Nr.", i, "des dt. Alphabets.")
|
||||
break
|
||||
|
||||
|
||||
# %%
|
||||
# einfacher:
|
||||
|
||||
for i, char in enumerate(alphabet):
|
||||
if char == buchstabe:
|
||||
print(buchstabe, "ist Buchstabe Nr.", i + 1, "des dt. Alphabets.")
|
||||
break
|
||||
|
||||
# %% [markdown]
|
||||
# # Funktionen
|
||||
|
||||
# %%
|
||||
# Aufruf eingebauter Funktionen
|
||||
|
||||
x = pow(2, 8)
|
||||
y = max(x, 100)
|
||||
z = min(max(0, x), 100)
|
||||
print(x, y, z)
|
||||
|
||||
# %%
|
||||
# optionale Parameter
|
||||
|
||||
print()
|
||||
print("")
|
||||
|
||||
# %%
|
||||
# eigene Funktionen
|
||||
|
||||
def add(a, b):
|
||||
return a + b
|
||||
|
||||
print(add(2, 3))
|
||||
|
||||
# %%
|
||||
# Keyword arguments
|
||||
|
||||
print(add(a=5, b=10))
|
||||
print(add(4, b=20))
|
||||
|
||||
# %%
|
||||
# Das geht nicht:
|
||||
print(add(a=6, 10))
|
||||
|
||||
# %% [markdown]
|
||||
# # Vergleichsausdrücke
|
||||
|
||||
# %%
|
||||
a = 10
|
||||
b = 50
|
||||
c = 2
|
||||
d = 5 * a
|
||||
|
||||
print(a == b)
|
||||
print(a != d)
|
||||
print(a > b)
|
||||
print(a > c)
|
||||
print(a >= c)
|
||||
print(c > a)
|
||||
print(c < b)
|
||||
print(c >= 2)
|
||||
|
||||
# %%
|
||||
# Negation
|
||||
|
||||
print(not c > a)
|
||||
print(not a == b) # klarer: a != b
|
||||
print(not b != d) # klarer: b == d
|
||||
print(not a > c)
|
||||
|
||||
# %%
|
||||
# logische Kombination
|
||||
|
||||
print(b > a and a > c)
|
||||
print(a > b or a > c)
|
||||
print(not a < c and b >= d)
|
||||
|
||||
# %%
|
||||
# Bool'scher (True/False) Wert von Integers, Floats und Strings
|
||||
print(a > c)
|
||||
print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen Wert haben
|
||||
# Aber:
|
||||
print(bool(a))
|
||||
print(not a) # auch ein bool'scher Ausdruck
|
||||
|
||||
|
||||
# %%
|
||||
print(bool(0))
|
||||
print(bool(1))
|
||||
print(bool(-1))
|
||||
print(bool(0.0))
|
||||
print(bool(0.1))
|
||||
print(bool(-0.1))
|
||||
print(bool("True"))
|
||||
print(bool("False")) # !!!
|
||||
print(bool(""))
|
||||
print(bool("\0"))
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue