Skip to content
Snippets Groups Projects
tutorial00_python_ex2.ipynb 21 KiB
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": null,
   "metadata": {},
   "outputs": [],
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "source": [
    "a, b = 1, 2\n",
    "a, b = b, a\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Operatoren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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": null,
   "metadata": {
    "scrolled": true
   },
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(l)\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[:])\n",
    "\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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
    "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",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "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",
   "execution_count": null,
   "metadata": {},
    "    print(\"Hello world\")\n",
    "\n",
    "greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
    "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
    "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "for el in l2:\n",
    "    print(el)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "for i in range(len(l2)):\n",
    "    print(i, l2[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "for i, el in enumerate(l2):\n",
    "    print(i, el)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "for key, val in c.items():\n",
    "    print(key, val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Objektorientierte Programmierung"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
   "execution_count": null,
   "metadata": {},
   "source": [
    "p = Person(\"Anna\")\n",
    "p.greet()\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "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",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "lsq = map(lambda x: x**2, l)\n",
    "print(lsq)\n",
    "print(list(lsq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(list(filter(lambda x: x % 2 == 1, l)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "from functools import reduce\n",
    "print(reduce(lambda a, b: a + b, l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(reduce(lambda a, b: a if a > b else b, l))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## == vs. is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(5 == 5)\n",
    "print(5 is 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
    "a = b = [1,2,3]\n",
    "print([1,2,3] == [1,2,3])\n",
    "print([1,2,3] is [1,2,3])\n",
    "print(a is b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy Grundlagen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "a = np.array([1, 2, 3, 4, 5, 6])\n",
    "print(a)\n",
    "print(a[0])\n",
    "print(a[-1])\n",
    "print(a[1:4])\n",
    "print(a[:2])\n",
    "print(a[::-1])\n",
    "print(a[1::2])\n",
    "print(a[[0, 2, 3, 4]]) # Liste von Indizes\n",
    "print(a[[True, False, True, False, False, True]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Operatoren auf NumPy Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
    "print(a + 2)\n",
    "print(3 * a)\n",
    "print(a**2)\n",
    "print(2**a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([6, 5, 4, 3, 2, 1]) # b = a[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(a + b)\n",
    "print(a * b)\n",
    "print(a**b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Zweidimensionale Arrays (Matrizen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(A)\n",
    "print(A[0,0], A[1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[0])\n",
    "print(A[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[:2, :2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[:2, 1::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[1::-1, :2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Werte überschreiben"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "B = A.copy()\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "B[0] = 12\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "B[:, 1] = -5\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "B[2] = [5, 6, 7]\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "B[:, 1] = B[0]\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dimensionen hinzufügen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[None])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[:, None])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[:, :, None])"
   ]
  },
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[1,0],[0,1]])\n",
    "Y = np.array([[1,1],[2,1],[3,4]])\n",
    "print(X[:, None] - Y[None, :])\n",
    "print(X[None, :] - Y[:, None])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Boolesche Funktionen auf NumPy Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([1, 0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(b == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(b < 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(A[b == 1])"
   ]
  },