From 6a29bc0f5143fb4360be2bfcd072aa1dd4b8dd59 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 22 Jul 2026 12:04:02 +0200 Subject: [PATCH] Unterricht Mittwoch --- {grundlagen => python}/aufgaben_tag1.ipynb | 0 {grundlagen => python}/aufgaben_tag2.ipynb | 53 +-- python/aufgaben_tag3.ipynb | 385 ++++++++++++++++++ {grundlagen => python}/computer.py | 0 {grundlagen => python}/fraction.py | 0 {grundlagen => python}/grundlagen.ipynb | 0 {grundlagen => python}/grundlagen.py | 0 {grundlagen => python}/klassen.ipynb | 0 {grundlagen => python}/klassen.py | 0 python/lambda.ipynb | 451 +++++++++++++++++++++ {grundlagen => python}/smartphone.py | 0 {grundlagen => python}/telefon.py | 0 python/test.ipynb | 69 ++++ 13 files changed, 915 insertions(+), 43 deletions(-) rename {grundlagen => python}/aufgaben_tag1.ipynb (100%) rename {grundlagen => python}/aufgaben_tag2.ipynb (94%) create mode 100644 python/aufgaben_tag3.ipynb rename {grundlagen => python}/computer.py (100%) rename {grundlagen => python}/fraction.py (100%) rename {grundlagen => python}/grundlagen.ipynb (100%) rename {grundlagen => python}/grundlagen.py (100%) rename {grundlagen => python}/klassen.ipynb (100%) rename {grundlagen => python}/klassen.py (100%) create mode 100644 python/lambda.ipynb rename {grundlagen => python}/smartphone.py (100%) rename {grundlagen => python}/telefon.py (100%) create mode 100644 python/test.ipynb diff --git a/grundlagen/aufgaben_tag1.ipynb b/python/aufgaben_tag1.ipynb similarity index 100% rename from grundlagen/aufgaben_tag1.ipynb rename to python/aufgaben_tag1.ipynb diff --git a/grundlagen/aufgaben_tag2.ipynb b/python/aufgaben_tag2.ipynb similarity index 94% rename from grundlagen/aufgaben_tag2.ipynb rename to python/aufgaben_tag2.ipynb index a56c8d6..e76d66c 100644 --- a/grundlagen/aufgaben_tag2.ipynb +++ b/python/aufgaben_tag2.ipynb @@ -188,7 +188,7 @@ "\n", " a: Author # Typ-Hinweis\n", " for a in authors:\n", - " a.books.append(self) # Füge allen übergebenen Authors das Book hinzu.\n", + " a.books.append(self) # Füge allen übergebenen Authors das Book hinzu.\n", "\n", " @property\n", " def title(self):\n", @@ -353,12 +353,7 @@ "id": "152f0dfd54408937" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2026-07-22T06:07:02.682183793Z", - "start_time": "2026-07-22T06:07:02.631660938Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "class Artikel:\n", @@ -375,7 +370,7 @@ ], "id": "f876b667a5783072", "outputs": [], - "execution_count": 11 + "execution_count": null }, { "metadata": {}, @@ -384,12 +379,7 @@ "id": "9b3dba2c6071ca86" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2026-07-22T06:07:05.233159899Z", - "start_time": "2026-07-22T06:07:05.193137265Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "class WarenkorbItem:\n", @@ -403,7 +393,7 @@ ], "id": "1590b144e6e119a2", "outputs": [], - "execution_count": 12 + "execution_count": null }, { "metadata": {}, @@ -412,12 +402,7 @@ "id": "26737f39f9902970" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2026-07-22T06:07:07.653116857Z", - "start_time": "2026-07-22T06:07:07.603058901Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "class Warenkorb:\n", @@ -458,15 +443,10 @@ ], "id": "edcaca28fe6a6cb0", "outputs": [], - "execution_count": 13 + "execution_count": null }, { - "metadata": { - "ExecuteTime": { - "end_time": "2026-07-22T06:07:10.299292598Z", - "start_time": "2026-07-22T06:07:10.195456640Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "apfel = Artikel(1, \"Apfel\", 1.00, 10)\n", @@ -482,21 +462,8 @@ "print(warenkorb.gesamtpreis)" ], "id": "769c973f4a9ec4a5", - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "11.0\n", - "True\n", - "16.5\n", - "False\n", - "True\n", - "0\n" - ] - } - ], - "execution_count": 14 + "outputs": [], + "execution_count": null } ], "metadata": { diff --git a/python/aufgaben_tag3.ipynb b/python/aufgaben_tag3.ipynb new file mode 100644 index 0000000..07b87d9 --- /dev/null +++ b/python/aufgaben_tag3.ipynb @@ -0,0 +1,385 @@ +{ + "cells": [ + { + "metadata": { + "collapsed": true + }, + "cell_type": "markdown", + "source": [ + "# Aufgabe 1\n", + "## Aufgabenstellung\n", + "Ein Online-Shop hat eine Liste mit Produktpreisen.\n", + "\n", + "Beispiel: preise = [20, 55, 80, 30, 120]\n", + "\n", + "Produkte über 50 EUR erhalten 10% Rabatt.\n", + "\n", + "Schreiben Sie ein Programm, das mit Hilfe von Lamdba eine neue Liste mit reduzierten Preisen erstellt." + ], + "id": "897a5654dca6cca5" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Bearbeitung\n", + "### Variante A" + ], + "id": "ee68cc8aef8a90e1" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "from datetime import date\n", + "\n", + "preise = [20, 55, 80, 30, 120]\n", + "print(\"Originalpreis\")\n", + "print(preise)\n", + "\n", + "berechne_reduzierten_preis = lambda x: x * 0.9 if x > 50 else x\n", + "reduzierte_preise = list(map(berechne_reduzierten_preis, preise))\n", + "\n", + "print(\"Mit Rabatt\")\n", + "print(reduzierte_preise)" + ], + "id": "7992fb80d80b9a9d", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Variante B", + "id": "79212ee1313a3dab" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "preise = [20, 55, 80, 30, 120]\n", + "\n", + "format_preis = lambda x: f'{x:.2f} EUR'\n", + "\n", + "print(\"Originalpreis\")\n", + "print(list(map(format_preis, preise)))\n", + "\n", + "berechne_reduzierten_preis = lambda x: x * 0.9 if x > 50 else x\n", + "reduzierte_preise = list(map(berechne_reduzierten_preis, preise))\n", + "\n", + "print(\"Mit Rabatt\")\n", + "print(list(map(format_preis, reduzierte_preise)))" + ], + "id": "d0783086c8499ec", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "# Aufgabe 2\n", + "## Aufgabenstellung\n", + "Schreibe ein Programm zur Verwaltung von Mitarbeitern.\n", + "\n", + "Jeder Mitarbeiter besitzt:\n", + "- Name\n", + "- Alter\n", + "- Abteilung\n", + "- Gehalt" + ], + "id": "35d95a33d0cfa30f" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "from datetime import date\n", + "from dateutil.relativedelta import relativedelta" + ], + "id": "272c0dd3d94b644b", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "class Abteilung:\n", + "\n", + " def __init__(self, name:str, *args):\n", + " self.__name = name\n", + " super().__init__(*args)\n", + "\n", + " @property\n", + " def name(self):\n", + " return self.__name\n", + "\n", + " def __str__(self):\n", + " return self.name" + ], + "id": "90d84c95ac6ed431", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "class Mitarbeiter:\n", + "\n", + " def __init__(self, name:str, geburtsdatum:date, abteilung:Abteilung, gehalt:float, *args):\n", + " self.__name = name\n", + " self.__geburtsdatum = geburtsdatum\n", + " self.__abteilung = abteilung\n", + " self.__gehalt = gehalt\n", + " super().__init__(*args)\n", + "\n", + " @property\n", + " def name(self):\n", + " return self.__name\n", + "\n", + " @property\n", + " def alter(self):\n", + " return relativedelta(date.today(), self.__geburtsdatum).years\n", + "\n", + " @property\n", + " def gehalt(self):\n", + " return self.__gehalt\n", + "\n", + " @property\n", + " def abteilung(self):\n", + " return self.__abteilung\n", + "\n", + " def __str__(self):\n", + " return f\"{self.name}, geboren {self.__geburtsdatum} ({self.alter} Jahre alt) arbeitet in {self.abteilung} und verdient {self.gehalt:.2f} EUR.\"" + ], + "id": "e75bd9332850f6fa", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "Erstelle mindestens 6 Mitarbeiter und speichere sie in einer Liste." + ], + "id": "80f0590dbc1f3eea" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "it = Abteilung(\"IT-Abteilung\")\n", + "personal = Abteilung(\"Personalabteilung\")\n", + "vertrieb = Abteilung(\"Vertrieb\")\n", + "\n", + "mitarbeiter_list = [\n", + " Mitarbeiter(\"Albert\", date(1970, 2, 22), personal, 1234.5),\n", + " Mitarbeiter(\"Bert\", date(1980, 3, 11), it, 2345.6),\n", + " Mitarbeiter(\"Camembert\", date(2008, 1, 15), it, 1234.5),\n", + " Mitarbeiter(\"Dilbert\", date(2001, 2, 22), personal, 1234.5),\n", + " Mitarbeiter(\"Elbert\", date(1999, 3, 11), it, 2345.6),\n", + " Mitarbeiter(\"Filbert\", date(1968, 1, 15), it, 5234.5),\n", + "]" + ], + "id": "f54a24a33da9ef37", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "Das Programm soll anschließend:\n", + "\n", + "- Alle Mitarbeiter ausgeben." + ], + "id": "186cd3483d573698" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "for mitarbeiter in mitarbeiter_list:\n", + " print(mitarbeiter)" + ], + "id": "888cea3bcb830aed", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "- Alle Mitarbeiter anzeigen, die älter als 30 Jahre sind.", + "id": "b29b20967f9c3ac7" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "mitarbeiter_ueber_30 = list(filter(lambda x: x.alter > 30, mitarbeiter_list))\n", + "\n", + "for mitarbeiter in mitarbeiter_ueber_30:\n", + " print(mitarbeiter)" + ], + "id": "1a992cb2a3092ae5", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "- Das durchschnittliche Gehalt berechnen.", + "id": "aa7c0e7d3d25a806" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "summe_gehalt = sum(list(map(lambda x : x.gehalt, mitarbeiter_list)))\n", + "durchschnitt_gehalt = summe_gehalt / len(mitarbeiter_list)\n", + "\n", + "print(durchschnitt_gehalt)" + ], + "id": "3d16715679dd9a23", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "- Alle Mitarbeiter der IT-Abteilung ausgeben.", + "id": "7a99b3570a2444a3" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "mitarbeiter_in_it = list(filter(lambda x: x.abteilung == it, mitarbeiter_list))\n", + "\n", + "for mitarbeiter in mitarbeiter_in_it:\n", + " print(mitarbeiter)" + ], + "id": "bce2741c8f96e7eb", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "- Die Namen aller Mitarbeiter in Großbuchstaben ausgeben.", + "id": "51d3d88f26dc87fa" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "MITARBEITER_NAMEN = list(map(lambda x: x.name.upper(), mitarbeiter_list))\n", + "\n", + "print(MITARBEITER_NAMEN)" + ], + "id": "ceab8fc00992178", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "- Alle Mitarbeiter ausgeben, deren Gehalt über 4000 € liegt.", + "id": "2909d6f2185cd42c" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "mitarbeiter_gehalt_ueber_4000 = list(filter(lambda x:x.gehalt > 4000, mitarbeiter_list))\n", + "\n", + "for mitarbeiter in mitarbeiter_gehalt_ueber_4000:\n", + " print(mitarbeiter)" + ], + "id": "c7653e3dd84adf1", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "### Zusatzaufgabe\n", + "Gib aus, welcher Mitarbeiter das höchste Gehalt besitzt." + ], + "id": "6c2a5da8a3ffbcea" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "mitarbeiter_max_gehalt = mitarbeiter_list[0]\n", + "\n", + "for mitarbeiter in mitarbeiter_list:\n", + " if mitarbeiter_max_gehalt.gehalt < mitarbeiter.gehalt:\n", + " mitarbeiter_max_gehalt = mitarbeiter\n", + "\n", + "print(mitarbeiter_max_gehalt)\n" + ], + "id": "faaf607cc0508e17", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Alternative mit Lambda:", + "id": "13dba464d7ddd6af" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-07-22T09:46:00.972621400Z", + "start_time": "2026-07-22T09:46:00.935546200Z" + } + }, + "cell_type": "code", + "source": [ + "mitarbeiter_max_gehalt = max(mitarbeiter_list, key= lambda x: x.gehalt)\n", + "print(mitarbeiter_max_gehalt)" + ], + "id": "1946de57dee64b91", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Filbert, geboren 1968-01-15 (58 Jahre alt) arbeitet in IT-Abteilung und verdient 5234.50 EUR.\n" + ] + } + ], + "execution_count": 9 + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/grundlagen/computer.py b/python/computer.py similarity index 100% rename from grundlagen/computer.py rename to python/computer.py diff --git a/grundlagen/fraction.py b/python/fraction.py similarity index 100% rename from grundlagen/fraction.py rename to python/fraction.py diff --git a/grundlagen/grundlagen.ipynb b/python/grundlagen.ipynb similarity index 100% rename from grundlagen/grundlagen.ipynb rename to python/grundlagen.ipynb diff --git a/grundlagen/grundlagen.py b/python/grundlagen.py similarity index 100% rename from grundlagen/grundlagen.py rename to python/grundlagen.py diff --git a/grundlagen/klassen.ipynb b/python/klassen.ipynb similarity index 100% rename from grundlagen/klassen.ipynb rename to python/klassen.ipynb diff --git a/grundlagen/klassen.py b/python/klassen.py similarity index 100% rename from grundlagen/klassen.py rename to python/klassen.py diff --git a/python/lambda.ipynb b/python/lambda.ipynb new file mode 100644 index 0000000..a0af8ed --- /dev/null +++ b/python/lambda.ipynb @@ -0,0 +1,451 @@ +{ + "cells": [ + { + "metadata": { + "collapsed": true + }, + "cell_type": "markdown", + "source": [ + "# Funktionale Programmierung in Python\n", + "\n", + "## Ziel dieser Einheit\n", + "\n", + "In dieser Einheit lernst du:\n", + "\n", + "- was funktionale Programmierung bedeutet\n", + "- wie Lambda-Ausdrücke in Python funktionieren\n", + "- wie filter() verwendet wird\n", + "- wie map() verwendet wird\n", + "- wie mehrere funktionale Operationen kombiniert werden\n", + "- wie List Comprehensions als pythonische Alternative funktionieren" + ], + "id": "aba2c4fa1f75f62d" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## 1. Grundidee der funktionalen Programmierung\n", + "\n", + "Funktionale Programmierung bedeutet:\n", + "\n", + "- Wir verändern Daten nicht direkt\n", + "- Wir benutzen Funktionen, um neue Daten zu erzeugen\n", + "- Häufig genutzte Funktionen sind:\n", + " - filter() → filtern\n", + " - map() → umwandeln\n", + " - sum() → berechnen\n", + "\n", + "In diesem Beispiel arbeiten wir mit einer Liste von Zahlen." + ], + "id": "5fe9a211e4e1f290" + }, + { + "metadata": {}, + "cell_type": "code", + "source": "zahlen = [1, 2, 3, 4, 5, 6]", + "id": "eb8147e500b5876e", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "Die ursprüngliche Liste bleibt dabei unverändert.\n", + "Stattdessen erzeugen wir neue Listen mit den gewünschten Ergebnissen." + ], + "id": "ce47b4e7ceac7b2b" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## 2. Lambda-Ausdrücke\n", + "\n", + "Ein Lambda-Ausdruck ist eine kurze, anonyme Funktion.\n", + "\n", + "Anonym bedeutet:\n", + "\n", + "- die Funktion hat keinen eigenen Namen\n", + "- sie wird direkt dort definiert, wo sie gebraucht wird\n", + "\n", + "Beispiel:" + ], + "id": "308f2ba46c30f8bd" + }, + { + "metadata": {}, + "cell_type": "code", + "source": "lambda x: x % 2 == 0", + "id": "36ecb9f8790e3ef3", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "Bedeutung:\n", + "\n", + "- x ist die Variable für die Funktion\n", + "- x % 2 == 0 prüft, ob eine Zahl gerade ist\n", + "- das Ergebnis ist True oder False\n", + "\n", + "Vergleich mit Java:\n", + "\n", + " (x) -> x % 2 == 0\n", + "\n", + "In Java würde ein ähnlicher Lambda-Ausdruck also mit Pfeil geschrieben werden." + ], + "id": "ab7b62549d06d131" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## 3. Filtern mit filter()\n", + "\n", + "Zuerst sollen nur die geraden Zahlen aus der Liste ausgewählt werden." + ], + "id": "4f313a6ccf2b179e" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-07-22T08:15:25.729179Z", + "start_time": "2026-07-22T08:15:25.652826100Z" + } + }, + "cell_type": "code", + "source": [ + "gerade_zahlen = list(filter(lambda x: x % 2 == 0, zahlen))\n", + "\n", + "print(\"Gerade Zahlen:\", gerade_zahlen)" + ], + "id": "405e64898d7dd020", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gerade Zahlen: [2, 4, 6]\n" + ] + } + ], + "execution_count": 10 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "Erklärung:\n", + "\n", + "- filter() bekommt eine Funktion und eine Liste\n", + "- die Funktion prüft jedes Element\n", + "- nur Elemente, bei denen die Funktion True zurückgibt, bleiben erhalten\n", + "- list() wandelt das Ergebnis wieder in eine Liste um\n", + "\n", + "In diesem Beispiel prüft:\n", + "\n", + " lambda x: x % 2 == 0\n", + "\n", + "ob eine Zahl gerade ist.\n", + "\n", + "Aus der Liste:\n", + "\n", + " [1, 2, 3, 4, 5, 6]\n", + "\n", + "wird dadurch:\n", + "\n", + " [2, 4, 6]" + ], + "id": "a7786217eb1ebd0f" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Aufgabe zur Übung\n", + "Eine Liste mit den Zahlen von 1 bis 100 so filtern, dass alle Zahlen ausgegeben werden, die durch 12 oder 17 teilbar sind" + ], + "id": "10bddadd2af9878" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "zahlen_liste = []\n", + "for i in range (1, 101):\n", + " zahlen_liste.append(i)\n", + "\n", + "durch_12_oder_17_teilbar = list(filter(lambda x: x % 12 == 0 or x % 17 == 0, zahlen_liste))\n", + "print(durch_12_oder_17_teilbar)" + ], + "id": "a06029e472a8f213", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## 4. Umwandeln mit map()\n", + "\n", + "Als nächstes sollen alle Zahlen quadriert werden." + ], + "id": "84ceeb61fc5b523a" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "quadriere = lambda x: x * x\n", + "\n", + "quadrate = list(\n", + " map(quadriere, zahlen)\n", + ")\n", + "\n", + "print(\"Quadrate:\", quadrate)" + ], + "id": "5789adc3dd4af6f8", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "Erklärung:\n", + "\n", + "- map() wendet eine Funktion auf jedes Element an\n", + "- hier wird jede Zahl mit sich selbst multipliziert\n", + "- dadurch entsteht eine neue Liste mit den Quadraten\n", + "\n", + "Die Funktion:\n", + "\n", + " lambda x: x * x\n", + "\n", + "berechnet für jede Zahl ihr Quadrat.\n", + "\n", + "Aus:\n", + "\n", + " [1, 2, 3, 4, 5, 6]\n", + "\n", + "wird:\n", + "\n", + " [1, 4, 9, 16, 25, 36]" + ], + "id": "7210ac5691299df5" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Aufgabe zur Übung\n", + "Eine Map erstellen, die jede Zahl in einer Liste mit drei multipliziert:" + ], + "id": "2885e847dce66b9b" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "multipliziere_mit_drei = lambda x: 3 * x\n", + "\n", + "zahlen_mal_drei = list(map(multipliziere_mit_drei, zahlen))\n", + "print(zahlen_mal_drei)" + ], + "id": "a327676c343bd9b1", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## 5. Kombinieren von filter() und map()\n", + "\n", + "Funktionale Operationen können kombiniert werden.\n", + "\n", + "In diesem Beispiel sollen zuerst nur die geraden Zahlen gefiltert und danach quadriert werden." + ], + "id": "ac187b86ff0d3bc5" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "ergebnis = list(\n", + " map(\n", + " lambda x: x * x,\n", + " filter(lambda x: x % 2 == 0, zahlen)\n", + " )\n", + ")\n", + "\n", + "print(\"Quadrate der geraden Zahlen:\", ergebnis)" + ], + "id": "5c41c5c137fa4b65", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "Ablauf:\n", + "\n", + "1. filter() wählt nur gerade Zahlen aus\n", + "2. map() quadriert diese Zahlen\n", + "3. list() erzeugt daraus eine Liste\n", + "\n", + "Zwischenschritt:\n", + "\n", + " filter(lambda x: x % 2 == 0, zahlen)\n", + "\n", + "ergibt:\n", + "\n", + " [2, 4, 6]\n", + "\n", + "Danach wird mit map() quadriert:\n", + "\n", + " [4, 16, 36]\n", + "\n", + "## 6. Python-typische Schreibweise: List Comprehension\n", + "\n", + "Die gleiche Aufgabe kann in Python auch mit einer List Comprehension geschrieben werden." + ], + "id": "1c43c327479ca804" + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "ergebnis_comp = [x * x for x in zahlen if x % 2 == 0]\n", + "\n", + "print(\"Mit List Comprehension:\", ergebnis_comp)" + ], + "id": "a80de265be2ebfa5", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "Erklärung:\n", + "\n", + "- Das ist die pythonischere Variante\n", + "- sie ist oft besser lesbar als map() und filter()\n", + "- sie kombiniert Filtern und Umwandeln in einer Zeile\n", + "\n", + "Die Struktur lautet:\n", + "\n", + " neuer_wert for element in liste if bedingung\n", + "\n", + "In unserem Beispiel:\n", + "\n", + " x * x for x in zahlen if x % 2 == 0\n", + "\n", + "Bedeutung:\n", + "\n", + "- gehe durch alle Zahlen in zahlen\n", + "- verwende nur Zahlen, die gerade sind\n", + "- berechne für jede dieser Zahlen x * x\n", + "\n", + "Das Ergebnis ist wieder:\n", + "\n", + " [4, 16, 36]\n", + "\n", + "\n", + "## 7. Wichtige Konzepte im Überblick\n", + "\n", + "### Funktionale Programmierung\n", + "\n", + "Funktionale Programmierung beschreibt Berechnungen mit Funktionen.\n", + "\n", + "Die ursprünglichen Daten werden dabei möglichst nicht verändert.\n", + "Stattdessen werden neue Daten erzeugt.\n", + "\n", + "\n", + "### Lambda-Ausdruck\n", + "\n", + "Ein Lambda-Ausdruck ist eine kurze Funktion ohne Namen.\n", + "\n", + "Er eignet sich besonders für kleine Funktionen, die nur einmal gebraucht werden.\n", + "\n", + "\n", + "### filter()\n", + "\n", + "filter() wählt Elemente aus einer Sammlung aus.\n", + "\n", + "Die übergebene Funktion muss True oder False zurückgeben.\n", + "\n", + "\n", + "### map()\n", + "\n", + "map() wandelt jedes Element einer Sammlung um.\n", + "\n", + "Die übergebene Funktion bestimmt, wie jedes Element verändert wird.\n", + "\n", + "\n", + "### List Comprehension\n", + "\n", + "List Comprehensions sind eine typische Python-Schreibweise.\n", + "\n", + "Sie sind oft lesbarer als verschachtelte map()- und filter()-Aufrufe.\n", + "\n", + "\n", + "## Zusammenfassung\n", + "\n", + "- Funktionale Programmierung arbeitet mit Funktionen statt direkter Veränderung von Daten\n", + "- Lambda-Ausdrücke sind kurze anonyme Funktionen\n", + "- filter() filtert Elemente anhand einer Bedingung\n", + "- map() wandelt Elemente um\n", + "- map() und filter() können kombiniert werden\n", + "- List Comprehensions sind in Python oft die bevorzugte Schreibweise\n", + "\n", + "\n", + "## Verständnisfragen\n", + "\n", + "- Was ist ein Lambda-Ausdruck?\n", + "- Wofür wird filter() verwendet?\n", + "- Wofür wird map() verwendet?\n", + "- Warum wird list() um filter() oder map() geschrieben?\n", + "- Was ist der Vorteil einer List Comprehension?" + ], + "id": "239738006efd56b5" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/grundlagen/smartphone.py b/python/smartphone.py similarity index 100% rename from grundlagen/smartphone.py rename to python/smartphone.py diff --git a/grundlagen/telefon.py b/python/telefon.py similarity index 100% rename from grundlagen/telefon.py rename to python/telefon.py diff --git a/python/test.ipynb b/python/test.ipynb new file mode 100644 index 0000000..d9eecee --- /dev/null +++ b/python/test.ipynb @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "metadata": {}, + "cell_type": "markdown", + "source": "# Test", + "id": "5f992ca0ee06c01c" + }, + { + "metadata": { + "collapsed": true, + "ExecuteTime": { + "end_time": "2026-07-22T08:14:47.152767500Z", + "start_time": "2026-07-22T08:14:47.121874300Z" + } + }, + "cell_type": "code", + "source": [ + "a = 50\n", + "print(50)" + ], + "id": "cc6e758282212d7a", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n" + ] + } + ], + "execution_count": 1 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2026-07-22T08:15:04.685063100Z", + "start_time": "2026-07-22T08:15:04.670671300Z" + } + }, + "cell_type": "code", + "source": "a = a + 10", + "id": "7359f0aee5a53934", + "outputs": [], + "execution_count": 2 + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}