{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW06 - 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", "## Functions " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hoi\n", "Hoi\n", "Hoi\n", "Hoi\n" ] } ], "source": [ "def print_word(word, n):\n", " for _ in range(n):\n", " print(word)\n", "\n", "print_word(\"Hoi\", 4)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object cannot be interpreted as an integer", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#causes an error because parameter needs to fit\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mprint_word\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", "Cell \u001b[1;32mIn[1], line 2\u001b[0m, in \u001b[0;36mprint_word\u001b[1;34m(word, n)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprint_word\u001b[39m(word, n):\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(word)\n", "\u001b[1;31mTypeError\u001b[0m: 'str' object cannot be interpreted as an integer" ] } ], "source": [ "# causes an error because parameter needs to fit\n", "print_word(4, \"hello\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hej\n", "Hej\n", "Hej\n", "Hej\n", "Hej\n" ] } ], "source": [ "# with paramter name - then it is working\n", "print_word(n=5, word=\"Hej\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n", "hi\n", "hi\n" ] } ], "source": [ "def say_hi(n=3, word=\"hi\"):\n", " for _ in range(n):\n", " print(word)\n", "\n", "say_hi()\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "olla\n", "olla\n", "olla\n", "olla\n", "olla\n" ] } ], "source": [ "say_hi(5, \"olla\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### TypeHints " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n", "3.2\n" ] } ], "source": [ "def say_hallo(n:int, word:str=\"Hallo\"):\n", " for _ in range(n):\n", " print(word)\n", "\n", "say_hallo(10, 3.2)\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "111\n" ] } ], "source": [ "def add_binary(a:int, b:int) -> str:\n", " binary_sum = bin(a+b)[2:]\n", " return binary_sum\n", "\n", "print(add_binary(5, 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### docstring" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "205.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def calc_rect_area(length:float, width:float):\n", " \"\"\" \n", " Calculate the Rectangle Area from a rectangle with width w and length l.\n", " Parameters: \n", " length (float) : Length of the Rectangle\n", " width (float) : Width of the Rectangle\n", " Returns:\n", " rect_area (float) : rectangle area (length*width)\n", " \"\"\"\n", " rect_area = length * width\n", " return rect_area\n", "\n", "# calling the function with two arguments\n", "calc_rect_area(10.0, 20.5)\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "101\n" ] } ], "source": [ "def add_binary(a, b):\n", " '''\n", " Returns the sum of two decimal numbers in binary digits.\n", " Parameters: \n", " a (int): A decimal integer\n", " b (int): Another decimal integer\n", " Returns:\n", " binary_sum (str): Binary string of the sum of a and b\n", " '''\n", " binary_sum = bin(a+b)[2:]\n", " return binary_sum\n", "\n", "print(add_binary(3, 2))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Validity - Scope Variables " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n" ] } ], "source": [ "def my_func(x):\n", " y = x ** 2\n", " return y\n", "\n", "print(my_func(5))\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'my_inner_func' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 12\u001b[0m\n\u001b[1;32m 9\u001b[0m my_inner_func()\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Achtung: my_inner_func nur in my_func sichtbar!\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m \u001b[43mmy_inner_func\u001b[49m(\u001b[38;5;241m5\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'my_inner_func' is not defined" ] } ], "source": [ "# Global Scope\n", "x = 10\n", "def my_func():\n", " ## Function Scope\n", " y = 20\n", " def my_inner_func():\n", " ## inner Scope\n", " z = 30\n", " my_inner_func()\n", "\n", "# Achtung: my_inner_func nur in my_func sichtbar!\n", "my_inner_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "### Activation exercises " ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x = 10\n", "def my_func_2(y):\n", " x = y * 10\n", " return x\n", "\n", "\n", "my_func_2(1)\n", "print(x)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x = 10\n", "def my_func_2(y):\n", " x = y * 10\n", " return x\n", "\n", "my_func_2(1)\n", "print(x)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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": 4 }