.orig gelöscht
parent
2770563f1f
commit
9f21f266e4
|
@ -1,242 +0,0 @@
|
|||
{
|
||||
"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."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
|
@ -1,271 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "df41324e-da47-4d61-a80d-a7bd29198c8d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Texte\n",
|
||||
"\n",
|
||||
"Bisher haben wir nur mit Zahlen gearbeitet, d.h. Python als besseren Taschenrechner verwendet.\n",
|
||||
"Jetzt wollen wir auch Texte verwenden.\n",
|
||||
"\n",
|
||||
"Text wird auch Zeichenkette oder String genannt.\n",
|
||||
"\n",
|
||||
"Texte haben wir auch schon gesehen, z.B.:\n",
|
||||
"\n",
|
||||
"* Variablennamen `a`, `b`, `c`\n",
|
||||
"* Funktionsnamen wie `my_min` und `cos`\n",
|
||||
"\n",
|
||||
"Um Texte, mit denen wir rechnen wollen, von Variablen und Funktionen zu unterscheiden, müssen wir diese in \" oder ' einschließen."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "7ae417ae-c8d5-4f0e-a280-16265f3fa2d3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a = 'Wort'\n",
|
||||
"b = \"Zwei Worte\"\n",
|
||||
"c = \"\"\"Ein mehrzeiliger Text\n",
|
||||
"muss in\n",
|
||||
"drei Hochkommatas\n",
|
||||
"eingeschlossen werden.\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "8af71ec8-1c40-4106-bbd4-3b0bdd2968f7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Wort'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "14c7d057-124d-4e3b-b6be-a966687930e1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Ein mehrzeiliger Text\\nmuss in\\ndrei Hochkommatas\\neingeschlossen werden.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"c"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2e280d2d-eda2-473d-8997-94de82444f48",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"ℹ️ mit \\ lassen sich Sonderzeichen darstellen\n",
|
||||
"\n",
|
||||
"* \\\\ = \\\n",
|
||||
"* \\n = Zeilenumbruch\n",
|
||||
"* \\t = Tabulator"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "41dc4823-c5f0-4ffb-a6c6-c091789cf309",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Aufgabe\n",
|
||||
"❓ Was kann man alles mit Texten machen?\n",
|
||||
"\n",
|
||||
"Tipp:\n",
|
||||
"* `c.<TAB>`\n",
|
||||
"* `help(str)`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a376173a-66db-485f-acfe-818c44ad7320",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Lösung\n",
|
||||
"Die Aufgabe war so einfach, die braucht keine Lösung."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d6a2bb1b-120c-48bf-aed5-ec11fe83f50a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Aufgabe\n",
|
||||
"❓ Schreibt eine Funktion, die als Parameter ein englisches Wort akzeptiert und dessen Pluralform zurückliefert."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "59669fc1-2dc6-43a6-a297-3304f190d8ea",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Lösung\n",
|
||||
"Wir ignorieren die Sonderfälle und beschränken uns darauf, dBuchstaben **s** anzufügen wenn das Wort nicht damit endet."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "45aa62f3-1257-4499-b2fb-b818cc1dbd21",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def plural(s):\n",
|
||||
" if s.endswith('s'):\n",
|
||||
" return s\n",
|
||||
" else:\n",
|
||||
" return s + 's'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "ddfbc878-4c16-48a8-852c-97379f447ab9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'horses'"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plural(\"horse\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0e1833b4-aa3b-41b4-9602-0e36103e0070",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Rechnen mit Texten\n",
|
||||
"Texte kann man addieren und multiplizieren:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "17a904df-8ffc-467a-8744-e2a4fd8b2134",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Hallo Python'"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = \"Hallo\"\n",
|
||||
"b = \"Python\"\n",
|
||||
"a + ' ' + b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "8c7599a2-c199-433b-bc74-5da51688bdd1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'HalloHalloHallo'"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a * 3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "854d1c91-d4ea-478f-822e-3a3736fb5ff3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Das kann man z.B. nutzen, um einen langen Strich zu erzeugen:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "ef7ad859-36f3-46a4-9f5c-972eb45ad0e7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'________________________________________________________________________________'"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"'_' * 80"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
Loading…
Reference in New Issue