{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional statements\n", "\n", "Conditional statements allow to branch in dependence of a boolean condition.\n", "\n", "In python this can be done with if-then-else constructs like in this first examples\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k is 3\n" ] } ], "source": [ "# smallest example without else-branch\n", "k=3\n", "if (k==3):\n", " print(\"k is 3\")\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k is not 3\n" ] } ], "source": [ "\n", "# here with else-branch\n", "k=5\n", "if (k==3):\n", " print(\"k is 3\")\n", "else:\n", " print(\"k is not 3\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please denote the : after the if/else and the indentiation, which is crucial. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1: simple tests\n", "Write a function which takes 2 integer arguments **a** and **b** and prints out \n", " * **ab** depending on the values of a and b" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def simple1(a,b):\n", " print(\"TODO\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n", "TODO\n", "TODO\n" ] } ], "source": [ "simple1(1,3)\n", "simple1(3,1)\n", "simple1(5,5)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 2: a switch like statement\n", "Write a fuction which takes 1 integer argument **d** in range 0..7 and then \n", "prints out the name of the weekday for\n", " * d=0 -> \"Monday\" \n", " * ...\n", " * d=7 -> \"Sunday\"\n", " \n", "Write it **readable**.\n", "What happens if d is not in correct range? What can you do then?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def weekday(d):\n", " print(\"TODO\")\n", " \n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n", "TODO\n", "TODO\n", "TODO\n", "TODO\n" ] } ], "source": [ "weekday(0)\n", "weekday(3)\n", "weekday(7)\n", "weekday(9)\n", "weekday(\"gaga\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The for loop\n", "\n", "If you want to do something for lets say 10 times you can use the for loop like" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World 0 of 10\n", "Hello World 1 of 10\n", "Hello World 2 of 10\n", "Hello World 3 of 10\n", "Hello World 4 of 10\n", "Hello World 5 of 10\n", "Hello World 6 of 10\n", "Hello World 7 of 10\n", "Hello World 8 of 10\n", "Hello World 9 of 10\n" ] } ], "source": [ "n=10\n", "for i in range(n):\n", " print(\"Hello World %d of %d\" % (i,n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The while loop \n", "\n", "When you did not know how many times you like to do the while-loop is a good choice, for example" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x= 0.000000\n", "x= 0.200000\n", "x= 0.400000\n", "x= 0.600000\n", "x= 0.800000\n" ] } ], "source": [ "h=0.2\n", "x=0\n", "while (x<1):\n", " print(\"x= %f\" % (x))\n", " x=x+h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3: \n", "Write a function which takes a string argument **s** like **Hello python** and then it prints out \n", " * in the first line: **H**\n", " * in the second line: **He**\n", " * ...\n", " * in the last line: **Hello python** \n", "every line one character more.\n", "\n", "Implement this at first with a for loop and then with a while loop\n", " " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def sillywriter_for(s):\n", " print(\"TODO with for loop\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO with for loop\n", "TODO with for loop\n", "TODO with for loop\n", "TODO with for loop\n" ] } ], "source": [ "sillywriter_for(\"Hello World\")\n", "sillywriter_for(\"A\")\n", "sillywriter_for(\"\")\n", "sillywriter_for(123)\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def sillywriter_while(s):\n", " print(\"TODO with while loop\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO with while loop\n", "TODO with while loop\n", "TODO with while loop\n", "TODO with while loop\n" ] } ], "source": [ "sillywriter_while(\"Hello World\")\n", "sillywriter_while(\"A\")\n", "sillywriter_while(\"\")\n", "sillywriter_while(123)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Breaking loops\n", "\n", "Sometimes it comes up that we can leave a loop or continue with the next iteration. Therefore you have \n", " * **break** to leave the loop immediatly\n", " * **continue** to go on with the next iteration\n", " \n", "As examples see: \n", " " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i=0\n", "i=1\n", "i=2\n", "i=3\n", "i=4\n", "loop is done\n" ] } ], "source": [ "n=20\n", "for i in range(n):\n", " print(\"i=%d\" % (i))\n", " if (i>3):\n", " break\n", " \n", "print(\"loop is done\") \n", " \n", " " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i=0\n", "more code after\n", "i=1\n", "more code after\n", "i=2\n", "more code after\n", "i=3\n", "more code after\n", "i=4\n", "i=5\n", "i=6\n", "i=7\n", "i=8\n", "i=9\n", "loop is done\n" ] } ], "source": [ "n=10\n", "for i in range(n):\n", " print(\"i=%d\" % (i))\n", " if (i>3):\n", " continue\n", " print(\"more code after\") \n", " \n", " \n", "print(\"loop is done\") \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 4 \n", "Write a function which takes a string argument **s** which returns the first occurence of the letter g.\n", "So it should return\n", "* 6 for s=\"abcdefghijkl\"\n", "* 0 for s=\"gaga\"\n", "* something useful for s=\"abc\" --> think about it.\n", "\n", "\n", "Make no useless loops if you got it.\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def find_g(s):\n", " p=0\n", " print(\"TODO\")\n", " return p\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n", "0\n", "TODO\n", "0\n", "TODO\n", "0\n", "TODO\n", "0\n" ] } ], "source": [ "print ( find_g(\"abcdefghijkl\") )\n", "print ( find_g(\"gaga\") )\n", "print ( find_g(\"abc\") )\n", "print ( find_g(42) )\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 5 - The fizbaz-test\n", "In the so called fizbaz-test you have to print out the numbers i=1,2, ... n, **but**\n", "* if i is a multiple of 3 write out **fiz** instead of i\n", "* if i is a multiple of 5 write out **baz** instead of i\n", "* if i is a multiple of 3 and 5 write out **fizbaz** instead of i\n", "\n", "In the first variant fizbaz1(n) you can do this in 1 column, but in the second variant fizbaz2(n)\n", "do this in 3 colums, like \n", "~~~~\n", " 1 baz fiz\n", " 2 fiz baz\n", " fiz 7 11\n", " 4 8 \n", "~~~~\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def fizbaz1(n):\n", " print(\"TODO\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n" ] } ], "source": [ "fizbaz1(15)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def fizbaz2(n):\n", " print(\"TODO\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n" ] } ], "source": [ "fizbaz2(47)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Understanding and completing the codes" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "an_int = 2\n", "a_float = 11.5\n", "\n", "def print_numbers(an_int, a_float):\n", " print(an_int, ',', a_float)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What will this code produce? `print_numbers(5, 12.2)` produce?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- A) 5 , 11.5\n", "- B) 2, 12.2 \n", "- C) 2 , 11.5 \n", "- D) 5 , 12.2\n", "- E) None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try this on your own." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the folllowing code" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def string_manipulator(string):\n", " \n", " output = ''\n", " for char in string:\n", " if char == 'a' or char == 'e':\n", " char = 'z' \n", " output = output + char\n", " \n", " return output" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zbcdz\n" ] } ], "source": [ "variable = 'abcde'\n", "manipulated_string = string_manipulator(variable)\n", "print(manipulated_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 6 \n", "I use an American keyboard but now someone used to a German keyboard is using it. Modify the above function so that when a variable `american` is set to `True` the letters `z` and `y` are swapped in a string. Give an example!" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def string_manipulator(string):\n", " print(\"TODO\")\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n", "TODO\n", "TODO\n" ] } ], "source": [ "string_manipulator(\"qwertzuiop\")\n", "string_manipulator(\"yxvcvbn\")\n", "string_manipulator(\"zzyyy\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 7 \n", "\n", "Write a program that loads an integer and prints its **second digit from the left** and **third digit from the right.**\n", "\n", "If the number doesn't have two digits, then its second digit from the left doesn't exist and the program should print an informative message instead of that nonexistent digit. On the other hand, if the number has less than tree digits, its third digit from the right is zero." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "\n", "def digitmod(n):\n", " \"\"\"\n", " A program that loads a natural number and prints its\n", " 1. second digit from the left,\n", " 2. third digit from the right.\n", " If the number doesn't have two/three digits, the respective digits are\n", " considered to be zero. \n", " \"\"\"\n", " print(\"TODO\")\n", " " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TODO\n" ] } ], "source": [ "digitmod(1234567)" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }