917 lines
19 KiB
Plaintext
917 lines
19 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "87873422-9971-47b4-870b-82ea5cc663f2",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"# Lösungen Tutorial 1"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "373d0d4a-c11a-4fe3-b3f2-46701690fd3a",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe**: Geben Sie den Text `Hallo Python` aus. *1 Punkt*"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "bedcc819-ea7e-430e-ba1b-32543086d2dd",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Hallo Python\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(\"Hallo Python\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "612aa876-5806-4ae8-96d7-578b96e1780a",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *2 Punkte*: \n",
|
||
|
"\n",
|
||
|
"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": "fe38d9d9-9684-4d63-afe5-6eb68accf26d",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"a = 1\n",
|
||
|
"b = -2"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "bd7c915d-03b1-4c45-820b-6e2b12e81671",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *2 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Definieren Sie zwei Variablen `s` und `t` und initialisieren diese mit einem Floatwert ungleich `0`:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "6065bb38-0482-42b9-bd57-b0660de9eb7a",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"s = 1.5\n",
|
||
|
"t = -2.7"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "87ddb6c9-5291-4693-a3e1-65ce8c0a3ea4",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *2 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": "424e3dbe-ead5-4606-a1bc-6b0bb0739b40",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"-1"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"c = a + b\n",
|
||
|
"c"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c57e8354-bde5-4139-bf93-ceca856568fc",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *5 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 5,
|
||
|
"id": "c2078093-fd22-4fc2-9c79-11e716d047b1",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"m = a*b\n",
|
||
|
"d = a/b\n",
|
||
|
"i = a//b\n",
|
||
|
"e = a**b\n",
|
||
|
"r = a%b"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "04ffad98-7f3c-40df-984a-021a2ad79800",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *2 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 6,
|
||
|
"id": "68cb274d-dba5-49e8-a26e-cfb97499792f",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'Hi Mom, I am on TV!'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"text = \"Hi Mom, I am on TV!\"\n",
|
||
|
"print(text)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9dcb699b-091d-4d17-9e22-f958a34872b5",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 7,
|
||
|
"id": "745dda45-be87-4f55-a18a-fda0031148ba",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"a = 1 und b = -2\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 1. Möglichkeit Format Funktion\n",
|
||
|
"print(\"a = {} und b = {}\".format(a, b))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"id": "579bc33a-9b26-428d-a4b8-cfb0a0960ff5",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"a = 1 und b = -2\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 2. Möglichkeit 'f'-String\n",
|
||
|
"print(f\"a = {a} und b = {b}\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "df713414-9909-4130-b349-352cc8d951ed",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*: \n",
|
||
|
"\n",
|
||
|
"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": 10,
|
||
|
"id": "0371e614-f9d1-4048-a710-531b4bea7708",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"l = [0, 1, 2, 3, 4]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "41220c65-8c6b-486d-9da7-cecdce957ef3",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 11,
|
||
|
"id": "5f14b238-738e-4f37-9f80-b09b373bc387",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"l.append(42)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "007e4252-a8c5-464e-8a71-bda2718b6c5e",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 13,
|
||
|
"id": "b5f1c844-9017-459a-aebf-fabd639310d9",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"2"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l[2]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "478993a1-d922-4f3c-bde7-316e01688709",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 14,
|
||
|
"id": "f38d72ab-062e-4204-86dc-32864ef9cf5d",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"4"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l[-2]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "d19f6acb-8046-4a7b-b0b9-78fe655246dc",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 15,
|
||
|
"id": "4faaf2df-a250-41b2-aa26-d3e6a7327f01",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_dict = {\"apple\": \"Apfel\", \"banana\": \"Banane\", \"cherry\": \"Kirsche\"}"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "74939a92-ed87-44d8-894a-b6d5d5a05773",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Fügen Sie nun das Key-Value Paar `\"pear\": \"Birne\"` zu `my_dict` hinzu."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"id": "0cfbda9f-e6fe-4145-b4a4-05e6c9cdd576",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_dict[\"pear\"] = \"Birne\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "e5e4cd89-f01e-44df-91b7-7b255f5c4b67",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Geben Sie die nur die Werte des Dictionaries `my_dict` aus."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"id": "d0166c7e-6a45-461b-9369-53de7ccc7e8b",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"dict_values(['Apfel', 'Banane', 'Kirsche', 'Birne'])\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(my_dict.values())"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "7691cbcc-8af1-41be-9b01-e499d9981b48",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Geben Sie die Elemente des Dictionaries `my_dict` mit der Funktion `.items()` aus. "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 18,
|
||
|
"id": "a4f9f233-6f60-4d5d-a4ea-2f8be73be3d5",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"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": "44035bd1-3721-4510-aadc-b464f45e66a8",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"## Recap"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "1aa5ee93-7aba-4ae2-9a7f-77b133d0762b",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"```python\n",
|
||
|
"def some_function_name(param1, param2):\n",
|
||
|
" a = do_something1(param1)\n",
|
||
|
" b = do_something2(a, param2)\n",
|
||
|
" do_something3(b)\n",
|
||
|
" return b\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"\"gebe zurück\" = `return`\n",
|
||
|
"\"gebe aus\" = `print`"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "6095807e-03f2-42d0-89b8-54356206bd79",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Schreibe eine Funktion `successor` die auf jede Eingabe `+1` rechnet."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"id": "58784328-5d18-45e7-8283-226391292461",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"43"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 19,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def successor(n):\n",
|
||
|
" return n+1\n",
|
||
|
"\n",
|
||
|
"successor(42)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c792960f-f678-43f6-ba52-9e6904ec9077",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"Schreibe eine Funktion `add` mit den Eingabeparametern `a` & `b`, welche die Werte von `a` & `b` miteinander addiert."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"id": "b49b07e2-b2b6-4dc6-8ad7-32e4722616e9",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"7"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def add(a,b):\n",
|
||
|
" return a+b\n",
|
||
|
"\n",
|
||
|
"add(2,5)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "a94076aa-259b-44ee-9ee4-e239089d04fe",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** *1 Punkte*:\n",
|
||
|
"\n",
|
||
|
"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": 21,
|
||
|
"id": "e5f9cd1d-3bd7-49ff-9ea7-53899db1d911",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "subslide"
|
||
|
},
|
||
|
"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(2))\n",
|
||
|
"print(is_odd(3))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b65cb60c-34ad-40eb-b0b6-934abaab3bb6",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"source": [
|
||
|
"**Aufgabe** 7 Punkte: Schreibe eine Funktion `fubar` mit Eingabeparameter `n`.\n",
|
||
|
"Die Funktion soll wie folgt definiert sein:\n",
|
||
|
"\n",
|
||
|
"- Der Eingabeparameter `n` ist ein Integer, Floats geben `False` zurück\n",
|
||
|
"- Negative zahlen & 0 beenden die Funktion und geben `False` zurück\n",
|
||
|
"- Die Funktion zählt bis einschließlich dem Eingabeparameter\n",
|
||
|
" bsp.: $n=9 \\rightarrow 1, 2, 3, \\dots, 9$\n",
|
||
|
"- Bei jedem Schleifendurchlauf soll die Zahl bei der sich die Schleife gerade befindet mittels `print` ausgegeben werden werden.\n",
|
||
|
"- Ist der zurzeitige Schleifendurchlauf durch `3` teilbar, gebe mittels `print` denn String `Foo` aus.\n",
|
||
|
"- Ist der zurzeitige Schleifendurchlauf durch `5` teilbar, gebe mittels `print` denn String `Bar` aus.\n",
|
||
|
"- Ist der zurzeitge Schleifendurrchlauf durch `3 & 5` teilbar, gebe mittels `print` den String `FooBar` aus.\n",
|
||
|
"\n",
|
||
|
"**Tipp**: Implementiere nicht alles aufeinmal sollte Schritt für Schritt und teste deine Lösung nach jedem Schritt."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"id": "bec1be94-561e-4379-86a0-d302ef150920",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def fubar(n: int):\n",
|
||
|
" if isinstance(n, float) or n < 1:\n",
|
||
|
" return False\n",
|
||
|
"\n",
|
||
|
" count = 1\n",
|
||
|
" while count <= n:\n",
|
||
|
" msg = count\n",
|
||
|
" if count % 3 == 0:\n",
|
||
|
" msg = \"Foo\"\n",
|
||
|
" if count % 5 == 0:\n",
|
||
|
" msg = \"Bar\"\n",
|
||
|
" if count % 15 == 0:\n",
|
||
|
" msg = \"FooBar\"\n",
|
||
|
" \n",
|
||
|
" count += 1\n",
|
||
|
" print(msg, end=', ')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"id": "ad665bb0-9976-43e9-b8ab-bb52c4a0f1d4",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"slideshow": {
|
||
|
"slide_type": "slide"
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Negative Zahl False\n",
|
||
|
"Null False\n",
|
||
|
"Float False\n",
|
||
|
"Testen über 15\n",
|
||
|
"1, 2, Foo, 4, Bar, Foo, 7, 8, Foo, Bar, 11, Foo, 13, 14, FooBar, 16, "
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Testen\n",
|
||
|
"print(\"Negative Zahl\", fubar(-1))\n",
|
||
|
"print(\"Null\", fubar(0))\n",
|
||
|
"print(\"Float\", fubar(0.1))\n",
|
||
|
"print(\"Testen über 15\")\n",
|
||
|
"fubar(16)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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.12.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|