notizen hinzugefügt

main
romanamo 2023-06-23 23:05:46 +02:00
parent affe4b7826
commit cfb3a35023
7 changed files with 1176 additions and 0 deletions

View File

@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Division by zero caused by 3, 0\n"
]
}
],
"source": [
"# Try ... except\n",
"\n",
"def div(x,y):\n",
" try:\n",
" return x / y\n",
" except ZeroDivisionError:\n",
" print(\"Division by zero caused by {0}, {1}\".format(x,y))\n",
"\n",
"div(3,2)\n",
"div(3,0)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Ausnahme selbst werfen\n",
"\n",
"def div(x,y):\n",
" if y == 0:\n",
" raise ZeroDivisionError(\"Wert für Division ungültig\")\n",
" return x / y"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,374 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
"# Berechnung Umfang eines Kreises\n",
"def umfang(r : float) -> float:\n",
" return 2 * r * math.pi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Berechnung der wurzel durch ausprobieren\n",
"\n",
"def wurzel(z):\n",
" for n in range(100):\n",
" if n ** 2 == z:\n",
" return n\n",
" return -1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Multi-Return\n",
"\n",
"def multi_return():\n",
" return (42, 23)\n",
"\n",
"a,b = multi_return(42,23)\n",
"\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"101\n"
]
}
],
"source": [
"# variable Anzahl an Argumenten\n",
"\n",
"# mögliche Implementierung eine max-Funktion\n",
"def max(*a):\n",
" result = a[0]\n",
" for i in a:\n",
" if result < i:\n",
" result = i\n",
" return result\n",
"\n",
"print(max(4,5,34,78,12,101))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n",
"20\n",
"20\n"
]
}
],
"source": [
"# named arguments\n",
"\n",
"def calc(zahl1, zahl2):\n",
" return 2 * zahl1 + 4 * zahl2\n",
"\n",
"print(calc(2,4))\n",
"\n",
"print(calc(zahl1=2, zahl2=4))\n",
"print(calc(zahl2=4, zahl1=2))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 4 16 256\n"
]
}
],
"source": [
"# default arguments\n",
"\n",
"def potenz(basis = 2, exponent = 1):\n",
" return basis ** exponent\n",
"\n",
"a = potenz()\n",
"b = potenz(4)\n",
"c = potenz(2,4)\n",
"\n",
"d = potenz(exponent=8)\n",
"\n",
"print(a,b,c,d)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"source": [
"# Funktionen sind Objekte\n",
"\n",
"def twice(f,x):\n",
" return f(f(x))\n",
"\n",
"def quadriere(x):\n",
" return x*x\n",
"\n",
"print(twice(quadriere, 2))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"# global\n",
"\n",
"x = 0\n",
"\n",
"def incr_x2():\n",
" global x\n",
" x = x + 1\n",
"\n",
"incr_x2()\n",
"\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"12\n"
]
}
],
"source": [
"# verschachtelte Funktionen\n",
"\n",
"def function1(x):\n",
" def function2(y):\n",
" print(y + 2)\n",
" return y + 2\n",
" return 3 * function2(x)\n",
"\n",
"a = function1(2)\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"5\n"
]
}
],
"source": [
"# Lambdas\n",
"\n",
"# klassische Funktionsdefinition\n",
"def add(a,b):\n",
" return a + b\n",
"\n",
"# Definition über Lambda\n",
"add2 = lambda a,b : a + b\n",
"\n",
"print(add(1,2))\n",
"print(add2(2,3))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 6, 8]\n"
]
}
],
"source": [
"# Listen und Lambda\n",
"\n",
"my_list = list(range(1,9))\n",
"gerade = list(filter(lambda e: e% 2 == 0, my_list))\n",
"\n",
"print(gerade)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['PULP FICTION', 'KILL BILL', 'RESERVOIR DOGS']\n"
]
}
],
"source": [
"# Listen und Lambda\n",
"\n",
"filme = [\"Pulp Fiction\", \"Kill Bill\", \"Reservoir Dogs\"]\n",
"filme_gross = list(map(lambda f: f.upper(), filme))\n",
"\n",
"print(filme_gross)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"import functools\n",
"\n",
"a = [1,2,3,4]\n",
"\n",
"r = functools.reduce(lambda sum, x: sum + x, a)\n",
"\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fak(n):\n",
" return n * fak(n - 1) if n > 1 else 1"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24\n"
]
}
],
"source": [
"def fak(n):\n",
" ergebnis = 1\n",
"\n",
" for i in range(2, n+1):\n",
" ergebnis *= i\n",
" \n",
" return ergebnis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fibonacci-Folge\n",
"\n",
"def fibonacci(n : int) -> int:\n",
" if n == 1 or n == 2:\n",
" return n\n",
" return fibonacci(n-1) + fibonacci(n-2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"# Anzahl Lösungen quadratischer Gleichung\n",
"\n",
"def amount_solutions(a: int, b: int, c: int):\n",
" discriminant = b**2 - 4 * a * c\n",
"\n",
" if discriminant < 0:\n",
" return 0\n",
" elif discriminant == 0:\n",
" return 1\n",
" else:\n",
" return 2\n",
"\n",
"print(amount_solutions(5, 6, 1))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"a = 5\n",
"b = 4\n",
"\n",
"max = 0\n",
"\n",
"# normales if\n",
"\n",
"if a > b:\n",
" max = a\n",
"else:\n",
" max = b\n",
"\n",
"#ternäres if\n",
"\n",
"max = a if a > b else b\n",
"\n",
"print(max)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"4\n",
"9\n",
"16\n"
]
}
],
"source": [
"# Ausgabe der ersten 4 Quadratzahlen\n",
"\n",
"i = 1\n",
"\n",
"while i < 5:\n",
" print(i ** 2)\n",
" i = i + 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# Summe von 1-5 mit while-loop\n",
"\n",
"value = 0\n",
"i = 0\n",
"while i <= 5:\n",
" value += i\n",
" i += 1\n",
"\n",
"print(value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# Summe von 1-5 mit while-loop\n",
"\n",
"value = 0\n",
"for i in range(0, 5+1):\n",
" value += i\n",
"\n",
"print(value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Index Value Bar\n",
" 0 19 *******************\n",
" 1 3 ***\n",
" 2 15 ***************\n",
" 3 7 *******\n",
" 4 11 ***********\n"
]
}
],
"source": [
"# Formatierung Werte in Tabelle\n",
"\n",
"values = [19, 3, 15, 7, 11]\n",
"\n",
"print(f\"{'Index':>6} {'Value':>6} {'Bar'}\")\n",
"for index, item in enumerate(values):\n",
" print(f\"{index:>6} {item:>6} {'*' * item}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,3 @@
1 2 3.5
4 5 6
7.0 8 9.3

View File

@ -0,0 +1,509 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n",
"[[1. 2. 3.]\n",
" [4. 5. 6.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Array mit Ganzzahlen anlegen\n",
"a = np.array([[1,2,3], [4,5,6]])\n",
"print(a)\n",
"\n",
"# Array mit Fließkommazahlen anlegen\n",
"b = np.array([[1,2,3], [4,5,6]], float)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0.]\n",
" [0. 0. 0.]]\n",
"(2, 3)\n",
"[[1 0 0]\n",
" [0 2 0]\n",
" [0 0 3]]\n"
]
}
],
"source": [
"# Matrix nur mit Nullen\n",
"\n",
"a = np.zeros((2,3))\n",
"\n",
"print(a)\n",
"print(a.shape)\n",
"\n",
"# Diagonal-Matrix\n",
"\n",
"b = np.diag([1,2,3])\n",
"\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.31437735 0.70638017 0.893382 ]\n",
" [0.88031899 0.61047408 0.93577239]]\n"
]
}
],
"source": [
"a = np.random.random((2,3))\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0.]\n",
" [0. 0.]]\n",
"[[8. 0.]\n",
" [0. 0.]]\n"
]
}
],
"source": [
"a = np.zeros((2,2))\n",
"\n",
"print(a)\n",
"\n",
"a[0][0] = 7\n",
"#oder a[0,0] = 7\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"(2, 3)\n",
"6\n",
"int64\n",
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n"
]
}
],
"source": [
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"print(a.ndim)\n",
"print(a.shape)\n",
"print(a.size)\n",
"print(a.dtype)\n",
"print(a.T)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 3 4 12]\n",
"[2 2 0 1]\n",
"[ 0 3 8 15]\n"
]
}
],
"source": [
"a = np.arange(4)\n",
"\n",
"b = np.array([2,3,2,4])\n",
"\n",
"print(b * a)\n",
"print(b - a)\n",
"\n",
"c = [2,3,4,5]\n",
"\n",
"print(a * c)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (3,) (4,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[13], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m a \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marange(\u001b[39m3\u001b[39m)\n\u001b[1;32m 2\u001b[0m b \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marange(\u001b[39m4\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m(a\u001b[39m+\u001b[39;49mb)\n",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (4,) "
]
}
],
"source": [
"a = np.arange(3)\n",
"b = np.arange(4)\n",
"\n",
"print(a+b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 4 5]\n"
]
}
],
"source": [
"# Aufgabe\n",
"\n",
"x = np.array([0,1,2])\n",
"y = np.array([3])\n",
"\n",
"print(x+y)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"6\n",
"[-1 2 -1]\n",
"[[1 1 1]\n",
" [2 2 2]\n",
" [3 3 3]]\n"
]
}
],
"source": [
"# Listen werden automatisch in Vektoren konvertiert\n",
"\n",
"u = [1,2,3]\n",
"v = [1,1,1]\n",
"\n",
"print(np.inner(u, v))\n",
"print(np.dot(u, v))\n",
"print(np.cross(u, v))\n",
"\n",
"print(np.outer(u,v))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6]\n",
"[[1 2 3]\n",
" [4 5 6]]\n",
"[[3]\n",
" [6]]\n"
]
}
],
"source": [
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"# 2. Zeile, alle Spalten\n",
"print(a[1, :])\n",
"\n",
"# 1. und 2. Zeile, alle Spalten\n",
"print(a[0:2])\n",
"\n",
"# Alle Zeilen, Spalten 3 und 4\n",
"print(a[:, 2:4])\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"[4 5 6]\n",
"[1 2 3]\n",
"[4 5 6]\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"# Iterieren über Matrizen\n",
"\n",
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"# Zeilen und Spaltenweise iterieren\n",
"for row in a:\n",
" for column in a:\n",
" print(column)\n",
"\n",
"# Über alle Elemente iterieren\n",
"for element in a.flat:\n",
" print(element)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 2. 3.5]\n",
" [4. 5. 6. ]\n",
" [7. 8. 9.3]]\n"
]
}
],
"source": [
"# Matrix aus Datei lesen\n",
"\n",
"m = np.loadtxt(\"matrix.txt\")\n",
"\n",
"print(m)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 1.]\n",
" [1. 1.]\n",
" [1. 1.]]\n",
"[[1. 1. 1.]\n",
" [1. 1. 1.]]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.ones((3,2))\n",
"\n",
"b = a.T # Transponierte Matrix\n",
"\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 7 9]\n",
"[ 6 15]\n",
"21\n",
"[[1 2 3]\n",
" [5 7 9]]\n",
"[[ 1 3 6]\n",
" [ 4 9 15]]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"\n",
"# Spalten und Zeilensumme\n",
"print(a.sum(axis=0))\n",
"print(a.sum(axis=1))\n",
"\n",
"print(a.sum())\n",
"\n",
"# Kumulative Summe\n",
"print(a.cumsum(axis=0))\n",
"print(a.cumsum(axis=1))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"6\n",
"[1 4]\n",
"[3 6]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"\n",
"#Maximum und Minimum\n",
"print(a.min())\n",
"print(a.max())\n",
"\n",
"#Maximum und Minimum entlang einer Achse\n",
"print(a.min(axis=1))\n",
"print(a.max(axis=1))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 0. 0.]\n",
" [0. 1. 0.]\n",
" [0. 0. 1.]]\n",
"14\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"# 3x3 Identitätsmatrix\n",
"id = np.eye(3)\n",
"\n",
"print(id)\n",
"\n",
"# Spur berechnen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6],[7,6,8]])\n",
"print(a.trace())"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n",
"[[1 2 3]\n",
" [4 5 6]]\n"
]
}
],
"source": [
"# Matrix Operationen\n",
"\n",
"a = np.array([1,2,3])\n",
"b = np.array([4,5,6])\n",
"\n",
"print(np.column_stack([a,b]))\n",
"\n",
"print(np.row_stack((a,b)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,34 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Unit-Test\n",
"\n",
"import unittest\n",
"\n",
"def is_palindrome(s : str):\n",
" return s == s[::-1]\n",
"\n",
"class KnownInput(unittest.TestCase):\n",
" knownValues = (('lego', False), ('radar', True))\n",
"\n",
" def testKnownValues(self):\n",
" for word, palin in self.knownValues:\n",
" result = is_palindrome(word)\n",
" self.assertEqual(result, palin)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}