375 lines
6.6 KiB
Plaintext
375 lines
6.6 KiB
Plaintext
{
|
|
"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
|
|
}
|