mirror of
https://gitlab.rlp.net/pgp/pgp1-python-einfuehrung
synced 2024-11-16 13:48:11 +00:00
Split notebooks into individual lessons
This commit is contained in:
parent
7e3ea138cd
commit
17938cf303
5 changed files with 1178 additions and 449 deletions
|
@ -1147,7 +1147,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.6"
|
||||
"version": "3.9.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
File diff suppressed because it is too large
Load diff
568
Lektion2_Messtabellen.ipynb
Normal file
568
Lektion2_Messtabellen.ipynb
Normal file
|
@ -0,0 +1,568 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "21a11be0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Lektion 2. Messtabellen:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Messtabellen in Python:\n",
|
||||
"\n",
|
||||
"Damit uns eine Programmiersprache wie Python Arbeit abnehmen kann, sollte es auch möglich sein, größere Datenmengen, wie z.B. die Werte einer Messtabelle, in einer Variablen zu speichern. Python bietet hierfür verschiedene Konzepte an. Jedes dieser Konzepte hat unterschiedliche Stärken und Schwächen. Die gängigsten Methoden sind list, tuple, bzw. sogenannte numpy.arrays und pandas.dataframes. Aufgrund der limitierten Zeit im PGP 1 werden wir uns hier lediglich mit zwei dieser vier Methoden auseinandersetzen. \n",
|
||||
"\n",
|
||||
"Fangen wir zunächst mit Listen an. Eine Liste ist eine Ansammlung von Werten, welche alle den gleichen oder ganz unterschiedliche Datentypen haben können. Eine Liste kann auf zwei unterschiedliche Art und Weisen erstellt werden:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "ec6c1c02",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1', 'Wert2', 'Wert3']"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1 = ['Wert1', 'Wert2', 'Wert3'] # Variante 1\n",
|
||||
"Messwerte1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "21773a54",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[2, 0.9, '1']"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte2 = list([2, 0.9, '1']) # Variante 2\n",
|
||||
"Messwerte2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "be844385",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Sobald wir eine liste erstellt haben, können wir eine ganze Reihe von unterschiedlichen Manipulationen durchführen, um sie nach unserem Belieben zu verändern.\n",
|
||||
"\n",
|
||||
"Wir können zum Beispiel die bestehende Liste um einen Wert erweitern (`append`) oder einen zusätzlichen Wert an eine beliebige Stelle in der Liste hinzufügen (`insert`)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "b8e22f05",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1', 'Wert2', 'Wert3', 'Wert5']"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1.append('Wert5')\n",
|
||||
"Messwerte1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "9bdd72bf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1', 'Wert2', 'Wert3', 'Wert5', 'Wert4']"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1.insert(4, 'Wert4')\n",
|
||||
"Messwerte1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8f7316a2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Ups, was ist denn in der letzten Zelle passiert? Wert4 wurde ja garnicht an Stelle 4 der Liste gesetzt, Python scheint nicht zählen zu können... \n",
|
||||
"\n",
|
||||
"Leider zählt Python doch richtig. In Python läuft der Index von Objekten in einer Liste immer von 0,1,2,3...n. Dies können wir auch ganz einfach überprüfen, indem wir unsere Liste in verschiedene \"Scheiben\" schneiden (so genanntes slicing). Dies geht wie folgt:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "83e9b6f0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"NeueWerte = ['Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6'] "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "173ccfee",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Die kleinste Scheibe, welche wir abschneiden können, ist ein einzelner Wert:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "0b6c1f61",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Wert1'"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"NeueWerte[0] # Hier sehen Sie, dass der erste Wert den Index 0 hat."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "c851e5b0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Wert3'"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"wert_index_2 = NeueWerte[2] \n",
|
||||
"wert_index_2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7b9e07f7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Wie bei einer Pizza können wir uns natürlich auch größere Stücke nehmen."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "a35f8d4a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1', 'Wert2', 'Wert3']"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"NeueWerte[0:3]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "9e63410f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert3', 'Wert4', 'Wert5']"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"NeueWerte[2:5] # Python behandelt den letzten Wert wie in einem offenen Intervall [2,5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "27ae7119",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert3', 'Wert4', 'Wert5', 'Wert6']"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"NeueWerte[2:] # Hier werden alle Werte mit dem Index >= 2 zurückgegeben"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "bfb5a049",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert4', 'Wert5', 'Wert6']"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"NeueWerte[-3:] # Mit negativen Zahlen fangen Sie vom Ende der Liste an"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0735b414",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Neben `insert`, `append` und `slicing` bietet Python noch ein paar weitere Listenmanipulationen an. Mit Hilfe des `+` Operators können Sie die Werte in einer Liste direkt an eine andere Liste anfügen."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "38098cd2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1',\n",
|
||||
" 'Wert2',\n",
|
||||
" 'Wert3',\n",
|
||||
" 'Wert5',\n",
|
||||
" 'Wert4',\n",
|
||||
" 'Wert1',\n",
|
||||
" 'Wert2',\n",
|
||||
" 'Wert3',\n",
|
||||
" 'Wert4',\n",
|
||||
" 'Wert5',\n",
|
||||
" 'Wert6']"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1 + NeueWerte"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "88ce1ac5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Anders als `append`, welches die zweite Liste als Ganzes an die erste Liste anfügt:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "e0d648ac",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1',\n",
|
||||
" 'Wert2',\n",
|
||||
" 'Wert3',\n",
|
||||
" 'Wert5',\n",
|
||||
" 'Wert4',\n",
|
||||
" ['Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6']]"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1.append(NeueWerte)\n",
|
||||
"Messwerte1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "24c36d81",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Aber aufgepasst, bei `append` wird die Liste, an welche Sie die Daten anhängen (hier Messwerte1), direkt geändert (dies gilt auch für `insert`), während Sie beim `+` Operator die Variable überschreiben müssen, damit die Änderung wirksam wird. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "7763f952",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Wert1',\n",
|
||||
" 'Wert2',\n",
|
||||
" 'Wert3',\n",
|
||||
" 'Wert5',\n",
|
||||
" 'Wert4',\n",
|
||||
" ['Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6'],\n",
|
||||
" 'Wert1',\n",
|
||||
" 'Wert2',\n",
|
||||
" 'Wert3',\n",
|
||||
" 'Wert4',\n",
|
||||
" 'Wert5',\n",
|
||||
" 'Wert6']"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Messwerte1 = Messwerte1 + NeueWerte\n",
|
||||
"# Tipp: Dies können Sie auch einfach mithilfe von\n",
|
||||
"# Messwerte1 += NeueWerte\n",
|
||||
"Messwerte1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c9e1ed86",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Zwei weitere nützliche Befehle im Zusammenhang von Listen ist die `len`- und `range`-Funktion. \n",
|
||||
"\n",
|
||||
"`len` gibt die Länge einer Liste zurück "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "42f10cd5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['Wert1', 'Wert2', 'Wert3', 'Wert5', 'Wert4', ['Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6'], 'Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6']\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(Messwerte1)\n",
|
||||
"len(Messwerte1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6858a55f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"`range` erstellt ganzzahlige Werte zwischen zwei ganzen Zahlen"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "49eaf0a3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"range(0, 5, 2)"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"range(0, # <-- Startwert\n",
|
||||
" 5, # <-- Endwert (nicht mehr enthalten, offenes Ende)\n",
|
||||
" 2 # <-- Schrittweite\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fe1650e7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Sie können die `range` Rückgabe auch wieder in eine Liste umwandeln"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "16670845",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0, 2, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(range(0,5,2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7cd394f4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=task>\n",
|
||||
" \n",
|
||||
"#### Aufgabe 4.a.: Erstellen von Messwerttabellen:\n",
|
||||
"\n",
|
||||
"Erstellen Sie für jede Spalte der nachfolgenden Messtabelle eine Liste, welche die Messdaten beinhaltet. Benutzen Sie anschließend den `append` Befehl, um die Daten jeder Spalte an eine weitere Liste namens *daten* anzuhängen. \n",
|
||||
"\n",
|
||||
"| Messwertnummer | Spannung [V] | Strom [mA] | Fehler der Spannung [V] | Fehler des Stroms [mA] |\n",
|
||||
"|----------------|--------------|------------|-------------------------|---------------------------|\n",
|
||||
"| 1 | 12.00 | 110 | 0.32 | 10 |\n",
|
||||
"| 2 | 11.78 | 98 | 0.15 | 10 |\n",
|
||||
"| 3 | 12.56 | 102 | 0.63 | 10 |\n",
|
||||
"| 4 | 12.34 | 124 | 0.12 | 10 |\n",
|
||||
"| 5 | 12.01 | 105 | 0.20 | 10 |\n",
|
||||
"| 6 | 11.94 | 95 | 0.17 | 10 |\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Verwenden Sie anschließend das Slicing, um die umgesetzte Leistung im Widerstand für die Meswerte 3 und 5 zu berechnen.\n",
|
||||
"\n",
|
||||
"**Tipp:**\n",
|
||||
"\n",
|
||||
"1. Sie haben bereits die Funktionen für die Leistung in Aufgabe 3 definiert und können sie hier erneut verwenden. \n",
|
||||
"2. Das Slicen von verschachtelten Listen funktioniert genauso wie bei normalen Listen:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"spalte0 = daten[0] #<-- Wählt die Spalte 0 an \n",
|
||||
"spalte0[2] #<-- Wählt aus Spalte 0 den Messwert mit Index 2 an\n",
|
||||
"# oder als Einzeiler:\n",
|
||||
"daten[0][2] \n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"3. Geben Sie an, wie sich die Messwertnummer zum Listenindex verhält.\n",
|
||||
"<div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d85c9d1c",
|
||||
"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.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
BIN
images/A4_syntax_error.png
Executable file
BIN
images/A4_syntax_error.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 1.2 MiB |
BIN
images/Fehlermeldung_div0.png
Executable file
BIN
images/Fehlermeldung_div0.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 453 KiB |
Loading…
Reference in a new issue