Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Einführung in Python und NumPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variablen und Datentypen in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Primitive Datentypen"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 Hello World 3.14 True (4+3j)\n",
"<class 'int'> <class 'str'> <class 'float'> <class 'bool'> <class 'complex'>\n"
]
}
],
"source": [
"a = 5\n",
"b = \"Hello World\"\n",
"c = 3.14\n",
"d = True\n",
"e = 4 + 3j\n",
"print(a, b, c, d, e)\n",
"print(type(a), type(b), type(c), type(d), type(e))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen überschreiben"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50\n",
"Hello World\n"
]
}
],
"source": [
"var = 50\n",
"print(var)\n",
"var = \"Hello World\" # Neuer Datentyp\n",
"print(var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrere Variablen initialisieren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 6 7\n",
"ha ha ha\n"
]
}
],
"source": [
"x, y, z = 5, 6, 7\n",
"i = j = k = \"ha\"\n",
"print(x, y, z)\n",
"print(i, j, k)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen tauschen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"a, b = 1, 2\n",
"a, b = b, a\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operatoren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"-2\n",
"8\n",
"0.5\n",
"0\n",
"2\n",
"16\n",
"2.0\n"
]
}
],
"source": [
"a, b = 2, 4\n",
"print(a + b)\n",
"print(a - b)\n",
"print(a * b)\n",
"print(a / b)\n",
"print(a // b)\n",
"print(a % b)\n",
"print(a**b)\n",
"print(b**(1/a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen mit Operatoren neu zuweisen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12 35 16807\n"
]
}
],
"source": [
"i = j = k = 7\n",
"i += 5\n",
"j *= 5\n",
"k **= 5\n",
"print(i, j, k)\n",
"# Kein i++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boolesche Operatoren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"False\n",
"True\n",
"True\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"print(1 + 1 == 2)\n",
"print(1 + 2 != 2)\n",
"print(1 + 2 > 3)\n",
"print(1 + 2 <= 3)\n",
"print(1 + 2 is 3)\n",
"print(True or False) # ||\n",
"print(True and False) # &&\n",
"print(not True) # !"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python numpy scipy 10 True\n",
"True 10 scipy numpy python\n",
"5\n"
]
}
],
"source": [
"l = [\"python\", \"numpy\", \"scipy\", 10, True]\n",
"print(l[0], l[1], l[2], l[3], l[4])\n",
"print(l[-1], l[-2], l[-3], l[-4], l[-5])\n",
"print(len(l))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listen bearbeiten"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['python', 'numpy', 'scipy', 10, True, False]\n",
"['python', 'numpy', 'scipy', 'matplotlib', True, False]\n"
]
}
],
"source": [
"l.append(False)\n",
"print(l)\n",
"l[3] = \"matplotlib\"\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operatoren auf Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]\n",
"True\n",
"False\n"
]
}
],
"source": [
"l1, l2 = [1,2,3,4], [5,6,7,8]\n",
"l = l1 + l2\n",
"print(l)\n",
"print(3 * l1)\n",
"print(5 in l2)\n",
"print(5 in l1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrdimensionale Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"1 4 6\n",
"[1, 2, 3] [4, 5, 6] [7, 8, 9]\n"
]
}
],
"source": [
"A = [[1,2,3], [4,5,6], [7,8,9]]\n",
"print(A[0][0], A[1][0], A[1][2])\n",
"print(A[0], A[1], A[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List Slices\n",
"Notation: liste[start: stop: step]"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[3, 4, 5]\n",
"[3, 4, 5, 6, 7, 8]\n",
"[1, 2, 3, 4, 5]\n",
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[3, 5]\n",
"[6, 5, 4]\n",
"[8, 7, 6, 5, 4, 3, 2, 1]\n"
]
}
],
"print(l[2:5]) # der Index von stop ist nicht mit dabei\n",
"print(l[2:]) # stop weglassen => bis zum Ende\n",
"print(l[:5]) # start weglassen => vom Anfang an\n",
"print(l[2:5:2]) # Das erste Element (start) ist immer mit dabei\n",
"print(l[5:2:-1])\n",
"print(l[::-1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tupel"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Audi 250 4.7 True\n",
"True 4.7 250 Audi\n",
"4\n",
"True\n"
]
}
],
"source": [
"t = (\"Audi\", 250, 4.7, True)\n",
"print(t[0], t[1], t[2], t[3])\n",
"print(t[-1], t[-2], t[-3], t[-4])\n",
"print(len(t))\n",
"print(\"Audi\" in t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tupel können nicht verändert werden, nur zusammengefügt!"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Audi', 250, 4.7, True, 'white')\n",
"4.7 white\n"
]
}
],
"t += (\"white\",) # es wird ein neues Tupel erstellt\n",
"print(t)\n",
"a, b, c, d, e = t\n",
"print(c, e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Audi\n",
"dict_keys(['brand', 'ps', 'acc', 'color'])\n",
"dict_values(['Audi', 250, 4.7, 'white'])\n",
"dict_items([('brand', 'Audi'), ('ps', 250), ('acc', 4.7), ('color', 'white')])\n"
]
}
],
"source": [
"c = {\n",
" \"brand\": \"Audi\",\n",
" \"ps\": 250,\n",
" \"acc\": 4.7\n",
"}\n",
"\n",
"c[\"color\"] = \"white\"\n",
"\n",
"print(c[\"brand\"])\n",
"print(c.keys())\n",
"print(c.values())\n",
"print(c.items()) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktionen und Verzweigungen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
" print(\"Hello world\")\n",
"\n",
"greet()"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"def square(x: int) -> int:\n",
" return x**2\n",
"\n",
"print(square(4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Optionale Parameter und Parameter mit Namen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"100\n",
"25\n"
]
}
],
"source": [
"def pow(x, y = 0):\n",
" return x**y\n",
"\n",
"print(pow(10))\n",
"print(pow(10, 2))\n",
"print(pow(y=2, x=5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrere Rückgabewerte (Tupel)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 16\n",
"(9, 16)\n"
]
}
],
"source": [
"def f(x, y):\n",
" return x**2, y**2\n",
"\n",
"a, b = f(3, 4)\n",
"print(a, b)\n",
"ab = f(3, 4)\n",
"print(ab)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bedingte Anweisungen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 -1 0\n"
]
}
],
"source": [
"def sign(x):\n",
" if x < 0:\n",
" return -1\n",
" elif x > 0:\n",
" return 1\n",
" else:\n",
" return 0\n",
" \n",
"print(sign(17), sign(-3), sign(0))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True False\n"
]
}
],
"source": [
"def positive(x):\n",
" return True if x > 0 else False\n",
"\n",
"print(positive(14), positive(0))\n",
"\n",
"# C/Java return x > 0 ? True : False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Schleifen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[1, 2, 3, 4, 5]\n",
"[1, 3, 5, 7, 9]\n"
]
}
],
"for i in range(5): # Python For-Schleifen iterieren immer über ein iterable z.B. Liste\n",
" print(i)\n",
" \n",
"print(list(range(1, 6)))\n",
"print(list(range(1, 10, 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Funktionsweise: range(start, stop, step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List Comprehensions"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 4, 9, 16]\n",
"[0, 4, 16, 36, 64]\n"
]
}
],
"source": [
"a = [i**2 for i in range(5)]\n",
"print(a)\n",
"\n",
"b = [i**2 for i in range(10) if i % 2 == 0]\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Über Datenstrukturen iterieren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 6, 7, 8]\n"
]
}
],
"source": [
"print(l2)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"6\n",
"7\n",
"8\n"
]
}
],
"source": [
"for el in l2:\n",
" print(el)"
]
},
{
"cell_type": "code",
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 5\n",
"1 6\n",
"2 7\n",
"3 8\n"
]
}
],
"source": [
"for i in range(len(l2)):\n",
" print(i, l2[i])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 5\n",
"1 6\n",
"2 7\n",
"3 8\n"
]
}
],
"source": [
"for i, el in enumerate(l2):\n",
" print(i, el)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'brand': 'Audi', 'ps': 250, 'acc': 4.7, 'color': 'white'}\n"
]
}
],
"source": [
"print(c)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"brand Audi\n",
"ps 250\n",
"acc 4.7\n",
"color white\n"
]
}
],
"source": [
"for key, val in c.items():\n",
" print(key, val)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Objektorientierte Programmierung"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"class Person:\n",
" def __init__(self, name):\n",
" self.name = name\n",
" \n",
" def greet(self):\n",
" print(\"Hallo, ich bin {}\".format(self.name))\n",
" \n",
" def __repr__(self):\n",
" return \"Person: {}\".format(self.name)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo, ich bin Anna\n",
"Person: Anna\n"
]
}
],
"source": [
"p = Person(\"Anna\")\n",
"p.greet()\n",
"print(p)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"class Student(Person):\n",
" def __init__(self, name, age, height):\n",
" Person.__init__(self, name)\n",
" self.__age = age # private\n",
" self._height = height # protected\n",
" \n",
" def __repr__(self):\n",
" return \"Person[name={},age={},height={}]\"\\\n",
" .format(self.name, self.__age, self._height)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo, ich bin Anna\n",
"Person[name=Anna,age=18,height=175]\n"
]
}
],
"source": [
"s = Student(\"Anna\", 18, 175)\n",
"s.greet()\n",
"print(s)\n",
"# print(s.__age)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktionale Programmierung"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"81\n",
"16\n"
]
}
],
"source": [
"g = lambda x: x**4\n",
"h = lambda x, y: x**(2*y)\n",
"print(g(3))\n",
"print(h(2,2))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n"
]
}
],
"source": [
"print(l)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<map object at 0x0000029981C05508>\n",
"[1, 4, 9, 16, 25, 36, 49, 64]\n"
]
}
],
"source": [
"lsq = map(lambda x: x**2, l)\n",
"print(lsq)\n",
"print(list(lsq))"
]
},
{