3. Vorlesung
This commit is contained in:
parent
505860fcb9
commit
6d59ce7733
104
.obsidian/workspace.json
vendored
104
.obsidian/workspace.json
vendored
@ -41,16 +41,58 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Lectures/03 01.11.2024.md",
|
"file": "Template/Gruppe.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
"icon": "lucide-file",
|
"icon": "lucide-file",
|
||||||
"title": "03 01.11.2024"
|
"title": "Gruppe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "91e68fc77697e0f9",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "markdown",
|
||||||
|
"state": {
|
||||||
|
"file": "Gruppen/MeWi 1.md",
|
||||||
|
"mode": "source",
|
||||||
|
"source": false
|
||||||
|
},
|
||||||
|
"icon": "lucide-file",
|
||||||
|
"title": "MeWi 1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "d4d973e0d0e2e072",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "markdown",
|
||||||
|
"state": {
|
||||||
|
"file": "Gruppen/MeWi 2.md",
|
||||||
|
"mode": "source",
|
||||||
|
"source": false
|
||||||
|
},
|
||||||
|
"icon": "lucide-file",
|
||||||
|
"title": "MeWi 2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "91b08793b1132c55",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "markdown",
|
||||||
|
"state": {
|
||||||
|
"file": "Lectures/17 21.02.2025.md",
|
||||||
|
"mode": "source",
|
||||||
|
"source": false
|
||||||
|
},
|
||||||
|
"icon": "lucide-file",
|
||||||
|
"title": "17 21.02.2025"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"currentTab": 2
|
"currentTab": 5
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"direction": "vertical"
|
"direction": "vertical"
|
||||||
@ -122,7 +164,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "backlink",
|
"type": "backlink",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Lectures/03 01.11.2024.md",
|
"file": "Lectures/17 21.02.2025.md",
|
||||||
"collapseAll": false,
|
"collapseAll": false,
|
||||||
"extraContext": false,
|
"extraContext": false,
|
||||||
"sortOrder": "alphabetical",
|
"sortOrder": "alphabetical",
|
||||||
@ -132,7 +174,7 @@
|
|||||||
"unlinkedCollapsed": true
|
"unlinkedCollapsed": true
|
||||||
},
|
},
|
||||||
"icon": "links-coming-in",
|
"icon": "links-coming-in",
|
||||||
"title": "Backlinks for 03 01.11.2024"
|
"title": "Backlinks for 17 21.02.2025"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -141,12 +183,12 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outgoing-link",
|
"type": "outgoing-link",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Lectures/03 01.11.2024.md",
|
"file": "Lectures/17 21.02.2025.md",
|
||||||
"linksCollapsed": false,
|
"linksCollapsed": false,
|
||||||
"unlinkedCollapsed": true
|
"unlinkedCollapsed": true
|
||||||
},
|
},
|
||||||
"icon": "links-going-out",
|
"icon": "links-going-out",
|
||||||
"title": "Outgoing links from 03 01.11.2024"
|
"title": "Outgoing links from 17 21.02.2025"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -168,10 +210,10 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Lectures/03 01.11.2024.md"
|
"file": "Lectures/17 21.02.2025.md"
|
||||||
},
|
},
|
||||||
"icon": "lucide-list",
|
"icon": "lucide-list",
|
||||||
"title": "Outline of 03 01.11.2024"
|
"title": "Outline of 17 21.02.2025"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -213,20 +255,18 @@
|
|||||||
"table-editor-obsidian:Advanced Tables Toolbar": false
|
"table-editor-obsidian:Advanced Tables Toolbar": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"active": "a5dbdd4d7f31f3f3",
|
"active": "91b08793b1132c55",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"Material/data.txt",
|
|
||||||
"Material/test.txt",
|
"Material/test.txt",
|
||||||
"Material/untitled.txt",
|
"Material/data.txt",
|
||||||
"Lectures/17 21.02.2025.md",
|
|
||||||
"Material/V3.ipynb",
|
"Material/V3.ipynb",
|
||||||
"Material/Untitled.ipynb",
|
"Lectures/03 01.11.2024.md",
|
||||||
"submissions.xlsx",
|
|
||||||
"Template/Gruppe.md",
|
|
||||||
"Material/3.Vorlesung.slides.html",
|
"Material/3.Vorlesung.slides.html",
|
||||||
"Material/3.Vorlesung.ipynb",
|
"Material/3.Vorlesung.ipynb",
|
||||||
"Material/2.vorlesung.slides.html",
|
"Material/Untitled.ipynb",
|
||||||
"Material/2.vorlesung.html",
|
"Lectures/02 25.10.2024.md",
|
||||||
|
"To Do.md",
|
||||||
|
"README.md",
|
||||||
"Gruppen/MeWi 7 (DiKum).md",
|
"Gruppen/MeWi 7 (DiKum).md",
|
||||||
"Gruppen/MeWi 6.md",
|
"Gruppen/MeWi 6.md",
|
||||||
"Gruppen/MeWi 5.md",
|
"Gruppen/MeWi 5.md",
|
||||||
@ -235,11 +275,22 @@
|
|||||||
"Gruppen/MeWi 2.md",
|
"Gruppen/MeWi 2.md",
|
||||||
"Gruppen/MeWi 1.md",
|
"Gruppen/MeWi 1.md",
|
||||||
"Gruppen/Engineering 1.md",
|
"Gruppen/Engineering 1.md",
|
||||||
|
"Umfrage.md",
|
||||||
|
"Template/Gruppe.md",
|
||||||
|
"Template/Lecture.md",
|
||||||
"Gruppen",
|
"Gruppen",
|
||||||
"Lectures/16 14.02.2025.md",
|
"Material/README.md",
|
||||||
"Lectures/02 25.10.2024.md",
|
"Material/ToDo.md",
|
||||||
"To Do.md",
|
"Material/wise_24_25/lernmaterial/meme.png",
|
||||||
|
"Material/wise_24_25/lernmaterial/meme.webp",
|
||||||
|
"Student List.md",
|
||||||
"Timetable.md",
|
"Timetable.md",
|
||||||
|
"Lectures/17 21.02.2025.md",
|
||||||
|
"Lectures/16 14.02.2025.md",
|
||||||
|
"Material/2.vorlesung.ipynb",
|
||||||
|
"Material/env/etc/jupyter/labconfig/page_config.json",
|
||||||
|
"Material/env/etc/jupyter/labconfig",
|
||||||
|
"Material/env/lib/python3.12/site-packages/jupyter-1.1.1.dist-info/top_level.txt",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/bootstrap/fonts/glyphicons-halflings-regular.svg",
|
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/bootstrap/fonts/glyphicons-halflings-regular.svg",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/submitted/hacker/ps1/jupyter.png",
|
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/submitted/hacker/ps1/jupyter.png",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/submitted/bitdiddle/ps1/jupyter.png",
|
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/submitted/bitdiddle/ps1/jupyter.png",
|
||||||
@ -251,15 +302,6 @@
|
|||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/underscore/README.md",
|
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/underscore/README.md",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/jquery-color/README.md",
|
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/jquery-color/README.md",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/jquery/README.md",
|
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/jquery/README.md",
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/datatables.net-bs/Readme.md",
|
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/datatables.net-bs/Readme.md"
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/datatables.net/Readme.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/bootstrap/README.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/backbone/README.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/autosize/readme.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/server_extensions/formgrader/static/components/autosize/changelog.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/source/ps1_autotest/jupyter.png",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/user_guide/source/ps1/jupyter.png",
|
|
||||||
"Material/env/lib/python3.12/site-packages/nbgrader/docs/source/changelog.md",
|
|
||||||
"Material/env/lib/python3.12/site-packages/jupyter_server/i18n/README.md"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -1722,7 +1722,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.5"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -56,7 +56,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Kontrollstruckturen\n",
|
"# Kontrollstruckturen\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Aufgabe 3-3: Sequentielles - For Loop\n",
|
"## Sequentielles - For Loop\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Python verwendet eine spezielle Form des 'for-loops' dabei handelt es sich sprachlich um den 'for-each loop'.\n",
|
"Python verwendet eine spezielle Form des 'for-loops' dabei handelt es sich sprachlich um den 'for-each loop'.\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -81,16 +81,47 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Zu lesen ist dies wie folgt: \"Für jedes (for each) i in dem Intervall/Menge 0 bis arr.length mach etwas\"\n",
|
"Zu lesen ist dies wie folgt: \"Für jedes (for each) i in dem Intervall/Menge 0 bis arr.length mach etwas\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Der Unterschied besteht darin das Python jedes Element einer Menge durchläuft, der Catch liegt darin das es absolut unabhängig davon ist wie die Menge aussieht. Widmen wir uns zunächst einer Aufgabe:\n",
|
"Der Unterschied besteht darin das Python jedes Element einer Menge durchläuft, der Catch liegt darin das es absolut unabhängig davon ist wie die Menge aussieht. Widmen wir uns zunächst einer Aufgabe:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "7215a3e7-a240-43ec-914c-10221d8b28b0",
|
||||||
|
"metadata": {
|
||||||
|
"editable": true,
|
||||||
|
"nbgrader": {
|
||||||
|
"grade": false,
|
||||||
|
"grade_id": "cell-80add6da9914f961",
|
||||||
|
"locked": true,
|
||||||
|
"schema_version": 3,
|
||||||
|
"solution": false,
|
||||||
|
"task": false
|
||||||
|
},
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": ""
|
||||||
|
},
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"### Aufgabe \n",
|
||||||
"\n",
|
"\n",
|
||||||
"**Aufgabe**: Schreibe eine Funktion `sum_up`, mit Eingabeparameter `n`, die einen `for`-loop implementiert, welcher die Zahlen von `1...n` aufsummiert.\n",
|
"*3 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Schreibe eine Funktion `sum_up` mit Eingabeparameter `n`, welcher die Zahlen von `1...n` aufsummiert.\n",
|
||||||
|
"\n",
|
||||||
|
"Nutze dafür einen `for-loop`.\n",
|
||||||
|
"\n",
|
||||||
|
"**Beispiel**:\n",
|
||||||
|
"\n",
|
||||||
|
"$$n = 5$$ \n",
|
||||||
|
"$$sum\\_up(5) \\rightarrow 1 \\rightarrow 1 + 2 = 3 \\rightarrow 3 + 3 = 6 \\rightarrow 6 + 4 = 10 \\rightarrow 10 + 5 = 15$$\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Hinweis: die Funktion `range()` zählt standardmässig von `0...n-1`. Schauen Sie sich gerne dazu die offizielle Dokumentation an [PEP 204](https://peps.python.org/pep-0204/#list-ranges)."
|
"Hinweis: die Funktion `range()` zählt standardmässig von `0...n-1`. Schauen Sie sich gerne dazu die offizielle Dokumentation an [PEP 204](https://peps.python.org/pep-0204/#list-ranges)."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 12,
|
||||||
"id": "5426ddf1-2d2f-4c92-b007-2f6eca61703f",
|
"id": "5426ddf1-2d2f-4c92-b007-2f6eca61703f",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -120,7 +151,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 13,
|
||||||
"id": "3c38a839-3ab0-466c-98f9-189c35fc5025",
|
"id": "3c38a839-3ab0-466c-98f9-189c35fc5025",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -128,7 +159,7 @@
|
|||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-cff511e86dce0377",
|
"grade_id": "cell-cff511e86dce0377",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 3,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -138,9 +169,18 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"15\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(sum_up(5))\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"for n in range(3,12):\n",
|
"for n in range(3,12):\n",
|
||||||
" assert sum(range(n+1)) == sum_up(n)\n",
|
" assert sum(range(n+1)) == sum_up(n)\n",
|
||||||
@ -151,6 +191,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "4e6dfa94-18b9-4fb2-830a-83202d034752",
|
"id": "4e6dfa94-18b9-4fb2-830a-83202d034752",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
"editable": true,
|
||||||
"nbgrader": {
|
"nbgrader": {
|
||||||
"grade": false,
|
"grade": false,
|
||||||
"grade_id": "cell-02370acddb67290d",
|
"grade_id": "cell-02370acddb67290d",
|
||||||
@ -360,14 +401,18 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Ihnen ist ein Dictionary gegeben. Ändern Sie jeden Wert in dem Dictionary nach der Formel $f(x) = x^3-1$ mittels `for-loop`.\n",
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*2 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Ihnen ist das Dictionary `dict2` gegeben. Ändern Sie jeden Wert in dem Dictionary nach der Formel $f(x) = x^3-1$ mittels `for-loop`.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Tipp: Lassen Sie sich nicht von den Schlüsseln verwirren."
|
"Tipp: Lassen Sie sich nicht von den Schlüsseln verwirren."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 36,
|
||||||
"id": "abd323c0-5e1b-4c14-a65a-9d54a06f3a80",
|
"id": "abd323c0-5e1b-4c14-a65a-9d54a06f3a80",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -386,22 +431,22 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"dictionary = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
|
"dict2 = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
|
||||||
"### BEGIN SOLUTION\n",
|
"### BEGIN SOLUTION\n",
|
||||||
"dictionary = {key: value**3-1 for key, value in dictionary.items()}\n",
|
"dict2 = {key: value**3-1 for key, value in dict2.items()}\n",
|
||||||
"### END SOLUTION"
|
"### END SOLUTION"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 37,
|
||||||
"id": "7e0f9ac4-c5d4-44b6-a2fc-db98e2b46356",
|
"id": "7e0f9ac4-c5d4-44b6-a2fc-db98e2b46356",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"nbgrader": {
|
"nbgrader": {
|
||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-82eec3cba623ab8f",
|
"grade_id": "cell-82eec3cba623ab8f",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 2,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -411,13 +456,22 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"{'a': 175615, 5: 1727, 'python': 728, 3.14: 0.48706374396146557}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(dict2)\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"d = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
|
"d = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
|
||||||
"d = {key: value**3-1 for key, value in d.items()}\n",
|
"d = {key: value**3-1 for key, value in d.items()}\n",
|
||||||
"assert d == dictionary\n",
|
"assert d == dict2\n",
|
||||||
"### END HIDDEN TEST"
|
"### END HIDDEN TEST"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -544,7 +598,6 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "b6dcb24c-e01e-4522-8ac4-074adfe6105a",
|
"id": "b6dcb24c-e01e-4522-8ac4-074adfe6105a",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": false,
|
|
||||||
"nbgrader": {
|
"nbgrader": {
|
||||||
"grade": false,
|
"grade": false,
|
||||||
"grade_id": "cell-c6922240434c9d3a",
|
"grade_id": "cell-c6922240434c9d3a",
|
||||||
@ -561,12 +614,16 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"Probieren Sie sich gerne selber aus.\n",
|
"Probieren Sie sich gerne selber aus.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"**Zusatzaufgabe**: Erstellen Sie eine List mittels List Comprehension, welche die Zahlen `1...6` auf deren kubische Zahl `1...216` also der Funktion $f(x) = x^3$ abbildet."
|
"### Zusatzaufgabe \n",
|
||||||
|
"\n",
|
||||||
|
"*Keine Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Erstellen Sie eine List mittels List Comprehension, welche die Zahlen `1...6` auf deren kubische Zahl `1...216` also der Funktion $f(x) = x^3$ abbildet."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 15,
|
||||||
"id": "522f3228-1797-4ca2-9103-44fce48dfd4a",
|
"id": "522f3228-1797-4ca2-9103-44fce48dfd4a",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -593,14 +650,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 16,
|
||||||
"id": "7dc19b9f-116b-4741-a798-a66d495d477e",
|
"id": "7dc19b9f-116b-4741-a798-a66d495d477e",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
"editable": true,
|
||||||
"nbgrader": {
|
"nbgrader": {
|
||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-4e441b6db861559e",
|
"grade_id": "cell-4e441b6db861559e",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 0,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -610,311 +668,21 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
|
||||||
"### BEGIN HIDDEN TEST\n",
|
|
||||||
"c = [n**3 for n in range(1,7)]\n",
|
|
||||||
"assert c == cubics\n",
|
|
||||||
"### END HIDDEN TEST"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "44d14fea-c495-4489-a954-970cd92f6780",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-95bbc51225278c8f",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"## Aufgabe 3-4: Error Handling - Try Except Finally\n",
|
|
||||||
"\n",
|
|
||||||
"Finally die (für uns) letzte Kontrollstrucktur Das Error Handling.\n",
|
|
||||||
"\n",
|
|
||||||
"In Javascript meist mit `try-catch` notiert, im Sinne von \"Fehler fangen\", geht Python den anderen Weg und nutzt selbiges Konzept als `try-except`.\n",
|
|
||||||
"\n",
|
|
||||||
"Dabei wird der Fehler im `try` aufgefangen und im `except` bearbeitet. \n",
|
|
||||||
"\n",
|
|
||||||
"Dazu ein Beispiel, Teilen durch 0:"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "f6e456c2-b63b-462c-9580-4096ef23df81",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-e7fd21145b1d8a8b",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"1/0"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "04ddb838-ccd8-4e7b-8e5f-47640e2171f4",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-05a66eaf55ea54f5",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"Wie zu sehen ist das Ergebnis ein `ZeroDivisionError`, da die Division mit `0` nicht definiert ist.\n",
|
|
||||||
"\n",
|
|
||||||
"Jetzt werden Sie sich sicher wundern wie solch ein Fehler zustande kommt.\n",
|
|
||||||
"Dazu bringen wir noch ein beispiel an. Bei dem eine Liste mit zufälligen ganzzahligen Einträgen vorliegt und alle Werte nach dem Schema $\\frac{1}{n}$ dargestellt werden sollen."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "bc176b10-cbdf-4cfd-bba2-e14c449d0c90",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-3076653b52c0a71d",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"liste = [5,8,0,-4,3,6]\n",
|
|
||||||
"for el in liste:\n",
|
|
||||||
" print(1/el)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "932b0a5d-3766-4672-8ac9-e38d84aeab2a",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-20a30db416585e25",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"Ab dem dritten Eintrag der Liste löst die Operation einen Fehler aus und die Berechnung wird abgebrochen.\n",
|
|
||||||
"\n",
|
|
||||||
"Genau diesen Fehler wollen wir im nächsten Schritt mit `try-except` abfangen."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "c4bcccbd-1719-46ba-ac47-0446de21bae7",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-a6799a47e9560649",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"liste = [5,8,0,-4,3,6]\n",
|
|
||||||
"\n",
|
|
||||||
"try:\n",
|
|
||||||
" for el in liste:\n",
|
|
||||||
" print(1/el)\n",
|
|
||||||
"except:\n",
|
|
||||||
" print(\"Berechnung nicht möglich\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "44dccb14-a573-4b39-b92f-b42730bb0bb6",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-0e0a8498c4d1f7d6",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"In den `try` block wird die zu berechnende Anweisung geschrieben. Im `except` stehen dann die Anweisungen, die ausgeführt werden, sofern die Berechnung auf einen Fehler läuft. \n",
|
|
||||||
"Beachte hierbei das auch das Ausführen des Loops unterbrochen wird. Dies lässt sich verhindern indem der Loop außerhalb des `try-except` Blockes geschrieben wird."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "78c8992f-e980-49de-bf91-5587fe9dfe57",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-372501884a694566",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"**Aufgabe**: Gegeben sei ein Loop, welcher auf ein `IndexError` beim ausführen stößt. Verhindern Sie mittels `try-except` das dieser Loop einen Fehler wirft."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 3,
|
|
||||||
"id": "c81a314f-089e-4f2b-b3bf-4416e12dc0b9",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-eb932493525db1d8",
|
|
||||||
"locked": false,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": true,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"name": "stdout",
|
||||||
"text/plain": [
|
"output_type": "stream",
|
||||||
"[-1,\n",
|
"text": [
|
||||||
" -2,\n",
|
"[1, 8, 27, 64, 125, 216]\n"
|
||||||
" -4,\n",
|
]
|
||||||
" -8,\n",
|
|
||||||
" -16,\n",
|
|
||||||
" -32,\n",
|
|
||||||
" -64,\n",
|
|
||||||
" -128,\n",
|
|
||||||
" -256,\n",
|
|
||||||
" -1,\n",
|
|
||||||
" -2,\n",
|
|
||||||
" -4,\n",
|
|
||||||
" -8,\n",
|
|
||||||
" -16,\n",
|
|
||||||
" -32,\n",
|
|
||||||
" -64,\n",
|
|
||||||
" -128,\n",
|
|
||||||
" -256]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 3,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
|
||||||
"l = [2**n for n in range(10)]\n",
|
|
||||||
"s = list()\n",
|
|
||||||
"f = lambda l, i: l[i] - l[i+1]\n",
|
|
||||||
"\n",
|
|
||||||
"for i in range(len(l)-1):\n",
|
|
||||||
" s.append(f(l,i))\n",
|
|
||||||
"\n",
|
|
||||||
"# BEGIN SOLUTION\n",
|
|
||||||
"for i in range(len(l)):\n",
|
|
||||||
" try:\n",
|
|
||||||
" s.append(f(l,i))\n",
|
|
||||||
" except:\n",
|
|
||||||
" continue\n",
|
|
||||||
"# END SOLUTION \n",
|
|
||||||
"\n",
|
|
||||||
"s"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "9c48227f-5951-4e8c-976a-c1368674f05e",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": true,
|
|
||||||
"grade_id": "cell-0d65844c96851030",
|
|
||||||
"locked": true,
|
|
||||||
"points": 1,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(cubics)\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"assert s == [-2**n for n in range(9)]\n",
|
"c = [n**3 for n in range(1,7)]\n",
|
||||||
|
"assert c == cubics\n",
|
||||||
"### END HIDDEN TEST"
|
"### END HIDDEN TEST"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -939,89 +707,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# System Interactions\n",
|
"# System Interactions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Im folgenden Abschnitt beschäftigen wir uns mit dem Eingeben von Daten in ein Programm. Dies geschieht entweder 'von Hand', also über den Benutzer, oder über Dateien.\n",
|
"Im folgenden Abschnitt beschäftigen wir uns mit dem Eingeben von Daten in ein Programm. Dies geschieht entweder 'von Hand', also über den Benutzer, oder über Dateien."
|
||||||
"\n",
|
|
||||||
"## Aufgabe 4-1: Interaction - (User-)Input\n",
|
|
||||||
"\n",
|
|
||||||
"Neben der `print` Funktion bietet Python auch eine `input` Funktion zum Eingeben von Daten.\n",
|
|
||||||
"\n",
|
|
||||||
"Dabei kann beim Funktionsaufruf ein String mit einer Nachricht mitgegeben werden."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "9bc23729-8fcc-4cdd-ab73-dca9ebd597cc",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-faacd1403f906db1",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"input(\"Gib eine Zahl ein\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "7249872a-dffd-4b1e-948e-a7abcccc347c",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": false,
|
|
||||||
"grade_id": "cell-b8e15f786a268a73",
|
|
||||||
"locked": true,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": false,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"Wie (vielleicht) zu erwarten gibt die `input` Funktion den eingebenen Wert als String direkt zurück und kann damit in einer variablen gespeichert werden.\n",
|
|
||||||
"\n",
|
|
||||||
"**Aufgabe**: Schreibe ein Programm, welches den Input vom User als eine Zahl entgegen nimmt und diese Zahl+2 zurückgibt. Wichtig ist das der Wert als String zurückgegeben wird und vorher mittels `int()` Funktion zu einer Zahl umgewandelt werden muss."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "bc2d380a-25f7-4492-a2e5-c499b684d95e",
|
|
||||||
"metadata": {
|
|
||||||
"editable": true,
|
|
||||||
"nbgrader": {
|
|
||||||
"grade": true,
|
|
||||||
"grade_id": "cell-cae1081b4d4a17e7",
|
|
||||||
"locked": false,
|
|
||||||
"points": 1,
|
|
||||||
"schema_version": 3,
|
|
||||||
"solution": true,
|
|
||||||
"task": false
|
|
||||||
},
|
|
||||||
"slideshow": {
|
|
||||||
"slide_type": ""
|
|
||||||
},
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# BEGIN SOLUTION\n",
|
|
||||||
"a = int(input(\"Sag mir eine Zahl\"))\n",
|
|
||||||
"print(a+2)\n",
|
|
||||||
"# END SOLUTION"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1043,13 +729,11 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Aufgabe 4-2: Interaction - Files\n",
|
|
||||||
"\n",
|
|
||||||
"Im Normalfall wollen wir größere Datenmengen einlesen. Dazu nutzen wir die Built-In Funktion `open` ([Python Docs - Open](https://docs.python.org/3/library/functions.html?highlight=open#open)). Auch wenn in der Doku viele Parameter stehen benötigt man im Normalfall nur zwei. Der erste ist der Name der File, der zweite im welchem Modus die Datei geöffnet werden soll (Angegeben als String).\n",
|
"Im Normalfall wollen wir größere Datenmengen einlesen. Dazu nutzen wir die Built-In Funktion `open` ([Python Docs - Open](https://docs.python.org/3/library/functions.html?highlight=open#open)). Auch wenn in der Doku viele Parameter stehen benötigt man im Normalfall nur zwei. Der erste ist der Name der File, der zweite im welchem Modus die Datei geöffnet werden soll (Angegeben als String).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Zum bearbeiten der Datei nutzen wir den Python Kontext Manager - das `with`-Statement. Bei externen Daten ist es immer wichtig die Datei auch wieder zu schließen, damit es nicht zu Datenverlust oder Arbeitsspeichermüll kommt. Daher ist der Lebenzyklus einer Datei in einem Programm immer `Datei öffnen` -> `Datei Bearbeiten` -> `Datei schließen`. Kommt es in einer der Drei schritte zu einem Fehler, bleibt die Datei im RAM hängen und der Computer muss neu gestartet werden um diesen Speicher wieder Frei zu geben.\n",
|
"Zum bearbeiten der Datei nutzen wir den Python Kontext Manager - das `with`-Statement. Bei externen Daten ist es immer wichtig die Datei auch wieder zu schließen, damit es nicht zu Datenverlust oder Arbeitsspeichermüll kommt. Daher ist der Lebenzyklus einer Datei in einem Programm immer `Datei öffnen` -> `Datei Bearbeiten` -> `Datei schließen`. Kommt es in einer der Drei schritte zu einem Fehler, bleibt die Datei im RAM hängen und der Computer muss neu gestartet werden um diesen Speicher wieder Frei zu geben.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Daher gibt es Kontext Manager. Dieser versichert dem Programmierer, dass das schließen der Datei immer (!!) passiert. Die Sytax folgt folgender Strucktur:\n",
|
"Daher gibt es Kontext Manager. Dieser versichert dem Programmierer, dass das schließen der Datei immer (!!) passiert. Die Syntax folgt folgender Strucktur:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"```python\n",
|
"```python\n",
|
||||||
"with <kontext> as <var-name>:\n",
|
"with <kontext> as <var-name>:\n",
|
||||||
@ -1083,12 +767,18 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Erstellen und Öffnen sie eine Datei `testfile.txt` mit der `open` Funktion, nutzen Sie dafür das `with`-Statement. Schreiben Sie in diese Datei 100 mal den String `Python\\n`. "
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*2 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Erstellen und Öffnen sie eine Datei `testfile.txt` mit der `open` Funktion, nutzen Sie dafür das `with`-Statement.\n",
|
||||||
|
"\n",
|
||||||
|
"Schreiben Sie in diese Datei 100 mal den String `\"Python\\n\"`. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 17,
|
||||||
"id": "f11f7a0b-bcca-4db0-a6ca-bf95c70c7303",
|
"id": "f11f7a0b-bcca-4db0-a6ca-bf95c70c7303",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1116,7 +806,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 18,
|
||||||
"id": "c91e07bb-fc41-42c1-8b42-0aca56c57c35",
|
"id": "c91e07bb-fc41-42c1-8b42-0aca56c57c35",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1124,7 +814,7 @@
|
|||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-2592f8b51914455e",
|
"grade_id": "cell-2592f8b51914455e",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 2,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -1165,12 +855,16 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Öffnen Sie die zuvor erstellte Datei `testfile.txt` im Lesemodus und weißen Sie den Inhalt der `.readlines()` Funktion der Variabeln `lines` zu. "
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*2 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Öffnen Sie die zuvor erstellte Datei `testfile.txt` im Lesemodus und weißen Sie den Inhalt der `.readlines()` Funktion der Variabeln `lines` zu. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 19,
|
||||||
"id": "adf300e9-ee63-4da1-a6f3-c768fa2b5fc9",
|
"id": "adf300e9-ee63-4da1-a6f3-c768fa2b5fc9",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1198,7 +892,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 24,
|
||||||
"id": "229499d6-bb33-492b-af5d-a26ab3b3f5d4",
|
"id": "229499d6-bb33-492b-af5d-a26ab3b3f5d4",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1206,7 +900,7 @@
|
|||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-aa7c104b5f3f2572",
|
"grade_id": "cell-aa7c104b5f3f2572",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 2,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -1216,8 +910,18 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Anzahl der gelesenen Zeilen: 100\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(\"Anzahl der gelesenen Zeilen:\", len(lines))\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"with open('testfile.txt', 'r') as f:\n",
|
"with open('testfile.txt', 'r') as f:\n",
|
||||||
" assert f.readlines() == lines\n",
|
" assert f.readlines() == lines\n",
|
||||||
@ -1243,7 +947,7 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Aufgabe 4-3: Import Statement\n",
|
"## Import Statement\n",
|
||||||
"\n",
|
"\n",
|
||||||
"<img style=\"float: center;\" src=\"https://preview.redd.it/00824ycw3hwy.jpg?width=960&crop=smart&auto=webp&s=58a493191f1a6990d28bcbb6650b9f59749c5a9b\" width=800/> \n",
|
"<img style=\"float: center;\" src=\"https://preview.redd.it/00824ycw3hwy.jpg?width=960&crop=smart&auto=webp&s=58a493191f1a6990d28bcbb6650b9f59749c5a9b\" width=800/> \n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -1341,12 +1045,16 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Importiere Python Built-In Library `random` und rufe zuerst aus dem Modul die Funktion `seed` auf mit dem Eingabewert `42`, und weiße danach der Variable `rand` den Wert des Funktionsaufrufes von `randint(1,100)` zu. "
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*3 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Importiere Python Built-In Library `random` und rufe zuerst aus dem Modul die Funktion `seed` auf mit dem Eingabewert `42`, und weiße danach der Variable `rand` den Wert des Funktionsaufrufes von `randint(1,100)` zu. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 21,
|
||||||
"id": "0d80bc9f-6923-4e3f-8a70-909548e693a6",
|
"id": "0d80bc9f-6923-4e3f-8a70-909548e693a6",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1375,7 +1083,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 22,
|
||||||
"id": "ada0211b-03bf-463a-b932-2bad621d5559",
|
"id": "ada0211b-03bf-463a-b932-2bad621d5559",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1383,7 +1091,7 @@
|
|||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-d7817c3dd1ee34f9",
|
"grade_id": "cell-d7817c3dd1ee34f9",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 3,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -1393,9 +1101,18 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"82\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(rand)\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"assert rand == 82\n",
|
"assert rand == 82\n",
|
||||||
"### END HIDDEN TEST"
|
"### END HIDDEN TEST"
|
||||||
@ -1420,7 +1137,7 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"Aus Aufgabe 4-2 ist bereits das Keyword `as` bekannt dieses kann auch verwendet werden um Module beim import umzubennen. Dies ist dann Hilfreich wenn Module lange Namen haben.\n",
|
"Das Keyword `as` ist bereits bekannt, dieses kann auch verwendet werden um Module beim import umzubennen. Dies ist dann Hilfreich wenn Module lange Namen haben.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Numpy wird im Internet immer mit np abgekürzt. Beispiel:"
|
"Numpy wird im Internet immer mit np abgekürzt. Beispiel:"
|
||||||
]
|
]
|
||||||
@ -1469,12 +1186,16 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Importieren Sie die Built-In Library `datetime` als `dt`."
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*1 Punkt*\n",
|
||||||
|
"\n",
|
||||||
|
"Importieren Sie die Built-In Library `datetime` als `dt`."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 26,
|
||||||
"id": "e7e6c246-6dc4-4555-a202-73887b5f8249",
|
"id": "e7e6c246-6dc4-4555-a202-73887b5f8249",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1500,7 +1221,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 28,
|
||||||
"id": "b03ee34b-8520-4106-a23f-4419e54dcfcc",
|
"id": "b03ee34b-8520-4106-a23f-4419e54dcfcc",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1518,8 +1239,18 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2024-11-01 19:32:22.691479\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(dt.datetime.now()) # UTC Time also Standard Greenwich Zeit\n",
|
||||||
"### BEGIN HIDDEN TEST\n",
|
"### BEGIN HIDDEN TEST\n",
|
||||||
"assert 'dt' in dir()\n",
|
"assert 'dt' in dir()\n",
|
||||||
"### END HIDDEN TEST"
|
"### END HIDDEN TEST"
|
||||||
@ -1549,7 +1280,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 29,
|
||||||
"id": "638e6c20-7bba-4862-9acc-34031bae8514",
|
"id": "638e6c20-7bba-4862-9acc-34031bae8514",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1566,7 +1297,34 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"{0: 1,\n",
|
||||||
|
" 1: 2,\n",
|
||||||
|
" 2: 4,\n",
|
||||||
|
" 3: 8,\n",
|
||||||
|
" 4: 16,\n",
|
||||||
|
" 5: 32,\n",
|
||||||
|
" 6: 64,\n",
|
||||||
|
" 7: 128,\n",
|
||||||
|
" 8: 256,\n",
|
||||||
|
" 9: 512,\n",
|
||||||
|
" 10: 1024,\n",
|
||||||
|
" 11: 2048,\n",
|
||||||
|
" 12: 4096,\n",
|
||||||
|
" 13: 8192,\n",
|
||||||
|
" 14: 16384,\n",
|
||||||
|
" 15: 32768,\n",
|
||||||
|
" 16: 65536,\n",
|
||||||
|
" 17: 131072,\n",
|
||||||
|
" 18: 262144,\n",
|
||||||
|
" 19: 524288}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from pprint import pprint\n",
|
"from pprint import pprint\n",
|
||||||
"pprint({n: 2**n for n in range(20)})"
|
"pprint({n: 2**n for n in range(20)})"
|
||||||
@ -1591,12 +1349,17 @@
|
|||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**Aufgabe**: Importieren Sie die Funktion `sqrt` aus dem Built-In Modul `math`."
|
"### Aufgabe\n",
|
||||||
|
"\n",
|
||||||
|
"*2 Punkte*\n",
|
||||||
|
"\n",
|
||||||
|
"Importieren Sie die Funktion `sqrt` aus dem Built-In Modul `math`.\n",
|
||||||
|
"Berechnen Sie $\\sqrt4$. Speichern Sie das Ergebnis in der variablen `s4`."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 30,
|
||||||
"id": "81838f9b-558c-49b3-90ef-647e28380a97",
|
"id": "81838f9b-558c-49b3-90ef-647e28380a97",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1617,12 +1380,13 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# BEGIN SOLUTION\n",
|
"# BEGIN SOLUTION\n",
|
||||||
"from math import sqrt\n",
|
"from math import sqrt\n",
|
||||||
|
"s4 = sqrt(4)\n",
|
||||||
"# END SOLUTION"
|
"# END SOLUTION"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 33,
|
||||||
"id": "6d984a74-a93d-4865-b6d1-d2dec8586907",
|
"id": "6d984a74-a93d-4865-b6d1-d2dec8586907",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"editable": true,
|
"editable": true,
|
||||||
@ -1630,7 +1394,7 @@
|
|||||||
"grade": true,
|
"grade": true,
|
||||||
"grade_id": "cell-83c667de468e9ef8",
|
"grade_id": "cell-83c667de468e9ef8",
|
||||||
"locked": true,
|
"locked": true,
|
||||||
"points": 1,
|
"points": 2,
|
||||||
"schema_version": 3,
|
"schema_version": 3,
|
||||||
"solution": false,
|
"solution": false,
|
||||||
"task": false
|
"task": false
|
||||||
@ -1640,11 +1404,21 @@
|
|||||||
},
|
},
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2.0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Hier werden die Loesungen getestet...\n",
|
"# Hier werden die Loesungen getestet...\n",
|
||||||
|
"print(s4)\n",
|
||||||
"### BEGIN HIDDEN SOLUTION\n",
|
"### BEGIN HIDDEN SOLUTION\n",
|
||||||
"assert 'sqrt' in dir()\n",
|
"assert 'sqrt' in dir()\n",
|
||||||
|
"assert int(s4) == 2\n",
|
||||||
"### END HIDDEN SOLUTION"
|
"### END HIDDEN SOLUTION"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user