{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW03 - PYTHON BASICS \n", "\n", "These are the self-study tasks for the semester week, which you will solve within a week in your JupyterHub environment. After completing your work, download a copy of the Jupyter notebook file locally to your laptop (right-click on the file in the JupyterHub file browser -> Download).\n", "\n", "On ILIAS you will find the corresponding scheduled work assignments every week, where you will upload your solved Jupyter notebook file. After you have submitted your work, you will receive a corresponding sample solution for the work assignment. Your submission will not be corrected. Although the work assignments are marked \"mandatory\", there are no tests in the module. The grades from the tests during the semester count.\n", "\n", "We wish you much success!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 1: Is X odd or even? \n", "\n", "Write a small program that outputs whether the variable **x** is even or odd. Test this." ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 is odd\n", "2 is even\n", "3 is odd\n", "4 is even\n", "5 is odd\n", "6 is even\n", "7 is odd\n", "8 is even\n", "9 is odd\n", "10 is even\n", "11 is odd\n", "12 is even\n", "13 is odd\n", "14 is even\n", "15 is odd\n", "16 is even\n", "17 is odd\n", "18 is even\n", "19 is odd\n", "20 is even\n", "21 is odd\n", "22 is even\n", "23 is odd\n", "24 is even\n", "25 is odd\n", "26 is even\n", "27 is odd\n", "28 is even\n", "29 is odd\n", "30 is even\n", "31 is odd\n", "32 is even\n", "33 is odd\n", "34 is even\n", "35 is odd\n", "36 is even\n", "37 is odd\n", "38 is even\n", "39 is odd\n", "40 is even\n", "41 is odd\n", "42 is even\n", "43 is odd\n", "44 is even\n", "45 is odd\n", "46 is even\n", "47 is odd\n", "48 is even\n", "49 is odd\n", "50 is even\n" ] } ], "source": [ "x = 0\n", "counter = 0\n", "\n", "while True:\n", " x += 1\n", "\n", " if x % 2 == 0 and x <= 100:\n", " counter += 1\n", " print(f\"{x} is even\")\n", " \n", " else:\n", " counter += 1\n", " print(f\"{x} is odd\")\n", "\n", " if counter == 50:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 2: Outside or in-between \n", "\n", "Write a program that checks whether x is inside or outside of ``[a...b]``. Test this" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the number must to be between 0 and 10\n" ] } ], "source": [ "a = 0\n", "b = 10\n", "x = input(\"Write a number between 0 and 10: \")\n", "cond = True\n", "\n", "while cond:\n", " if a <= int(x) <= b:\n", " print(x)\n", " break\n", "\n", " else:\n", " print(\"the number must to be between 0 and 10\")\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 3: special number output \n", "\n", "Count from **1 to 30** and:\n", "- only output numbers which:\n", " - are straight\n", " - are smaller than 24\n", " - are not square numbers (e.g. 4, 9, 16)\n", "\n", "Implement this program with both a **for** and a **while** loop." ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "6\n", "8\n", "10\n", "12\n", "14\n", "18\n", "20\n", "22\n" ] } ], "source": [ "i = 1\n", "\n", "while True:\n", "\n", " for j in range(1,30):\n", " if j != i * i and j < 24:\n", " if j % 2 == 0:\n", " print(j)\n", " continue\n", "\n", " i += 1\n", "\n", " if j > 24:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 4 - Table with Fahrenheit programmed with for loop \n", "\n", "\n", "°Celsium can be converted to °Fahrenheit using the following formula.\n", "\n", "\n", "$$\n", "{\\displaystyle \\left\\{t\\right\\}_{\\mathrm {^{\\circ }F} }=\\left\\{t\\right\\}_{\\mathrm {^{\\circ }C} }\\cdot {\\tfrac {9}{5}}+32}\n", "$$\n", "\n", "\n", "\n", "Write a program that displays the °C from -10° to +30° on the console\n", "\n", "```python\n", "-10 °C => 26.4 °F\n", "-9 °C => 27.0 °F\n", "-8 °C => 27.6 °F\n", "-7 °C => 28.1 °F\n", "-6 °C => 28.7 °F\n", "...\n", "```\n", "\n", "*Note: To round a float number you can use the function round(3.1415, 3), which in this example outputs 3.142.*" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-10 °C => 14.0 °F\n", "-9 °C => 15.8 °F\n", "-8 °C => 17.6 °F\n", "-7 °C => 19.4 °F\n", "-6 °C => 21.2 °F\n", "-5 °C => 23.0 °F\n", "-4 °C => 24.8 °F\n", "-3 °C => 26.6 °F\n", "-2 °C => 28.4 °F\n", "-1 °C => 30.2 °F\n", "0 °C => 32.0 °F\n", "1 °C => 33.8 °F\n", "2 °C => 35.6 °F\n", "3 °C => 37.4 °F\n", "4 °C => 39.2 °F\n", "5 °C => 41.0 °F\n", "6 °C => 42.8 °F\n", "7 °C => 44.6 °F\n", "8 °C => 46.4 °F\n", "9 °C => 48.2 °F\n", "10 °C => 50.0 °F\n", "11 °C => 51.8 °F\n", "12 °C => 53.6 °F\n", "13 °C => 55.4 °F\n", "14 °C => 57.2 °F\n", "15 °C => 59.0 °F\n", "16 °C => 60.8 °F\n", "17 °C => 62.6 °F\n", "18 °C => 64.4 °F\n", "19 °C => 66.2 °F\n", "20 °C => 68.0 °F\n", "21 °C => 69.8 °F\n", "22 °C => 71.6 °F\n", "23 °C => 73.4 °F\n", "24 °C => 75.2 °F\n", "25 °C => 77.0 °F\n", "26 °C => 78.8 °F\n", "27 °C => 80.6 °F\n", "28 °C => 82.4 °F\n", "29 °C => 84.2 °F\n", "30 °C => 86.0 °F\n" ] } ], "source": [ "for celsius in range(-10,31):\n", " fahrenheit = (celsius * (9/5)) + 32\n", " print(f\"{celsius} °C => {round(fahrenheit,3)} °F\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 5 - min max avg \n", "\n", "Write a program that reads integers until the letter X or x is pressed. After each number is entered, the current minimum, maximum and average are displayed on the console.\n", "\n", "Use the given structure for the query" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter integer number, X for exit the program\n", "Min: 3, Max: 3, Total: 1, Average: 3.0\n", "Min: 3, Max: 10, Total: 2, Average: 6.5\n", "Min: -40, Max: 10, Total: 3, Average: -15.0\n", "Min: -40, Max: 10, Total: 4, Average: -15.0\n", "Min: -40, Max: 56, Total: 5, Average: 8.0\n", "Min: -40, Max: 56, Total: 6, Average: 8.0\n", "program terminated\n" ] } ], "source": [ "print(\"Enter integer number, X for exit the program\")\n", "min = max\n", "max = 0\n", "total = 0\n", "count = 0\n", "\n", "user_input = input(\"Number: \")\n", "while user_input != 'X' and user_input != 'x':\n", "\n", " number = int(user_input)\n", " total += 1\n", " \n", " if number > max:\n", " max = number\n", "\n", " if number < min:\n", " min = number\n", "\n", " count = (max + min) / 2\n", "\n", " print(f\"Min: {min}, Max: {max}, Total: {total}, Average: {count}\")\n", " \n", " user_input = input(\"Number: \")\n", "\n", "print(\"program terminated\")\n" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }