{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Jupyter-Notebook-Grundlagen\" data-toc-modified-id=\"Jupyter-Notebook-Grundlagen-1\"><span class=\"toc-item-num\">1 </span>Jupyter Notebook Grundlagen</a></span><ul class=\"toc-item\"><li><span><a href=\"#Standardbrowser\" data-toc-modified-id=\"Standardbrowser-1.1\"><span class=\"toc-item-num\">1.1 </span>Standardbrowser</a></span><ul class=\"toc-item\"><li><span><a href=\"#Anleitungen\" data-toc-modified-id=\"Anleitungen-1.1.1\"><span class=\"toc-item-num\">1.1.1 </span>Anleitungen</a></span></li></ul></li><li><span><a href=\"#Shortcuts\" data-toc-modified-id=\"Shortcuts-1.2\"><span class=\"toc-item-num\">1.2 </span>Shortcuts</a></span><ul class=\"toc-item\"><li><span><a href=\"#Beispiel:-Markdown-Zelle-vs.-Code-Zelle\" data-toc-modified-id=\"Beispiel:-Markdown-Zelle-vs.-Code-Zelle-1.2.1\"><span class=\"toc-item-num\">1.2.1 </span>Beispiel: Markdown Zelle vs. Code Zelle</a></span></li></ul></li></ul></li><li><span><a href=\"#Python-Grundlagen\" data-toc-modified-id=\"Python-Grundlagen-2\"><span class=\"toc-item-num\">2 </span>Python Grundlagen</a></span><ul class=\"toc-item\"><li><span><a href=\"#Grundlegende-Datentypen\" data-toc-modified-id=\"Grundlegende-Datentypen-2.1\"><span class=\"toc-item-num\">2.1 </span>Grundlegende Datentypen</a></span></li><li><span><a href=\"#Listen\" data-toc-modified-id=\"Listen-2.2\"><span class=\"toc-item-num\">2.2 </span>Listen</a></span></li><li><span><a href=\"#Mengen\" data-toc-modified-id=\"Mengen-2.3\"><span class=\"toc-item-num\">2.3 </span>Mengen</a></span></li><li><span><a href=\"#Ranges\" data-toc-modified-id=\"Ranges-2.4\"><span class=\"toc-item-num\">2.4 </span>Ranges</a></span></li><li><span><a href=\"#Tupel\" data-toc-modified-id=\"Tupel-2.5\"><span class=\"toc-item-num\">2.5 </span>Tupel</a></span></li><li><span><a href=\"#Dictionaries\" data-toc-modified-id=\"Dictionaries-2.6\"><span class=\"toc-item-num\">2.6 </span>Dictionaries</a></span></li><li><span><a href=\"#If-Anweisungen\" data-toc-modified-id=\"If-Anweisungen-2.7\"><span class=\"toc-item-num\">2.7 </span>If-Anweisungen</a></span></li><li><span><a href=\"#Schleifen\" data-toc-modified-id=\"Schleifen-2.8\"><span class=\"toc-item-num\">2.8 </span>Schleifen</a></span></li><li><span><a href=\"#Funktionen\" data-toc-modified-id=\"Funktionen-2.9\"><span class=\"toc-item-num\">2.9 </span>Funktionen</a></span></li></ul></li><li><span><a href=\"#----------------Praktisch-für-Python,-aber-nicht-für-unseren-Kurs------------------\" data-toc-modified-id=\"----------------Praktisch-für-Python,-aber-nicht-für-unseren-Kurs-------------------3\"><span class=\"toc-item-num\">3 </span><font color=\"red\">--------------- Praktisch für Python, aber nicht für unseren Kurs -----------------</font></a></span><ul class=\"toc-item\"><li><span><a href=\"#Lambda-Funktionen\" data-toc-modified-id=\"Lambda-Funktionen-3.1\"><span class=\"toc-item-num\">3.1 </span>Lambda-Funktionen</a></span></li><li><span><a href=\"#List-Comprehensions\" data-toc-modified-id=\"List-Comprehensions-3.2\"><span class=\"toc-item-num\">3.2 </span>List Comprehensions</a></span></li><li><span><a href=\"#Klassen\" data-toc-modified-id=\"Klassen-3.3\"><span class=\"toc-item-num\">3.3 </span>Klassen</a></span></li></ul></li></ul></div>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grundlagen Jupyter Notebooks\n", "\n", "Falls Sie im Python-Programmierkurs sind und dies ihr erstes geöffnetes Jupyter Notebook ist: Herzlichen Glückwunsch! :)\n", "\n", "Falls Sie im Python-Programmierkurs sind und dies ihr erstes geöffnetes Jupyter Notebook ist, lesen Sie nur bis bis zum Kapitel \"Python.Grundlagen\". Danach wechseln Sie zum Notebook [seminar00.ipynb](seminar00.ipynb).\n", "\n", "- Jupyter ist freie Software – ein browserbasiertes Tool für verschiedene Programmiersprachen (wir benutzen Python).\n", "- Es stellt Quellcode, Visualisierungen, Text und Erklärungen in einem (Web-)Dokument – dem Jupyter Notebook – dar.\n", "- Notebooks bestehen aus Blöcken bzw. \"Zellen\".\n", "- Es gibt zwei Arten von Blöcken: \"Code\" ist für Python-Code, \"Markdown\" ist für Texte, die Sie mit Hilfe der [Markdown-Syntax](https://de.wikipedia.org/wiki/Markdown#Auszeichnungsbeispiele) formatieren können.\n", "- Blöcke können \"ausgeführt\" werden, dabei wird der Python-Code ausgeführt und Markdown in HTML umgewandelt und angezeigt.\n", "- Unterhalb eines Blocks mit Quellcode wird der Rückgabewert der letzten Anweisung ausgegeben.\n", "- Quellcode kann auf mehrere Blöcke aufgeteilt werden (Variablen behalten ihre Gültigkeit/Sichtbarkeit).\n", "- Sie können auf Text doppelt klicken, um sich den Markdown-Quellcode anzuzeigen. Probieren Sie es mit diesem Text aus.\n", "- Durch die Tastenkombination \"Strg\" und \"Enter\" oder durch Drücken des \"Run\"-Buttons oben im Menü kommen Sie wieder in den Lesemodus \n", "- Wenn Sie mal etwas \"kaputtgespielt\" haben, hilft es evtl., im \"Kernel\"-Menü den \"Restart\"-Eintrag auszuwählen.\n", "\n", "\n", "\n", "## Standardbrowser\n", "\n", "Bitte verwenden Sie **nicht** Safari oder Internet Explorer. Auch mit Windows Edge gab es in der Vergangenheit Probleme. Wir empfehlen die Verwendung von Firefox, aber auch Google Chrome lief in der Vergangenheit problemlos. Wenn Sie unsicher sind, was ihr aktueller Standardbrowser ist, folgen Sie einfach der folgenden Anleitung zum Ändern des Standardbrowsers und gehen Sie sicher, dass der richtige Standardbrowser ausgewählt ist.\n", "\n", "### Anleitungen\n", "\n", "**Änderung des Standardbrowsers in...** \n", "\n", "*macOS:* \n", "1. Öffnen sie die Systemeinstellungen.\n", "2. Klicken Sie auf „Allgemein“.\n", "3. Wählen Sie unter „Standard-Webbrowser“ den gewünschten Browser aus.\n", "\n", "*Ubuntu Linux:* \n", "1. Öffnen Sie die System Settings.\n", "2. Klicken Sie auf „Applications“.\n", "3. Wählen Sie in der linken Spalte „Default Applications“ aus.\n", "4. Klicken Sie in der Spalte rechts davon auf „Web Browser“.\n", "5. Wählen Sie „in the following browser:“ aus.\n", "6. Wählen Sie den gewünschten Browser aus.\n", "\n", "*Windows:* \n", "1. Öffnen Sie die Systemsteuerung.\n", "2. Klicken Sie auf „Standardprogramme“.\n", "3. Klicken Sie auf „Standardprogramme festlegen“.\n", "4. Klicken Sie in der Liste auf den gewünschten Browser.\n", "5. Klicken Sie dann auf „Dieses Programm als Standard festlegen“.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Shortcuts\n", "\n", "**Bitte probieren Sie alle unten stehenden Befehle und verstehen Sie, was gemacht wird. Das ist die Basis, die Sie für das Arbeiten mit Jupyter Notebooks brauchen.**\n", "\n", "Übersicht: *Menü Help -> Keyboard Shortcuts*\n", "\n", "Wichtigste Shortcuts: \n", "\n", "- *Enter*: Editiermodus für selektierte Zelle (grün umrandet)\n", "- *Esc*: Editiermodus verlassen/Kommandomodus (blau umrandet)\n", "- *Strg + Enter*: Selektierte Zelle ausführen\n", "- *Shift + Enter*: Selektierte Zelle ausführen und in nächste Zelle springen\n", "\n", "Im Editiermodus:\n", "- *Tab*: Autocomplete oder Einrücken\n", "- *Shift + Tab*: Einrücken rückwärts\n", "\n", "Im Kommando-/Lesemodus:\n", "- *B*: Zelle darunter einfügen\n", "- *A*: Zelle darüber einfügen\n", "- *DD*: Zelle löschen\n", "- *M*: Zelltyp Markdown (für formatierte beschreibende Texte, zB diese Zelle)\n", "- *Y*: Zelltyp Code (Default)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Beispiel: Markdown Zelle vs. Code Zelle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dies ist eine **Markdown Zelle**. Hier kann jeglicher Text geschrieben werden. Durch Strg+Enter wird er in den Lesemodus gebracht, durch ein einfach Enter (oder Doppelklick) in den Editiermodus.\n", "\n", "Hier werden keine Rechnungen ausgeführt, siehe:\n", "\n", "5+3\n", "\n", "In diesen Feldern werden Erklärungen, Aufgabenstellungen und schriftliche Antworten von euch stehen." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Das ist eine **Code Zelle**, bei Strg+Enter wird die folgende Rechnung ausgeführt und darunter angezeigt\n", "# Mit Enter kommt man in den Editiermodus, mit Strg+Enter wird der Code ausgeführt.\n", "# Der Text kann hier nur stehen, da eine Raute am Anfang der Zeile steht. \n", "# Dadurch werden diese Zeilen nicht ausgeführt.\n", "\n", "5+3\n", "\n", "# In diesen Zellen wird der Python Code geschrieben, manchmal müssen Sie ihn schreiben, \n", "# manchmal steht der Code schon dort und muss von Ihnen nur ausgeführt werden." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python-Grundlagen\n", "- Dynamisch typisierte Sprache\n", "- Statt geschweifter Klammern für Codeblöcke (Funktionen, Schleifen) wird Code mit vier Leerzeichen (pro Hierarchieebene) eingerückt\n", "- Die meisten Editoren ersetzen Tab automatisch durch vier Leerzeichen\n", "- Kommentare beginnen mit Raute #" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grundlegende Datentypen\n", "Zahlentypen (int/float) sind größtenteils äquivalent zu Java. Strings können mit doppelten oder einfachen Anführungszeichen deklariert werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Variablen definieren\n", "\n", "a = 2*2.0\n", "b = 5\n", "\n", "# Ausgabe \n", "\n", "print(a + b)\n", "\n", "# Diese Zelle auswählen und mit `<SHIFT> + <ENTER>` ausführen.\n", "# Der letzte Rückgabewert der Zelle wird dann unten ausgegeben,\n", "# hier also der Wert von `a+b`:b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Die grundlegenden Rechenoperationen __`+`, `-`, `*`, `/`__ und __`**`__ sind ebenfalls in Python verfügbar und verhalten sich, wie man es erwartet:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# äquivalent zu a = a + 1\n", "a += 1\n", "\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Potenzieren\n", "\n", "2**4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Division\n", "\n", "2/4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Es gibt verschiedene Variablentypen:\n", " - int (Ganzzahl, z.B. 1, 20, 52432432)\n", " - float (Gleitkommazahl, z.B. 1.423, 1/3, 0.23487235723)\n", " - str (Zeichenkette, z.B. \"Hello World\", 'this is a string')\n", " - bool (Wahrheitswerte, z.B. True, False)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Zeichenketten bzw. strings\n", "\n", "text1 = 'Hallo '\n", "text2 = \"Welt\"\n", "\n", "print (text1 + text2)\n", "\n", "# Andere Datentypen müssen explizit in Strings konvertiert werden,\n", "# wenn sie an einen String angehängt werden sollen.\n", "print(text1 + str(1))\n", "\n", "# oder man muss sie durch Kommata trennen:\n", "\n", "print(text1,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Zeichenkette zu float \n", "float('1')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# integer zu float \n", "\n", "float(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*None* entspricht *null* in Java." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myvar = None" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(myvar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if myvar is None:\n", " print('x ist nicht definiert')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Listen\n", "Listen werden mit eckigen Klammern oder list() initialisiert." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3,3]\n", "l.append(4)\n", "\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vorsicht bei der Kombination von Listen. append hängt die übergebene Variable als einzelnen Listeneintrag an die Liste an." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l.append([5, 6])\n", "\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zur Kombination von zwei Listen wird der +-Operator verwendet." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l2 = l + [5,6]\n", "\n", "print(l2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mengen\n", "Mengen können mit geschweiften Klammern oder set() initialisiert werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1,2,3,3}\n", "s2 = set([2,3,4,5])\n", "\n", "print(s)\n", "print(s.intersection(s2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.add(9)\n", "s.remove(2)\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mit list() und set() können die Typen auch ineinander konvertiert werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ranges\n", "Auflistung von Zahlen, werden z.Bsp. für for-Schleifen mit Index verwendet." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tupel\n", "Für sortierete Werte mit fester Komponentenanzahl und fester Bedeutung für jeden Eintrag." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "essen = 'Chili'\n", "preis = 2.51\n", "boneintrag = (essen, preis)\n", "\n", "print(boneintrag)\n", "print(boneintrag[0])\n", "print(boneintrag[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(essen2, preis2) = boneintrag\n", "\n", "print(essen2)\n", "print(preis2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Klammern können oft weggelassen werden" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "boneintrag = essen, preis\n", "essen2, preis2 = boneintrag\n", "\n", "print(boneintrag)\n", "print(essen2)\n", "print(preis2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries\n", "Äquivalent zu Maps." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = {'Chili': 1.90, 'Penne': 2.50}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d.values()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key, val in d.items():\n", " print('{}: {}'.format(key, val))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['Chili']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d['Burger'] = 4.90\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del d['Burger']\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## If-Anweisungen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if 2>1 or 1>2:\n", " print ('Bedingung erfüllt.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = 10\n", "x = 5 if y > 10 else 4\n", "\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Schleifen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 5\n", "while x > 0:\n", " x -= 1\n", "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for x in ['a', 'b', 'c']:\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Funktionen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sum_upto(n):\n", " return n*(n+1)/2\n", "\n", "sum_upto(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zur besseren Übersicht (insbesondere bei vielen Parametern) können diese auch namentlich zugewiesen werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum_upto(n=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Funktionen können *Default-Parameter* haben, d.h. Werte, die von der Funktion als Standardwert verwendet werden (wenn kein anderer Wert übergeben wird). Default-Parameter müssen am Ende der Funktion stehen und übergeben werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def fun_with_default(x=3):\n", " print('Parameter is {}'.format(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fun_with_default()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fun_with_default(x=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Funktionen können wie Variablen referenziert werden (Pointer auf die Funktion) und zum Beispiel als Parameter übergeben werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def calc_and_print(calc_function, n):\n", " calc_result = calc_function(n)\n", " print(calc_result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calc_and_print(sum_upto, 4)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# <font color='red'>--------------- Praktisch für Python, aber nicht für unseren Kurs -----------------</font> \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lambda-Funktionen\n", "I.d.R. für Inline-Funktionen. Können zum Beispiel an Funktionen als Parameter übergeben oder als Variable deklariert werden. Lambdas enthalten ein einzelnes Statement, dessen Wert automatisch der Rückgabewert ist." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calc_square = lambda x: x**2\n", "calc_square(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calc_and_print(calc_square, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calc_and_print(lambda x: x**3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List Comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = [x**2 for x in range(10)]\n", "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xy = [(x, x**2) for x in range(10) if x%2 == 0]\n", "xy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Klassen\n", "Objekte einer Klasse bekommen bei Methodenaufrufen als ersten Parameter automatisch das *\"self\"*-Objekt übergeben (vgl. *this* in Java). Dieser Parameter muss also immer auch als erster Parameter einer Klassenmethode übergeben werden. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Vehicle():\n", " # Constructor\n", " def __init__(self, n_wheels=4, noise='beep'):\n", " self.n_wheels = n_wheels\n", " self.noise = noise\n", " \n", " def make_noise(self):\n", " print(self.noise)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "basicVehicle = Vehicle()\n", "basicVehicle.make_noise()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Properties sind immer von außen sichtbar und lassen sich verändern. Properties, die nicht zur Veränderung gedacht sind, werden nach Konvention durch zwei Unterstriche im Namen gekennzeichnet." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "basicVehicle.n_wheels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vererbung ist in Python möglich. In der Regel wird aber Duck-Typing verwendet. \n", "\n", "“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bike = Vehicle(n_wheels=2, noise='ring')\n", "bike.make_noise()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Bike(Vehicle):\n", " def __init__(self):\n", " self.n_wheels = 2\n", " self.noise = 'ring'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Bike().make_noise()" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }