251 lines
5.9 KiB
Plaintext
251 lines
5.9 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "5e2e3e9d-9d88-43b8-a7b6-351a8dd8eb9d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Funktionen\n",
|
||
|
"Wenn es `math.radians()` nicht gäbe, dann könnten wir die uns selbst schreiben:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "5e0982b9-eb6d-4db7-ba96-cceac29821ae",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import math\n",
|
||
|
"def rad(x):\n",
|
||
|
" return x * 2 * math.pi / 360"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "38cc7e3f-8186-4ed8-81ae-d199c3cb9d5e",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Zur Unterscheidung heißt sie einfach nur `rad()`.\n",
|
||
|
"Das Package `math` muss importiert sein um `pi` zu verwenden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "333d5172-b326-460e-9851-6e5271bcab79",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"3.141592653589793"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"rad(180)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "1b67ecf5-cf68-4c29-ae90-39d2a0f6ce00",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Aufgabe\n",
|
||
|
"❓ Das sieht schon toll aus. Geht das aber auch besser?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b792d4e1-41ce-4622-8298-077355437419",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Lösung\n",
|
||
|
"Beim Programmieren ist es oft nicht leicht zu entscheiden was **besser** ist.\n",
|
||
|
"\n",
|
||
|
"Man könnnte `x * math.pi / 180` schreiben. Das spart evtl. die Berechnung von `2 / 360`.\n",
|
||
|
"Warum `evtl`? Weil manche Programmiersprachen dies selbständig optimieren.\n",
|
||
|
"Warum ist das kürzere nicht besser? Die Formel, die man in der Schule gelernt hat ist so nicht direkt wiederzuerkennen und somit das Programm schwerer zu verstehen.\n",
|
||
|
"\n",
|
||
|
"Wichtig ist aber immer eine gute Dokumentation von Funktionen."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "2ec6f0f0-4109-4aef-92dd-d247c43cc4b3",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Help on function rad in module __main__:\n",
|
||
|
"\n",
|
||
|
"rad(x)\n",
|
||
|
"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"help(rad)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "537bad50-0e57-463c-9d70-b6c0b60de395",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Mmh, das sieht nicht so toll aus.\n",
|
||
|
"\n",
|
||
|
"Es braucht einen [Docstring](https://www.python.org/dev/peps/pep-0257/),\n",
|
||
|
"um hier etwas vernünftiges angezeigt zu bekommen."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b5f77780-5a55-4471-9bed-7295e95f34a3",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Aufgabe\n",
|
||
|
"Schreibt die Funktion `rad()` so um, dass `help(rad)` einen hilfreichen Text anzeigt."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "4c1d9d64-00bf-4418-8416-5fd7c3eba36c",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Lösung\n",
|
||
|
"Wir verwenden einfach den Hilfetext von `math.rad`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"id": "ab898f30-4161-49c8-b8be-e9631349bb2a",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def rad(x):\n",
|
||
|
" \"\"\"Convert angle x from degrees to radians.\"\"\"\n",
|
||
|
" return x * 2 * math.pi / 360"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "de201d97-2f1d-4798-9732-77c40ba99b78",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Aufgabe\n",
|
||
|
"Schreibt eine Funktion, die die Seitenlänge eines rechtwinkligen Dreiecks anhand des Satzes von Pythagoras berechnet. Zur Erinnerung, der lautet a² + b² = c².\n",
|
||
|
"\n",
|
||
|
"Überlegt:\n",
|
||
|
"* Was sind die Parameter?\n",
|
||
|
"* Was soll das Ergebnis sein?\n",
|
||
|
"* Wie kann man das Berechnen?\n",
|
||
|
"* Wie soll die Funktion heißen?\n",
|
||
|
"* Was soll die Hilfe zeigen"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"id": "1f69e422-69dc-4563-8ca3-2e8a15a8ee74",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def pyth(a, b):\n",
|
||
|
" \"\"\"Berechnet Hypothenuse eines rechtwinkligen Dreiecks mit Seitenlängen a und b\"\"\"\n",
|
||
|
" return math.sqrt(a*a + b*b)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"id": "8cb40698-547a-4abd-a076-a14e05584a2c",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"5.0"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"pyth(3, 4)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "26f52ec4-a4aa-4b96-a247-2c0816e10a39",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Aufgabe\n",
|
||
|
"❓ Schreibt eine Funktion, die die kleinere von zwei Zahlen zurück gibt."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c724390e-e029-4bd3-be92-3e0a1ffac267",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Lösungen\n",
|
||
|
"**Lösung 1**\n",
|
||
|
"\n",
|
||
|
"Das ist unnötig. Die Funktion gibt es schon. Sie heißt `min()`\n",
|
||
|
"\n",
|
||
|
"**Lösug 2**\n",
|
||
|
"\n",
|
||
|
"Das ist zwar unnötig, aber wenn es denn sein muss:\n",
|
||
|
"\n",
|
||
|
" def my_min(a, b):\n",
|
||
|
" return min(a, b)\n",
|
||
|
"\n",
|
||
|
"**Lösung 3**\n",
|
||
|
"\n",
|
||
|
"Wie man das selbst implementiert lernen wir erst im nächsten Kapitel."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "892db341-2524-4f29-918c-9dd035b07982",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"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.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|