{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW04 - Flipped Classroom \n", "\n", "*This JupyterNotebook is intended to practice the theory alongside the slides and is used as a \"practical check of the theory input\". There are no sample solutions and the file will not be corrected and does not have to be submitted.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### F-string " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string plus 19.2\n", "string plus 19.2\n" ] } ], "source": [ "variabel = 19.2\n", "\n", "normal_string = \"string plus\" + str(variabel)\n", "print(\"string plus \",variabel)\n", "\n", "f_string = f\"string plus {variabel}\"\n", "print(f\"string plus {variabel}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1416\n", "00012\n", "11\n", "b\n", "13\n" ] } ], "source": [ "# runden (4 stellen nach dem Komma)\n", "pi = 3.141592653589793\n", "print(f\"{pi:.4f}\")\n", "\n", "# 0-padding (5 stellen vor dem Komma)\n", "print(f\"{12:05}\")\n", "\n", "# binar \n", "print(f\"{3:b}\")\n", "\n", "# hex\n", "print(f\"{11:x}\")\n", "\n", "# oktal\n", "print(f\"{11:o}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Sequences " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_string = \"sequenz\"\n", "\n", "len(my_string)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_string[1]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s\n", "e\n", "q\n", "u\n", "e\n", "n\n", "z\n" ] } ], "source": [ "for letter in my_string:\n", " print(letter)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "print('o' in my_string)\n", "\n", "print('e' in my_string)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Juan\" in \"Juan Carlos Santiago-Dominguez Rodriguez Hernandez Martinez Fernandez\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[11], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# geht nicht, da non-mutable\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mmy_string\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "# geht nicht, da non-mutable\n", "my_string[2] = \"a\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Slicing " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ln od\n", "xedni ot drow gnol\n" ] } ], "source": [ "word = \"long word to index\"\n", "print(word[0:10:2])\n", "\n", "print(word[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Strings " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WOLFGANG\n", "wolfgang\n" ] } ], "source": [ "name = \"Wolfgang\"\n", "print(name.upper())\n", "print(name.lower())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Wolfgang', 'Amadeus', 'Mozart']\n", "['', 'path', 'to', 'a', 'file', 'deep', 'in', 'a', 'folder']\n", "folder\n" ] } ], "source": [ "# split function\n", "name = \"Wolfgang Amadeus Mozart\"\n", "print(name.split())\n", "\n", "\n", "string_path = \"//path//to//a//file//deep//in//a//folder\"\n", "print(string_path.split(\"//\"))\n", "\n", "last_folder = string_path.split(\"//\")[-1]\n", "print(last_folder)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "print(string_path.count(\"//\"))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'H\\xc3\\xb6llo'\n", "\n", "[72, 195, 182, 108, 108, 111]\n" ] } ], "source": [ "word = \"Hallo\"\n", "utf_8_word = word.encode()\n", "print(utf_8_word)\n", "\n", "# it's now bytes not String anymore\n", "print(type(utf_8_word))\n", "\n", "print(list(utf_8_word))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Lists " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "1\n", "1\n", "2\n", "String\n", "2.0\n", "True\n" ] } ], "source": [ "my_list = [1,2,\"String\",2.0]\n", "\n", "print(len(my_list))\n", "print(my_list[0])\n", "\n", "for el in my_list:\n", " print(el)\n", "\n", "print(1 in my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 34, 'String', 2.0]\n", "[30, 34, 'String', 2.0]\n" ] } ], "source": [ "my_list[0] = \"a\"\n", "print(my_list)\n", "\n", "my_list[0:2] = [30, 34]\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", "['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']\n", "['this', 'is', 'great!']\n", "['this', 'is', 'great!']\n" ] } ], "source": [ "# Dies ist eine typische Iterable (Liste), generiert durch range()\n", "print(list(range(20)))\n", "\n", "# geht auch mit strings\n", "print(list(\"Hello World\"))\n", "\n", "# liste mit Woertern - list() kann weggelassen werden\n", "print(list(\"this is great!\".split()))\n", "# split() kreiert schon eine Liste\n", "print(\"this is great!\".split())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 'String', 2.0]\n", "1\n", "[2, 'String', 2.0]\n", "[2, 'String', 2.0, 5]\n", "['a', 2, 'String', 2.0, 5]\n", "['a', 2, 'String', 2.0, 5, 1, 2, 3]\n" ] } ], "source": [ "my_list = [1,2,\"String\",2.0]\n", "print(my_list)\n", "\n", "elem = my_list.pop(0)\n", "print(elem)\n", "print(my_list)\n", "\n", "my_list.append(5)\n", "print(my_list)\n", "\n", "my_list.insert(0, \"a\")\n", "print(my_list)\n", "\n", "my_list.extend([1,2,3])\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, [1, 2, 3]]\n", "[1, 2, 3, 1, 2, 3]\n" ] } ], "source": [ "# Quiz from Theorie\n", "\n", "my_list = [1,2,3]\n", "\n", "my_list.append([1,2,3])\n", "\n", "print(my_list)\n", "\n", "# again but with extend\n", "my_list = [1,2,3]\n", "\n", "my_list.extend([1,2,3])\n", "\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 2, 3, 4, 5, 1, 2, 3]\n", "['a', 2, 3, 4, 5, 2, 3]\n" ] }, { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[44], line 13\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[38;5;66;03m# causes an error\u001b[39;00m\n\u001b[0;32m 12\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m---> 13\u001b[0m \u001b[43mmy_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(my_list)\n", "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "my_list = ['a', 1, 2, 3, 4, 5, 1, 2, 3]\n", "\n", "value = 1\n", "my_list.remove(value)\n", "print(my_list)\n", "\n", "value = 1\n", "my_list.remove(value)\n", "print(my_list)\n", "\n", "# causes an error\n", "value = 1\n", "my_list.remove(value)\n", "print(my_list)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "my_list = ['a', 1, 2, 3, 4, 5, 1, 2, 3]\n", "\n", "my_list.clear()\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3.0, 5, 10, 30]\n", "[30, 10, 5, 3.0, 2, 1]\n", "['Martin', 'martin']\n", "['martin', 'Martin']\n" ] } ], "source": [ "my_list = [5,3.0,2,1,10,30]\n", "my_list.sort()\n", "print(my_list)\n", "\n", "my_list.reverse()\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### typical errors with mutables - Copy by Reference " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "World\n", "World\n" ] } ], "source": [ "a = \"World\"\n", "b = a\n", "print(b)\n", "# b ist eine volle Kopie \n", "a = \"another World\"\n", "print(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[1, 2, 4]\n" ] } ], "source": [ "my_list = [1, 2, 3]\n", "my_list_2 = my_list\n", "print(my_list_2)\n", "\n", "# copy by reference mit Listen\n", "my_list_2[2] = 4\n", "print(my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[1, 2, 3]\n" ] } ], "source": [ "# \"richtige\" Kopien erstellen\n", "\n", "my_list = [1, 2, 3]\n", "my_list_2 = my_list.copy()\n", "print(my_list_2)\n", "\n", "# copy by reference mit Listen\n", "my_list_2[2] = 4\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Lists and loops " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterating through a sequence (list, tuple, string) has the form:\n", "\n", "```python\n", "for element in iterable\n", "```\n", "\n", "where *element* is an (arbitrary variable name) and has the content of the respective element of the iterable (e.g. list)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "my_list = [1, 2, 3, 4]\n", "for elem in my_list:\n", " print(elem)\n", "\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 4, 9, 16]\n" ] } ], "source": [ "# mit for-Schleife eine neue Liste erstellen\n", "my_list = [1,2,3,4]\n", "my_sqr_list = []\n", "\n", "for nbr in my_list:\n", " my_sqr_list.append(nbr**2)\n", "\n", "print(my_sqr_list)\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "# Erstellen einer Liste mit einer for Schleife\n", "\n", "my_list = []\n", "for i in range(10):\n", " my_list.append(i)\n", "\n", "print(my_list)\n", "\n", "# bessere Lösung wäre:\n", "my_list_alt = list(range(10))\n", "print(my_list_alt)" ] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }