{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python: erste Schritte\n",
"Dies ist ein interaktives Notebook um den Einstig in das Programmieren mit Python zu begleiten.\n",
"\n",
"Jede Zelle kann interaktiv ausgeführt werden, in dem entweder das ▶ - Symbol in der oberen Leiste mit der Maus gewählt wird, oder die Tasten-Kombination <kbd>⇧ Shift</kbd><kbd>Enter</kbd> in der Zeile ausgeführt werden.\n",
"\n",
"Probieren Sie es aus: Setzten Sie den Cursor in Zelle [2] mit dem Inhalt `1 + 2` und drücken Sie <kbd>⇧ Shift</kbd><kbd>Enter</kbd>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"6 * 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Das Ergenis sollte in der Zeile darunter erscheinen. Ein Notebook funktioniert analog zu einer Kommandozeile in der ein interaktiver Python Interpreter, die sog. [REPL-Umgebung](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) läuft. \n",
"\n",
"Oft werden hier einzelne Ausgaben von Text, Variablen-Werten wie Ergebnissen ausgegeben. Dazu wird die `print` Funktion ausgeführt, die fest in Python eingebaut ist: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Hallo Python\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In diesem Falle wurde die Zeichenkette `Hello Python` als sog. Parameter and die Funktion übergeben. Zeichenketten werden mit `\"` begonnen und beendet."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variablen und Datentypen\n",
"\n",
"Statt ein Ergebnis einfach nur zu berechnen und in der Konsole oder dem Notebook auszugeben, können wir den Wert auch einer Variblen zuweisen. Der Python-Interpreter sucht dabei automatisch den passenden Datenytpen aus:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis = 1 + 5\n",
"Ergebnis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablenamen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Es können beliebig viele Variablen in ein Skript eingefügt werden. Variablen müssen eindeutige Namen haben"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Datentypen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jede Variable in Python hat einen Datentypen. Streng genommen ist jeder Datentyp in Python selber ein Objekt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis = 3 * 5\n",
"type(Ergebnis)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jedes mal wenn ein neuer Wert einer vorhandenen Variablen zugewiesen wird, kann sich der Datentyp ändern. Hier wird `Ergebnis` zu einer Gleitkommazahl vom Typ `float`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis = 3.5 / 3\n",
"Ergebnis\n",
"type(Ergebnis)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Datentypen können explizit angegeben werden:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = float(1) \n",
"f"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ganzzahl_pi = int(\"33\")\n",
"ganzzahl_pi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"type(ganzzahl_pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 3\n",
"b = a\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 5\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Textausgabe und Zeichenketten\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variablen können mit `print()` ausgegeben werden:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Ergebnis = 3 * 5\n",
"print(Ergebnis)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variablen Werte, die keine Strings sind können durch Formatierungsstrings ausgegeben werden.\n",
"Verschiedenen Ansätze habe sich im Lauf der Zeit entwickelt.\n",
"\n",
"Der traditionelle Weg is über das `%` Zeichen. Dieser findet sich noch häufig in älterem Quellcode, ist aber nicht mehr empfohlen: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print (\"Das Ergebnis lautet: %s!\" % Ergebnis)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Der moderne Weg ab Python 3 ist über sogenannte Formatstring `F-Strings`\n",
"Dabei wird dem String ein `f` vorangestellt. Innerhalb des String können dann Variablen in `{` und `}` eingefügt werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"Das Ergebnis ist {Ergebnis}. Immer noch\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hier können auch kleiner Operationen ausgeführt oder Funktionen aufgerufen werden:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print (f\"Spontane Berechnung: {4*5}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"zahl = 1 / 3\n",
"print (f\"Zahl ohne Begrenzung:\\t {zahl}\")\n",
"print (f\"Zahl mit Begrenzung:\\t {zahl:.4f}\")\n",
"print (f\"Zentriert: \\t\\t {zahl:^20.3f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sequentielle Datentypen\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kollektion (Container) mit Elementen in geordneter Folge\n",
"- Tupel\n",
" - unveränderbar (immutable)\n",
" - schnell\n",
" - begrenzt mit '(' und ')'\n",
"- Listen\n",
" - veränderbar (mutable)(können nach Erzeugung verändert werden)\n",
" - begrentzt mit '[' und ']'\n",
" - umfrangreiche Manipulation möglich\n",
"\n",
"Beide können Werte unterschiedlicher Datentypen haben/mischen\n",
"\n",
"Strings sind unveränderbare Sequenzen\n",
"- wie Tupel\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tupel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mein_tupel = (44, 2, 5, 7, 8)\n",
"another_tuple = (1,3,\"string\", 4, (4,4), [1,2,3,4], {})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste = [1, 2, 5, 8, 9]\n",
"andere_liste = [1,3, \"string\", 4, (4,4), [1,2,3,4], {}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"andere_liste"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Range\n",
"mit `range(start, ende, [erhöhung])` können Sequenzen von Ganzahlen erzeugt werden. Der Aufruf der Funktion `list` verwandelt diese in eine Liste:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste = range(1,10,1)\n",
"list(liste)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste = range(1,10,2)\n",
"list(liste)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Für Gleitkommawerte steht die `arange` des mächtigen Moduls NumPy zur Verfügung. Es ist jedoch nicht Teil der Standarbibliothek und muss meist erst installiert werden"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from numpy import arange\n",
"arange (1,10,0.2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Zugriff auf Elemente"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zugriff mit [index]\n",
"- Erstes Element 0\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mein_tupel[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slicing ('Scheiben rausschneiden'):\n",
"- Zwischen Grenzen : [untere:obere]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste = [1,3, \"string\", 4, (4,4), [1,2,3,4], {}]\n",
"liste[1:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"bis Obergrenze : [:obergrenze]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Beginnend von Untergrenze bis Ende der Sequenz: [untergrenze:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste[2:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Unveränderbarkeit (immutability)\n",
"Werte in Tupeln können nicht verändert werden"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tupel = (\"one\", \"zwei\", \"trois\", \"quattro\", \"πέντε\")\n",
"liste = [\"one\", \"zwei\", \"trois\", \"quattro\", \"πένaτε\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tupel[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste[1] = \"two\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tupel[1] = \"two\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### + Plus Operator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste1 = [1, 2, 3]\n",
"liste2 = [\"vier\", \"fünf\", \"sechs\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste1 + liste2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vorname = \"Erika\"\n",
"nachname = \"Musterfrau\"\n",
"vorname + nachname"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vorname + ' ' + nachname"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listenoperationen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Methoden sind Funktionen die von von einer Objektinstanz aufgerufen weren können\n",
"\n",
"Verfügbare Methoden können durch `dir(objekt)` oder `help(objekt)` aufgerufen werden\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`append(wert)` für das Anhängen an eine Liste"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste = [\"one\", \"zwei\", \"trois\", \"quattro\", \"πένaτε\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.append(\"sechs\")\n",
"liste"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`reverse()` zum Umkehren der Reihenfolge"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.reverse()\n",
"liste"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`sort()` zur Sortierung (numerische, alphabetische Reihenfolge etc.). Kann mit eignen Sortierfunktionen erweiterter werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.sort()\n",
"liste"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`count()` Anzahl der Elemente eines Wertes\n",
"\n",
"`len` Länge der Liste\n",
"\n",
"`index(wert)` Erstes Vorkommen des Elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.count(\"sechs\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.append(\"sechs\")\n",
"liste.append(\"sechs\")\n",
"liste.count(\"sechs\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(liste)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"liste.index(\"sechs\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"help(list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Übungsaufgabe Listen\n",
"- Erzeuge die Liste eine Wandaufbaus einer mehrschaligen Wand\n",
"- schlage die funktionen `pop` und `remove` nach und manipuliere die Liste des Wandaufbaus\n",
" - benutze die `help(liste)` Funktion oder schaue in der Dokumeknttion nach https://docs.python.org/3/tutorial/datastructures.html#more-on-lists \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries (Schlüssel-Wert-Tabellen / Mappingtypen)\n",
"begrenztdurch `{ }`\n",
"\n",
"Eine Tabelle von Schlüsseln und Werten in der Form {schluessel : wert}\n",
"\n",
"- Ein Schlüssel kann jeder unveränderbare Datentyp sein \n",
"- Werte können jeglichen Datentypen haben\n",
"- einschliesslich Listen, dictionaries in beliebiger Verschachtelung\n",
"- nützlich zum \n",
" - speichern\n",
" - indizieren\n",
" - nachschlagen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende = {\"name\":\"Erika\", \"familyname\" : \"Musterfrau\", \"matrikel\":12345}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zugriff auf Werte mit [schluessel]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende[\"name\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende[\"matrikel\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alle Schlüssel durch Aufruf der Funktion `keys()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`values()` für alle Werte"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.values()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`items()` für eine Liste von Schlüsseln und Werten "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.items()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"paare = studierende.items()\n",
"paare"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"verwende `.get(key, defaultvalue)` wenn nicht klar ist, ob ein/der Wert existiert"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.get(\"name\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.get(\"notenschnitt\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"studierende.get(\"notenschnitt\", 1.7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Übungsaufgabe Dictionary: \n",
"- erstelle das Dictionary einer selbstmodellierten, einfachen Klasse aus der UML Aufgabe\n",
"- erzeuge drei Einträge eines Dictionaries als Teil einer Liste"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = \"some new value\""
]
},
{
"cell_type": "code",
"execution_count": null,
"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"
},
"toc-autonumbering": true,
"toc-showmarkdowntxt": false
},
"nbformat": 4,
"nbformat_minor": 4
}