{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8bcfdc75-d1e0-4c58-b753-06af7cb06e08",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Basics & Zahlentypen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d78a33e-0b2f-4918-83a7-b3d13b06e109",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie den Text `Hallo Python` aus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa04993d-3b1d-4513-b929-7002f8d97f1f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hallo Python\n"
     ]
    }
   ],
   "source": [
    "print(\"Hallo Python\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "567d27f7-4f7c-4b76-badd-544846554578",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Definieren Sie zunächst die zwei Variablen `a` und `b` und initialisieren diese mit einem Integerwert ungleich `0`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0711fa35-6ccf-4493-84c9-d06f2b9f17ca",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = -2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09d5ea58-a00f-42c9-944b-66894ff9eb86",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Definieren Sie zwei Variablen `s` und `t` und initialisieren diese mit einem Floatwert ungleich `0`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ded76770-8698-418d-b0a8-edb4be3c9777",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "s = 1.5\n",
    "t = -2.7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d891404e-1729-4300-ab7f-375d1a6c4c68",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe:** Addieren Sie die Werte der Variablen `a` und `b` und speichern Sie das Ergebnis in der Variable `c`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "13be6643-b464-4dc9-a3c7-a69897eafa05",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a + b\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25f751a9-5cc9-4816-ad61-33c2165e7aa4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Nutzen Sie die Variablen `a` & `b` und Speichern Sie die Ergebnisse für die Multiplikation, Division, Ganzzahldivision, Exponentiation und den Modulo-Operator in den unten angegebenen Variablen:\n",
    "\n",
    "\\begin{align}\n",
    "m &= a\\cdot b\\\\\n",
    "d &= \\frac{a}{b}\\\\\n",
    "i &= \\lfloor\\frac{a}{b}\\rfloor\\\\\n",
    "e &= a^b\\\\\n",
    "r &= a\\; \\textrm{mod}\\; b\n",
    "\\end{align}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "64d5a150-503e-4ec4-bb1d-462001403128",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-2\n",
      "-0.5\n",
      "-1\n",
      "1.0\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "m = a*b\n",
    "d = a/b\n",
    "i = a//b\n",
    "e = a**b\n",
    "r = a%b\n",
    "\n",
    "print(m, d, i, e, r, sep='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09bab021-bdae-49e9-ae07-63017a01926e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Ein String-Objekt (Text) können sie mit Hilfe von `'Some Text'` oder `\"Some Text2\"` definieren. Definieren sie die Variable `text` mit einem beliebigen Text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "423e686b-bf72-402e-b5a5-a5c57e0b4d0a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi Mom, I am on TV!'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"Hi Mom, I am on TV!\"\n",
    "text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0057c68b-4bb3-4332-81df-ba209b56c6b8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie die Variablen `a` & `b` aus Aufgabe 1 im format `\"a = 12 und b = 12\"` (Die Werte sollen dann den Werten aus ihrer Definition entsprechen. 12 ist hier nur ein Beispiel) aus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fec0692d-27e8-449d-83e7-621ec032d83c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1 und b = -2\n"
     ]
    }
   ],
   "source": [
    "print(\"a = {} und b = {}\".format(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "012f63e6-c1d1-40a6-abc5-ef1b4869f6b9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1 und b = -2\n"
     ]
    }
   ],
   "source": [
    "print(f\"a = %d und b = %d\" % (a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "45e524e7-8ff3-4266-85b3-126625f95eec",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1 und b = -2\n"
     ]
    }
   ],
   "source": [
    "print(f\"a = {a} und b = {b}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "364cb8f4-8b51-4196-86fa-324135438751",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Listen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d46cf9-1518-4ff6-a781-4c7555e39371",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Definieren Sie die Variable `l` und weisen Sie dieser Variable eine Liste mit aufsteigenden Integerwerten von `0` bis `4` zu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b759648a-2d01-418e-99be-562cb14faebd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l  = list(range(5))\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1ca653eb-426a-446d-a643-f14d7de165cc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [0, 1, 2, 3, 4]\n",
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f0af0b5-98bf-4826-8bea-b5fce950e12a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Hängen Sie der Liste `l` noch den Wert `42` an.\n",
    "\n",
    "Hinweis: Nutzen Sie dafür die Methode [.append](https://www.w3schools.com/python/ref_list_append.asp)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b524e552-0c7b-4b60-8c8f-f5c8da933d62",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 42]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append(42)\n",
    "l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5612150-549b-4a4a-b577-b07841cdb65c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie das dritte Element der Liste `l` aus.\n",
    "\n",
    "Hinweis: Achten Sie darauf das der erste Index immer `0` ist. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3f75eb1b-4f3b-43fc-8b7c-5d4baa20768d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ef22b7-383e-4faa-b53b-7c37e9386bdc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie das vorletzte Element der Liste `l` aus.\n",
    "\n",
    "Hinweis: Achten Sie darauf das der letzte Index mit `-1` ausgegeben wird"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f9efaac7-3060-45c8-a6f6-12e3c4a0fe39",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4405f8c-e423-4628-84cb-87afe9993396",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97ac1164-424f-48da-bbcf-418cc5017ebf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Initialisieren Sie die Dictionary Variable `my_dict` mit folgendem Mapping:\n",
    "\n",
    "| Key | Value |\n",
    "|:----|:------|\n",
    "| `\"apple\"` | `\"Apfel\"` |\n",
    "| `\"banana\"` | `\"Banane\"` |\n",
    "| `\"cherry\"` | `\"Kirsche\"` |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "20919c22-ff12-42ec-b682-2a0aafcbdc47",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "my_dict = {\"apple\": \"Apfel\", \"banana\": \"Banane\", \"cherry\": \"Kirsche\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ec352a4-c1f6-4ad5-9b41-d9e35eb4749b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe:** Fügen Sie nun das Key-Value Paar `\"pear\": \"Birne\"` zu `my_dict` hinzu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "74f9b918-36e0-4853-a389-241293882f75",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 'Apfel', 'banana': 'Banane', 'cherry': 'Kirsche', 'pear': 'Birne'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict[\"pear\"] = \"Birne\"\n",
    "my_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4f9bd582-0ea0-4ffe-9332-c2d4214992d7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 'Apfel', 'banana': 'Banane', 'cherry': 'Kirsche', 'pear': 'Birne'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict.update({\"pear\": \"Birne\"})\n",
    "my_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb3b2984-6bc5-4700-9509-2e228eb027d4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie die nur die Werte des Dictionaries `my_dict` aus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "34eb6ea6-c75e-4287-903a-8c279c80b31d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['Apfel', 'Banane', 'Kirsche', 'Birne'])\n"
     ]
    }
   ],
   "source": [
    "print(my_dict.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8531b0f-7a65-4f8d-9e86-fd1949f44474",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Geben Sie die Elemente des Dictionaries `my_dict` mit der Funktion `.items()` aus. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5f4a9a4c-3f61-4813-b0e9-b4276ade30a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('apple', 'Apfel'), ('banana', 'Banane'), ('cherry', 'Kirsche'), ('pear', 'Birne')])\n"
     ]
    }
   ],
   "source": [
    "print(my_dict.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d870c8db-c271-4980-9522-88c4fae8cce4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Funktionen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a38095b-3293-4682-8721-618ebe08211e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Schreibe eine Funktion `successor` die auf jede Eingabe `+1` rechnet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "896a1f20-9538-4d78-847a-cd0b5ccba1b9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def successor(n: int) -> int:\n",
    "    return n+1\n",
    "    \n",
    "successor(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d0f876c-73d4-4dd6-830d-f06168911883",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**_ Schreibe eine Funktion `add` mit den Eingabeparametern `a` & `b`, welche die Werte von `a` & `b` miteinander addiert."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4bd74029-1deb-42b1-88f9-b82549856694",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add(a: int, b: int) -> int:\n",
    "    return a+b\n",
    "\n",
    "add(2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e52a992-b925-4c29-9065-c7c29135fc57",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Kontrollstruckturen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b78c8407-f810-4899-9b6d-e6827c9e622c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Schreibe eine Funktion `is_odd` mit einem Eingabeparameter `n` die prüft ob die eingegebene Zahl ungerade ist.\n",
    "\n",
    "Wenn die Zahl gerade ist gebe den Text `\"Gerade Zahl\"` und bei ungerade `\"Ungerade Zahl\"` zurück."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c383d4dc-84d2-4c54-83e2-30d56f4711d9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gerade Zahl\n",
      "Ungerade Zahl\n"
     ]
    }
   ],
   "source": [
    "def is_odd(n):\n",
    "    if n % 2 == 0:\n",
    "        return \"Gerade Zahl\"\n",
    "    else:\n",
    "        return \"Ungerade Zahl\"\n",
    "\n",
    "print(is_odd(8), is_odd(7), sep='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0bf049b-ac60-4008-9fa7-f38fb2bd1ae1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Schreibe eine Funktion `faktoriel` mit Eingabeparameter `n`, welche die Fakultät der Zahl `n` berechnet. Dabei ist die Fakultät definiert als\n",
    "\n",
    "$$f(n) = n! = 1 \\cdot 2 \\cdot 3 \\cdot ... \\cdot n-1 \\cdot n$$\n",
    "\n",
    "Ein Beispiel für die Zahl 5:\n",
    "\n",
    "$$5! = 1 \\cdot 2 \\cdot 3 \\cdot 4 \\cdot 5$$\n",
    "$$\\Rightarrow 5! = 2 \\cdot 3 \\cdot 4 \\cdot 5$$\n",
    "$$\\Rightarrow 5! = 6 \\cdot 4 \\cdot 5$$\n",
    "$$\\Rightarrow 5! = 24 \\cdot 5$$\n",
    "$$\\Rightarrow 5! = 120$$\n",
    "\n",
    "Für den Fall `n=0` ist $0!=1$ für negative (-1) und gebrochene Zahlen (3.14) ist die Fakultät nicht definiert, gebe daher für die Spezialfälle `-1` zurück."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "69704fa0-66e9-44ef-915e-2da0039e1f17",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def faktoriel(n: int) -> int:\n",
    "    if not isinstance(n, int):\n",
    "        return -1  \n",
    "    if n < 0:\n",
    "        return -1\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    \n",
    "    count = 1\n",
    "    while n > 0:\n",
    "        count *= n\n",
    "        n -= 1\n",
    "    return count\n",
    "\n",
    "faktoriel(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62383e3a-2fa3-4628-bd6b-a36bc4fe1af1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Schreibe eine Funktion `sum_up`, mit Eingabeparameter `n`, die einen `for`-loop implementiert, welcher die Zahlen von `1...n` aufsummiert.\n",
    "\n",
    "Hinweis: die Funktion `range()` zählt standardmässig von `0...n-1`. Schauen Sie sich gerne dazu die offizielle Dokumentation an [PEP 204](https://peps.python.org/pep-0204/#list-ranges)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f7d2b0e2-ece4-4194-8925-81433fec49da",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sum_up(n: int) -> int:\n",
    "    count = 0\n",
    "    for i in range(1,n+1):\n",
    "        count += i\n",
    "    return count\n",
    "\n",
    "sum_up(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3171113-2096-46a2-871e-d56c40dceb54",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Nach Gauß ist: $$\\sum_{k=1}^n k = \\frac{n(n+1)}{2}$$\n",
    "Dementsprechen ist dies eine Lösung ohne For Loop:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "2b65fd66-fdf9-48fb-b197-d3ade327e4c4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sum_up(n: int) -> int:\n",
    "    return int(n*(n+1)/2)\n",
    "\n",
    "sum_up(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef8945d5-8b0c-435d-a51b-0e1c362d63f0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Ihnen ist ein Dictionary gegeben. Ändern Sie jeden Wert in dem Dictionary nach der Formel $f(x) = x^3-1$ mittels `for-loop`.\n",
    "\n",
    "Tipp: Lassen Sie sich nicht von den Schlüsseln verwirren."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "be593191-8580-4014-b8eb-0681d7a32137",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 175615, 5: 1727, 'python': 728, 3.14: 0.48706374396146557}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
    "\n",
    "for key, value in dictionary.items():\n",
    "    dictionary[key] = value**3-1\n",
    "\n",
    "dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "5c4c4d3f-4b95-4994-a2a1-34658e2864d8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 175615, 5: 1727, 'python': 728, 3.14: 0.48706374396146557}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
    "\n",
    "dictionary = {key: value**3-1 for key, value in dictionary.items()}\n",
    "\n",
    "dictionary"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f66a998-83b3-4af6-8bba-620aaf3d2c4e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Zusatzaufgabe**: Erstellen Sie eine List mittels List Comprehension, welche die Zahlen `1...6` auf deren kubische Zahl `1...216` also der Funktion $f(x) = x^3$ abbildet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "b14187b3-7783-4254-a6a7-7e3de9e6284e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125, 216]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cubics = [n**3 for n in range(1,7)]\n",
    "cubics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed724164-6172-4050-84be-f304034f95c4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Gegeben sei ein Loop, welcher auf ein `IndexError` beim ausführen stößt. Verhindern Sie mittels `try-except` das dieser Loop einen Fehler wirft."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "8434b26e-ef2e-4a74-a8e4-f5cfd360d41f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1, -2, -4, -8, -16, -32, -64, -128, -256]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [2**n for n in range(10)]\n",
    "s = list()\n",
    "f = lambda l, i: l[i] - l[i+1]\n",
    "\n",
    "for i in range(len(l)):\n",
    "    try:\n",
    "        s.append(f(l,i))\n",
    "    except:\n",
    "        continue\n",
    "        \n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "456a2413-2a93-4cf2-ba20-09b9adea1d8a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Inputs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e57a3e9-9755-4880-bb08-f2efc949d858",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Schreibe ein Programm, welches den Input vom User als eine Zahl entgegen nimmt und diese Zahl+2 zurückgibt.\n",
    "\n",
    "Wichtig ist das der Wert als String zurückgegeben wird und vorher mittels `int()` Funktion zu einer Zahl umgewandelt werden muss."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f0ceff5-9806-439f-b34a-ed475fa558b0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = int(input(\"Sag mir eine Zahl\"))\n",
    "print(a+2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cb8721a-0e32-4e6d-91a5-ab63ba5567b1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Erstellen und Öffnen sie eine Datei `testfile.txt` mit der `open` Funktion, nutzen Sie dafür das `with`-Statement. Schreiben Sie in diese Datei 100 mal den String `Python\\n`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "1b25b645-0213-4506-986d-4b8bc777d38e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open('testfile.txt', 'w') as f:\n",
    "    for _ in range(100):\n",
    "        f.write(\"Python\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99a5f2e2-a0e8-4c86-b456-6b75c1d16d78",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Öffnen Sie die zuvor erstellte Datei `testfile.txt` im Lesemodus und weißen Sie den Inhalt der `.readlines()` Funktion der Variabeln `lines` zu. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1e235a57-e963-4089-bdc6-27d921da9ab4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Länge: 100\n",
      "Erstes Element: Python\n",
      "\n"
     ]
    }
   ],
   "source": [
    "lines = None\n",
    "\n",
    "with open('testfile.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "print(\"Länge:\", len(lines))\n",
    "print(\"Erstes Element:\", lines[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19998fec-f95f-40b0-ac0e-91468a5814ec",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Alternative ohne `with` Statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f2e50078-251f-4a6b-9829-9b3f06537c02",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Länge: 100\n",
      "Erstes Element: Python\n",
      "\n"
     ]
    }
   ],
   "source": [
    "lines = None\n",
    "\n",
    "f = open('testfile.txt', 'r')\n",
    "lines = f.readlines() \n",
    "f.close()\n",
    "\n",
    "print(\"Länge:\", len(lines))\n",
    "print(\"Erstes Element:\", lines[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f5e9bbc-d3b8-4c49-b6d1-28068af6adb3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Import"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ff6348-24a5-4b9f-8a28-958589576750",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Importiere Python Built-In Library `random` und rufe zuerst aus dem Modul die Funktion `seed` auf mit dem Eingabewert `42`, und weiße danach der Variable `rand` den Wert des Funktionsaufrufes von `randint(1,100)` zu. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "a8b20ae3-a2cc-448f-9f4f-00c281ea96d9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "82"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand = None\n",
    "\n",
    "import random\n",
    "random.seed(42)\n",
    "rand = random.randint(1,100)\n",
    "\n",
    "rand"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2d77e45-0695-4e0c-b884-401e74adb970",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Importieren Sie die Built-In Library `datetime` als `dt`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "94515670-53a6-4eba-bf66-9f169c9b56e2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'datetime' from '/usr/lib/python3.11/datetime.py'>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime as dt\n",
    "\n",
    "dt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5166238e-a2ab-470b-9df8-c2203d23e859",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Importieren Sie die Funktion `sqrt` aus dem Built-In Modul `math`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "39b7f60c-94f6-4756-ab25-3d7ed550147e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function math.sqrt(x, /)>"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from math import sqrt\n",
    "\n",
    "sqrt"
   ]
  }
 ],
 "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.11.5"
  },
  "toc": {
   "base_numbering": 0
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
