{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW02 - 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", "Python_Basics - HSLU" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1 \n", "### Task 1a - Variables \n", "Create the following variables / constants and stick to the naming convention.\n", "\n", "1. create a variable that contains its address as a `string`,\n", "2. create a **constant** for the number of hours in the day \n", "3. create a variable for an annual salary of 120'000 CHF\n", "\n", "\n", "### Task 1b - Output of types \n", "Output the types of all variables and constants\n", "\n", "### Task 1c - Calculations \n", "- Calculate the monthly salary (from the annual salary) in another variable\n", "- What is its data type. Explain this in a **comment block**\n", " - Provide your comment block with a striped line **before** and **after** the justification (---------)\n", "- Also provide your justification in a **print statement**. Print statement and comment block **should look the same**." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Task 1a\n", "var_1 = \"hello\"\n", "HOURS_PER_DAY = 24\n", "annual_salary = 120000\n", "\n", "# Task 1b\n", "print(type(var_1), type(HOURS_PER_DAY), type(annual_salary))\n", "\n", "# Task 1c\n", "monthly_salary = annual_salary/12\n", "print('-' *60+\n", " \"\"\"\n", " the 'float' type is a number with its decimal values.\n", "\"\"\"+ '-'*60)\n", "print(f\"monthly salary = {monthly_salary}\"[0:22], \"CHF\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2 - Strings \n", "\n", "Create a string with all letters of the alphabet (`A-Z`)\n", "- print the **5th** letter \n", "- print the **5 last** letters\n", "- print the alphabet in **reverse order**\n", "- print **every 2nd letter**\n", "- print the **14th** and **1st** letters **20 times**." ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e\n", "zyxwv\n", "zyxwvutsrqponmlkjihgfedcba\n", "acegikmoqsuwy\n", "aaaaaaaaaaaaaaaaaaaa nnnnnnnnnnnnnnnnnnnn\n" ] } ], "source": [ "x = \"abcdefghijklmnopqrstuvwxyz\"\n", "\n", "print(x[4])\n", "print(x[::-1][:5])\n", "print(x[::-1])\n", "print(x[::2])\n", "print(x[0]*20, x[13]*20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3 - Troubleshooting: \n", "\n", "Find the **logical** errors in the following code \n", "- Copy the code below into the empty code block below.\n", "- Correct the errors there\n", "- Use the **Debugger** to do this" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The doughnut with outer radius 1 and inner radius 6 has an area of -15.7075\n", "Doughnut\u0004 = -4.0\n" ] } ], "source": [ "\"\"\"\n", "Calculate the Area of a Doughnut\n", "\"\"\"\n", "\n", "# Define Radius uf inner and outer cyrcle\n", "r_1 = 3\n", "r_2 = 4\n", "\n", "# Concstants\n", "PI = 3.1415\n", "\n", "# Calculate Radius - Squared \n", "r_1_squared = r_1^2\n", "r_2_squared = r_2^2\n", "\n", "# Calculate Area of Outer and Inner Cyrcle\n", "area_1 = r_1_squared * PI\n", "area_2 = r_2_squared * PI\n", "\n", "# Subtract Circle Areas\n", "area_doughnut = area_1 - area_2\n", "\n", "\n", "# Give out result\n", "print(f\"The doughnut with outer radius {r_1_squared}\"\n", " f\" and inner radius {r_2_squared} has an area of {area_doughnut}\")\n", "\n", "\n", "# Calculate how much Area a person would get, if the doughnut is split in fourths\n", "nr_of_people = 4\n", "area_split = area_doughnut // nr_of_people\n", "\n", "print(\"Doughnut\\4 = \",area_split)\n" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The doughnut with outer radius 4 and inner radius 3 has an area of 21.9905\n", "Doughnut area divided by 4 = 5.497625\n" ] } ], "source": [ "\"\"\"\n", "Calculate the Area of a Doughnut\n", "\"\"\"\n", "\n", "# Define Radius uf inner and outer cyrcle\n", "R_1 = 3\n", "R_2 = 4\n", "\n", "# Constants\n", "PI = 3.1415\n", "\n", "# Calculate Radius - Squared \n", "r_1_squared = r_1**2\n", "r_2_squared = r_2**2\n", "\n", "# Calculate Area of Outer and Inner Cyrcle\n", "area_1 = r_1_squared * PI\n", "area_2 = r_2_squared * PI\n", "\n", "# Subtract Circle Areas\n", "area_doughnut = area_2 - area_1\n", "\n", "\n", "# Give out result\n", "print(f\"The doughnut with outer radius {R_2}\"\n", " f\" and inner radius {R_1} has an area of {area_doughnut}\")\n", "\n", "\n", "# Calculate how much Area a person would get, if the doughnut is split in fourths\n", "nr_of_people = 4\n", "area_split = area_doughnut / nr_of_people\n", "\n", "print(f\"Doughnut area divided by {nr_of_people} = {area_split}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task 4 - Markdown \n", "\n", "Create a Markdown block under this task where you describe the errors found in task 3. \n", "\n", "- Create a title for this task and a subtitle for each documented error\n", "- Use **numbered - listings** for the justification of the different errors.\n", "- Include a **screenshot** of your IDE when you are debugging the code. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3 errors\n", "\n", "## Operation signs\n", "At line 13 and 14, the square operators used in formulas is \"^\", when the right one is \"**\"\n", "\n", "##\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 5\n", "\n", "### input() \n", "\n", "1. read in the current day of the week from the console (e.g. Monday)\n", "2. in a second input, ask for the current hour of the current time (e.g. 9)\n", "\n", "Enter the first two letters of the day of the week read in and the number of minutes since midnight **e.g. Mon 540 min.** in the program." ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fr 180 Min.\n" ] } ], "source": [ "day = input(\"day: \")\n", "hour = input(\"hour: \")\n", "minutes = ((24*60)-(int(hour)*60))\n", "print(day,minutes, \"Min.\")\n" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }