@@ -1,152 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das drei Zahlen von der Tastatur einliest und anschließend die\n", | |||
"größte von ihnen ausgibt." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "-" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"is_executing": false, | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"num1 = float(input(\"Erste Zahl:\"))\n", | |||
"num2 = float(input(\"Zweite Zahl:\"))\n", | |||
"num3 = float(input(\"Dritte Zahl:\"))\n", | |||
"\n", | |||
"maximum = num1 if num1 > num2 else num2\n", | |||
"maximum = maximum if maximum > num3 else num3\n", | |||
"print(maximum) \n", | |||
"\n", | |||
"\n", | |||
"if num1 > num2:\n", | |||
" if num1 > num3:\n", | |||
" print(num1)\n", | |||
" else:\n", | |||
" print(num3)\n", | |||
"else:\n", | |||
" if num2 > num3:\n", | |||
" print(num2)\n", | |||
" else:\n", | |||
" print(num3)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das zwei Zahlen von der Tastatur einliest. Anschließend soll\n", | |||
"nachgefragt werden, ob die beiden Zahlen addiert, subtrahiert, mulitpliziert oder dividiert werden sollen. \n", | |||
"Führen Sie die gewünschte Operation aus und geben Sie das Ergebnis aus. " | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "-" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"num1 = float(input(\"Erste Zahl:\"))\n", | |||
"num2 = float(input(\"Zweite Zahl:\"))\n", | |||
"\n", | |||
"op = input(\"Was soll gemacht werden (+,-,*,/):\")\n", | |||
"if op == \"+\":\n", | |||
" erg = num1 + num2\n", | |||
"elif op == \"-\":\n", | |||
" erg = num1 - num2\n", | |||
"elif op == \"*\":\n", | |||
" erg = num1 * num2\n", | |||
"elif op == \"/\":\n", | |||
" erg = num1 / num2\n", | |||
"else:\n", | |||
" erg = None\n", | |||
" \n", | |||
"print(f\"Unbekannter Operator <{op}>.\") if erg is None else print(f\"{num1} {op} {num2} = {erg}\")" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
}, | |||
"pycharm": { | |||
"stem_cell": { | |||
"cell_type": "raw", | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"source": [] | |||
} | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,146 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das zunächst eine Zahl von der Tastatur einliest,\n", | |||
"anschließend mittels einer <code>while</code>-Schleife die Binärdarstellung\n", | |||
"der Zahl ermittelt und diese ausgibt." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "-" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"is_executing": false, | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"num = int(input(\"Zahl:\"))\n", | |||
"\n", | |||
"ergebnis = \"\"\n", | |||
"while num > 0:\n", | |||
" ergebnis = str(num % 2) + ergebnis\n", | |||
" num = num // 2\n", | |||
"\n", | |||
"print(ergebnis)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das einen String bestehend aus <code>0</code> und <code>1</code> \n", | |||
"von der Tastatur einliest, diesen als Binärzahl betrachtet und den dezimalen Wert ausgibt." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "-" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"binaer = input(\"Binärzahl:\")\n", | |||
"\n", | |||
"# Lösung 1\n", | |||
"ergebnis = 0\n", | |||
"index = 0\n", | |||
"while index < len(binaer):\n", | |||
" ergebnis = ergebnis * 2\n", | |||
" if binaer[index] == \"1\":\n", | |||
" ergebnis = ergebnis + 1\n", | |||
" index = index + 1\n", | |||
"\n", | |||
"# Lösung 2 \n", | |||
"index = len(binaer)-1\n", | |||
"ergebnis2 = 0\n", | |||
"while index >=0:\n", | |||
" stellenwert = len(binaer) - 1 - index\n", | |||
" ergebnis2 = ergebnis2 + int(binaer[index])* (2 ** stellenwert ) \n", | |||
" index = index - 1\n", | |||
" \n", | |||
"print(ergebnis)\n", | |||
"print(ergebnis2)" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
}, | |||
"pycharm": { | |||
"stem_cell": { | |||
"cell_type": "raw", | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"source": [] | |||
} | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,152 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das einen Text von der Tastatur einliest und anschließend\n", | |||
"die Anfangsbuchstaben aller Wörter im Text ausgibt. Dabei soll jeder Buchstabe nur einmal vorkommen." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": {}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"text = input(\"Text:\")\n", | |||
"\n", | |||
"worte = text.split()\n", | |||
"startchars = [wort[0] for wort in worte ]\n", | |||
"\n", | |||
"for index, current in enumerate(startchars):\n", | |||
" if current not in startchars[:index]:\n", | |||
" print(current)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Gegeben sind zwei Listen gleicher Länge mit Artikeln und zugehörgen Preisen, z.B.:\n", | |||
"\n", | |||
"<code>artikel = [ \"Klopapier\", \"Maske\", \"Desinfektionsmittel\" ]</code>\n", | |||
"\n", | |||
"<code>preis = [ 3.5, 2.25, 7.4 ]</code>\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Programm, das eine Ergebnisliste berechnet, in der Artikel und Preis zu Tupeln \n", | |||
"zusammengefasst sind:\n", | |||
"\n", | |||
"<code>ergebnis = [('Klopapier', 3.5), ('Maske', 2.25), ('Desinfektionsmittel', 7.4)] </code>" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "subslide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"is_executing": false, | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"artikel = [ \"Klopapier\", \"Maske\", \"Desinfektionsmittel\" ]\n", | |||
"preis = [ 3.50, 2.25, 7.4 ]\n", | |||
"\n", | |||
"ergebnis = [(a, preis[index]) for index, a in enumerate(artikel)]\n", | |||
"print(ergebnis)\n", | |||
"\n", | |||
"########\n", | |||
"ergebnis2 = list(zip(artikel, preis))\n", | |||
"print(ergebnis2)\n", | |||
"\n", | |||
"########\n", | |||
"index = 0\n", | |||
"ergebnis3 = []\n", | |||
"while index < len(artikel):\n", | |||
" ergebnis3.append((artikel[index], preis[index]))\n", | |||
" index = index + 1\n", | |||
"print(ergebnis3)" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
}, | |||
"pycharm": { | |||
"stem_cell": { | |||
"cell_type": "raw", | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"source": [] | |||
} | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,153 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie eine Funktion <code>schnittmenge</code>, die zwei Listen als Parameter erwartet\n", | |||
"und eine Liste zurückgibt, die nur die Elemente beinhaltet, die in beiden übergebenen Listen enthalten waren." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": {}, | |||
"outputs": [], | |||
"source": [ | |||
"# Todo: Hier die Funktion anlegen\n", | |||
"print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"is_executing": false, | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def schnittmenge(liste1, liste2):\n", | |||
" ergebnis = []\n", | |||
" for element in liste2:\n", | |||
" if element in liste1:\n", | |||
" ergebnis.append(element)\n", | |||
" return ergebnis\n", | |||
"\n", | |||
"print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Schreiben Sie eine Funktion <code>pascal</code>, der eine Liste <code>z</code> von Integer-Zahlen \n", | |||
"übergeben wird. Sei <code>n</code> die Länge dieser Liste. \n", | |||
"Das Ergebnis soll eine neue Liste sein mit folgenden Einträgen\n", | |||
"\n", | |||
"Falls <code>n == 0</code>:\n", | |||
"\n", | |||
" <code>[ 1 ]</code>\n", | |||
"\n", | |||
"Falls <code>n == 1</code>:\n", | |||
"\n", | |||
" <code>[ 1 , 1 ]</code>\n", | |||
"\n", | |||
"Sonst:\n", | |||
"\n", | |||
" <code>[ 1 , z[0]+z[1] , z[1]+z[2] , ... , z[n-2]+z[n-1] , 1 ]</code>" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"# Todo: Hier die Funktion anlegen\n", | |||
"\n", | |||
"print(pascal([]))\n", | |||
"print(pascal([5]))\n", | |||
"print(pascal([5, 2, 7, 3]))" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "subslide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def pascal(z):\n", | |||
" erg = [1]\n", | |||
" erg.extend([a+b for a, b in zip(z[:-1], z[1:])])\n", | |||
" if len(z) > 0:\n", | |||
" erg.append(1)\n", | |||
" return erg\n", | |||
"\n", | |||
"print(pascal([]))\n", | |||
"print(pascal([5]))\n", | |||
"print(pascal([5, 2, 7, 3]))" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
}, | |||
"pycharm": { | |||
"stem_cell": { | |||
"cell_type": "raw", | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"source": [] | |||
} | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,98 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie ein Python-Skript, das Studierende in einem Dictionary verwaltet. Schlüssel soll\n", | |||
"die ganzzahlige Matrikelnummer sein, der Wert der Name des Studierenden.\n", | |||
"\n", | |||
"Ihr Skript soll dabei zunächst eine Matrikelnummer einlesen. Ist sie im Dictionary vorhanden,\n", | |||
"soll der gespeicherte Name ausgegeben werden. Anschließend soll das Programm mit dem\n", | |||
"Einlesen der nächsten Matrikelnummer fortgesetzt werden.\n", | |||
"\n", | |||
"Ist die Matrikelnummer nicht vorhanden, soll der Name eingelesen und im Dictionary gespeichert\n", | |||
"werden. Anschließend geht es weiter mit der nächsten Matrikelnummer.\n", | |||
"\n", | |||
"Bei der Eingabe einer negativen Matrikelnummer soll der Studierende aus dem Dictionary\n", | |||
"entfernt werden.\n", | |||
"\n", | |||
"Wird 0 eingegeben, endet das Programm." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"is_executing": false, | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"dict = {}\n", | |||
"matrikel = None\n", | |||
"\n", | |||
"while matrikel != 0:\n", | |||
" matrikel = int(input(\"Matrikelnummer: \"))\n", | |||
" \n", | |||
" if matrikel < 0:\n", | |||
" del(dict[-matrikel])\n", | |||
" elif matrikel in dict:\n", | |||
" print(dict[matrikel])\n", | |||
" elif matrikel > 0:\n", | |||
" name = input(\"Name: \")\n", | |||
" dict[matrikel] = name\n", | |||
" \n", | |||
"print(dict)" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,234 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie eine Funktion <code>get_it_or_none1</code>, die ein Dictionary und einen\n", | |||
"Schlüsselwert als Parameter erhält. Sie soll den im Dictionary gespeicherten Wert\n", | |||
"zurückgeben, wenn er enthalten ist, andernfalls <code>None</code>.\n", | |||
"\n", | |||
"Vermeiden Sie bei der Implementierung das Auftreten einer Exception, indem Sie vor\n", | |||
"dem Zugriff prüfen, ob der Schlüssel enthalten ist." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def get_it_or_none1(dict_arg, key):\n", | |||
" if key in dict_arg:\n", | |||
" return dict_arg[key]\n", | |||
" else:\n", | |||
" return None\n", | |||
" \n", | |||
"my_dict = {'a': 1, 2: 'b'}\n", | |||
"print(get_it_or_none1(my_dict, 'a'))\n", | |||
"print(get_it_or_none1(my_dict, 'b'))" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Implementieren Sie eine Funktion <code>get_it_or_none2</code> mit der gleichen\n", | |||
"Funktionalität. Allerdings verzichten Sie dieses Mal auf die Prüfung und Fangen\n", | |||
"eine evtl. auftretende Exception, um dann <code>None</code> zurückzugeben." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def get_it_or_none2(dict_arg, key):\n", | |||
" try:\n", | |||
" return dict_arg[key]\n", | |||
" except:\n", | |||
" return None\n", | |||
" \n", | |||
"my_dict = {'a': 1, 2: 'b'}\n", | |||
"print(get_it_or_none2(my_dict, 'a'))\n", | |||
"print(get_it_or_none2(my_dict, 'b'))" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 3_\n", | |||
"\n", | |||
"Überprüfen Sie die Laufzeit der beiden Varianten, indem Sie jede Funktion\n", | |||
"10000-mal so aufrufen, dass <code>None</code> zurückgegeben wird. Was ist schneller?" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"import timeit\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"import timeit\n", | |||
"\n", | |||
"my_dict = {}\n", | |||
"print(f'get_it_or_none1:: {timeit.timeit(lambda: get_it_or_none1(my_dict, 0), number=10000)}')\n", | |||
"print(f'get_it_or_none2:: {timeit.timeit(lambda: get_it_or_none2(my_dict, 0), number=10000)}')\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 4_\n", | |||
"\n", | |||
"Tatsächlich besitzt ein Python-Dictionary eine bisher nicht eingeführte Methode\n", | |||
"<code>get</code>, der neben dem Schlüssel auch ein Default-Wert übergeben werden kann,\n", | |||
"der zurückgegeben wird, wenn der Schlüssel nicht enthalten ist.\n", | |||
"\n", | |||
"<code>dict.get( \\< key \\>, \\< default \\> ) </code>\n", | |||
"\n", | |||
"Überprüfen Sie auch die Laufzeit dieses Ansatzes." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"print(f'get_it_or_none2:: {timeit.timeit(lambda: my_dict.get(0), number=10000)}')\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Wie erklären Sie sich die Ergebnisse?" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,171 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Schreiben Sie eine Funktion *prim_sum_up_to*, die die Summe aller Primzahlen von 2 bis *n* berechnet, wobei *n* als\n", | |||
"Parameter übergeben wird." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def prim_sum_up_to(end):\n", | |||
" prims = []" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"def prim_sum_up_to(n):\n", | |||
" result = 0\n", | |||
" for m in range(2, n+1):\n", | |||
" if is_prim(m):\n", | |||
" result += m\n", | |||
" return result\n", | |||
"\n", | |||
"def is_prim(n):\n", | |||
" divisor = 2\n", | |||
" while divisor * divisor <= n:\n", | |||
" if n % divisor == 0:\n", | |||
" return False\n", | |||
" divisor += 1\n", | |||
" return True\n", | |||
"\n", | |||
"print(prim_sum_up_to(100))\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Sobald Sie die Funktion mit größeren *n* aufrufen, steigt die Laufzeit des Programms an. Um zu verhindern,\n", | |||
"dass der Anwender fälschlicherweise annimmt, dass das Programm abgestürzt ist, soll jede Sekunde ein\n", | |||
"Stern ausgegeben werden, bis das Ergebnis vorliegt.\n", | |||
"\n", | |||
"Verwenden Sie hierfür das in der Standardbibliothek enthaltene Modul <code>datetime</code> und\n", | |||
"importieren Sie daraus die Objekte <code>datetime</code> und <code>timedelta</code>.\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"from datetime import datetime, timedelta\n", | |||
"\n", | |||
"last_run = datetime.now()\n", | |||
"intervall = timedelta(seconds=1)\n", | |||
"\n", | |||
"def do_only_every_second():\n", | |||
" global last_run\n", | |||
" if last_run + intervall > datetime.now():\n", | |||
" return\n", | |||
" print(\"*\", end='')\n", | |||
" last_run = datetime.now()\n", | |||
"\n", | |||
"\n", | |||
"def is_prim(n):\n", | |||
" divisor = 2\n", | |||
" while divisor * divisor <= n:\n", | |||
" if n % divisor == 0:\n", | |||
" return False\n", | |||
" divisor += 1\n", | |||
" do_only_every_second()\n", | |||
" return True\n", | |||
"\n", | |||
"print(f'\\n{prim_sum_up_to(1000000)}')\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Vielleicht können Sie das Prinzip in einem zukünftigen Praktikum nutzen, wenn nicht bei jedem Schleifendurchlauf\n", | |||
"etwas passieren soll...." | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,137 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"collapsed": true, | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Sie entwickeln im Team ein CAD-Programm,\n", | |||
"das das Zusammenstellen komplexer Zeichnungen aus einfachen 2D-Elementen erlaubt.\n", | |||
"Gegeben ist bereits eine Klasse *Element*." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"class Element():\n", | |||
"\n", | |||
" def get_bezeichnung(self):\n", | |||
" return \"N.N.\"\n", | |||
"\n", | |||
" def umfang(self):\n", | |||
" raise NotImplementedError()\n", | |||
"\n", | |||
" def __str__(self):\n", | |||
" return self.get_bezeichnung() + f\" Umfang: {self.umfang()}\"" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"Ihr Teamleiter beauftragt Sie, für die Elemente *Kreis* und *Rechteck* Unterklassen\n", | |||
"zur Oberklasse *Element* so zu implementieren,\n", | |||
"dass anschließend die Aufrufe\n", | |||
"\n", | |||
"<code>k = Kreis(radius=3)</code>\n", | |||
"\n", | |||
"<code>r = Rechteck(breite=10, hoehe=5)</code>\n", | |||
"\n", | |||
"möglich sind.\n" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"import math\n", | |||
"\n", | |||
"\n", | |||
"k = Kreis(radius=3)\n", | |||
"print(k)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"\n", | |||
"r = Rechteck(breite=10, hoehe=5)\n", | |||
"print(r)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"_Aufgabe 2_\n", | |||
"\n", | |||
"Fügen Sie jetzt noch eine weitere Klasse *Quadrat* hinzu. Welche Oberklasse besitzt diese neue Klasse?" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"\n", | |||
"q = Quadrat(seite=4)\n", | |||
"print(q)\n", | |||
"\n", | |||
"\n", | |||
"\n" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"kernelspec": { | |||
"display_name": "Python 3", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.8.2" | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |
@@ -1,108 +0,0 @@ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"\n", | |||
"Gegeben ist eine binäre Grafikdatei <code>ohm.png</code>." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"from IPython.display import Image\n", | |||
"Image(filename=\"ohm.png\")" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%% md\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"_Aufgabe 1_\n", | |||
"\n", | |||
"Erstellen Sie ein Python-Programm, dass diese Datei in eine zweite\n", | |||
"Datei <code>logo.png</code> kopiert. Übertragen Sie die Daten bitte\n", | |||
"in 32 Byte Paketen." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": {}, | |||
"outputs": [], | |||
"source": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"metadata": { | |||
"pycharm": { | |||
"name": "#%%\n" | |||
}, | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"outputs": [], | |||
"source": [ | |||
"from IPython.display import Image\n", | |||
"\n", | |||
"\n", | |||
"with open(\"ohm.png\", \"rb\") as source_file:\n", | |||
" with open(\"logo.png\", \"wb\") as dest_file:\n", | |||
" buffer_size = 32\n", | |||
" buffer = source_file.read(buffer_size)\n", | |||
" while len(buffer)>0:\n", | |||
" dest_file.write(buffer)\n", | |||
" buffer = source_file.read(buffer_size)\n", | |||
"\n", | |||
"\n", | |||
"Image(filename=\"logo.png\")" | |||
] | |||
} | |||
], | |||
"metadata": { | |||
"celltoolbar": "Slideshow", | |||
"kernelspec": { | |||
"display_name": "Python 3 (ipykernel)", | |||
"language": "python", | |||
"name": "python3" | |||
}, | |||
"language_info": { | |||
"codemirror_mode": { | |||
"name": "ipython", | |||
"version": 3 | |||
}, | |||
"file_extension": ".py", | |||
"mimetype": "text/x-python", | |||
"name": "python", | |||
"nbconvert_exporter": "python", | |||
"pygments_lexer": "ipython3", | |||
"version": "3.9.9" | |||
} | |||
}, | |||
"nbformat": 4, | |||
"nbformat_minor": 1 | |||
} |