From 46afb35614f48811016fe28b998ffc5e9e666acf Mon Sep 17 00:00:00 2001 From: Jonathan Sada Date: Tue, 6 May 2025 17:14:18 +0200 Subject: [PATCH 1/2] Lab answers --- .../cfu-data-types-checkpoint.ipynb | 134 ++++++++++++++++++ cfu-data-types.ipynb | 71 ++++++++-- 2 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 .ipynb_checkpoints/cfu-data-types-checkpoint.ipynb diff --git a/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb new file mode 100644 index 0000000..e0fee02 --- /dev/null +++ b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb @@ -0,0 +1,134 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CFU | Data Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Working with Data Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Objective: Practice working with different data types and their corresponding operations." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Exercise 1: Calculate remaining salary" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this exercise, prompt the user to enter their personal information such as name, age, address, salary, and expenses, and then perform some calculations to determine how much money they have left after expenses. \n", + "\n", + "Use data type casting to ensure that the age, salary, and expenses are stored as integers or floats, as appropriate, and round the remaining money to one decimal. \n", + "\n", + "Use a boolean variable to indicate whether the remaining salary is greater than or equal to 500. \n", + "\n", + "Finally, you will use string formatting to print a message to the screen in the following format: \n", + "\"*name*, who is *age* years old, and lives in *address* has *remaining_salary* dollars left from her salary after expenses. It is *is_salary_good* that she has more than $500 left.\", using the mentioned variables.\n", + "\n", + "*Hint:* \n", + "- *You can use the input() function to ask the user to enter their information. Beware that input() function returns a String.*\n", + "- *You can round the remaining salary to one decimal using the round() function.*\n", + "- *Use data type conversion functions. Recommended external resources: [Data type conversion](https://www.geeksforgeeks.org/type-conversion-in-python/)*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### Exercise 2: Text Cleaning and Concatenation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write code that takes text from the variable `poem`, removes the punctuation and leaves everything in lowercase. \n", + "\n", + "Concatenate the resulting string with the string \"python is awesome!\" and store the result in a new variable. \n", + "\n", + "Print the length of the new string.\n", + "\n", + "Split the string into a list of strings using the space delimiter, save it in a variable `poem_list` and print it. \n", + "\n", + "*Hint:*\n", + "\n", + "- *You can use the len() function to get the length of a string.*\n", + "- *Search string methods to accomplish the other steps. Recommended External Resources: [Python String Methods](https://www.w3schools.com/python/python_ref_string.asp)*\n", + "- *Use method chaining to simplify your code. If you are not sure what it is, read this tutorial before: https://pyneng.readthedocs.io/en/latest/book/04_data_structures/method_chaining.html*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/cfu-data-types.ipynb b/cfu-data-types.ipynb index e0fee02..a1c3313 100644 --- a/cfu-data-types.ipynb +++ b/cfu-data-types.ipynb @@ -49,11 +49,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What is your name? Jona\n", + "How old are you? 40,2\n" + ] + }, + { + "ename": "ValueError", + "evalue": "could not convert string to float: '40,2'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here\u001b[39;00m\n\u001b[1;32m 2\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat is your name?\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHow old are you?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4\u001b[0m address \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat is your address?\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 5\u001b[0m salary \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mfloat\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHow much is your salary?\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", + "\u001b[0;31mValueError\u001b[0m: could not convert string to float: '40,2'" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "name = input(\"What is your name?\")\n", + "age = float(input(\"How old are you?\"))\n", + "address = input(\"What is your address?\")\n", + "salary = float(input(\"How much is your salary?\"))\n", + "expenses = float(input(\"How much are your expenses?\"))\n", + "\n", + "remaining_salary = salary - expenses\n", + "is_salary_good = (remaining_salary > 500)\n", + "\n", + "print(f\"{name}, who is {age} years old, and lives in {address} has {remaining_salary} dollars left from her salary after expenses. It is {is_salary_good} that she has more than $500 left.\")" ] }, { @@ -85,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -102,11 +132,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "258\n" + ] + }, + { + "ename": "ValueError", + "evalue": "empty separator", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[25], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m poem_2 \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpython is awesome!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mlen\u001b[39m(poem_2))\n\u001b[0;32m----> 7\u001b[0m poem_list \u001b[38;5;241m=\u001b[39m \u001b[43mpoem_2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: empty separator" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "poem_2 = ''.join(ch for ch in poem if ch not in set(string.punctuation))\n", + "poem_2 = poem_2.lower() \n", + "poem_2 += \"python is awesome!\"\n", + "print(len(poem_2))\n", + "\n", + "poem_list = poem_2.split(\"\")" ] } ], @@ -126,7 +181,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.0" } }, "nbformat": 4, From 64841c9fca4503f4b2014d9e706585d1cd38e66a Mon Sep 17 00:00:00 2001 From: Jonathan Sada Date: Tue, 6 May 2025 17:18:20 +0200 Subject: [PATCH 2/2] Lab improvements --- .../cfu-data-types-checkpoint.ipynb | 62 ++++++++++++++++--- cfu-data-types.ipynb | 43 +++++-------- 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb index e0fee02..def0bc6 100644 --- a/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb +++ b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb @@ -49,11 +49,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What is your name? Jopna\n", + "How old are you? 40\n", + "What is your address? sldkfsldf\n", + "How much is your salary? 545768\n", + "How much are your expenses? 65\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Jopna, who is 40.0 years old, and lives in sldkfsldf has 545703.0 dollars left from her salary after expenses. It is True that she has more than $500 left.\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "name = input(\"What is your name?\")\n", + "age = float(input(\"How old are you?\"))\n", + "address = input(\"What is your address?\")\n", + "salary = float(input(\"How much is your salary?\"))\n", + "expenses = float(input(\"How much are your expenses?\"))\n", + "\n", + "remaining_salary = salary - expenses\n", + "is_salary_good = (remaining_salary >= 500)\n", + "\n", + "print(f\"{name}, who is {age} years old, and lives in {address} has {remaining_salary} dollars left from her salary after expenses. It is {is_salary_good} that she has more than $500 left.\")" ] }, { @@ -85,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -102,11 +131,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "258\n", + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire\\nsome', 'say', 'in', 'ice\\nfrom', 'what', 'i’ve', 'tasted', 'of', 'desire\\ni', 'hold', 'with', 'those', 'who', 'favor', 'fire\\nbut', 'if', 'it', 'had', 'to', 'perish', 'twice\\ni', 'think', 'i', 'know', 'enough', 'of', 'hate\\nto', 'say', 'that', 'for', 'destruction', 'ice\\nis', 'also', 'great\\nand', 'would', 'sufficepython', 'is', 'awesome!']\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import string\n", + "poem_2 = ''.join(ch for ch in poem if ch not in set(string.punctuation))\n", + "poem_2 = poem_2.lower() \n", + "poem_2 += \"python is awesome!\"\n", + "print(len(poem_2))\n", + "\n", + "poem_list = poem_2.split(\" \")\n", + "print(poem_list)" ] } ], @@ -126,7 +172,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.0" } }, "nbformat": 4, diff --git a/cfu-data-types.ipynb b/cfu-data-types.ipynb index a1c3313..def0bc6 100644 --- a/cfu-data-types.ipynb +++ b/cfu-data-types.ipynb @@ -49,26 +49,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ - "What is your name? Jona\n", - "How old are you? 40,2\n" + "What is your name? Jopna\n", + "How old are you? 40\n", + "What is your address? sldkfsldf\n", + "How much is your salary? 545768\n", + "How much are your expenses? 65\n" ] }, { - "ename": "ValueError", - "evalue": "could not convert string to float: '40,2'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here\u001b[39;00m\n\u001b[1;32m 2\u001b[0m name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat is your name?\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHow old are you?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4\u001b[0m address \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat is your address?\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 5\u001b[0m salary \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mfloat\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHow much is your salary?\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", - "\u001b[0;31mValueError\u001b[0m: could not convert string to float: '40,2'" + "name": "stdout", + "output_type": "stream", + "text": [ + "Jopna, who is 40.0 years old, and lives in sldkfsldf has 545703.0 dollars left from her salary after expenses. It is True that she has more than $500 left.\n" ] } ], @@ -81,7 +80,7 @@ "expenses = float(input(\"How much are your expenses?\"))\n", "\n", "remaining_salary = salary - expenses\n", - "is_salary_good = (remaining_salary > 500)\n", + "is_salary_good = (remaining_salary >= 500)\n", "\n", "print(f\"{name}, who is {age} years old, and lives in {address} has {remaining_salary} dollars left from her salary after expenses. It is {is_salary_good} that she has more than $500 left.\")" ] @@ -132,36 +131,28 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "258\n" - ] - }, - { - "ename": "ValueError", - "evalue": "empty separator", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[25], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m poem_2 \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpython is awesome!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mlen\u001b[39m(poem_2))\n\u001b[0;32m----> 7\u001b[0m poem_list \u001b[38;5;241m=\u001b[39m \u001b[43mpoem_2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", - "\u001b[0;31mValueError\u001b[0m: empty separator" + "258\n", + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire\\nsome', 'say', 'in', 'ice\\nfrom', 'what', 'i’ve', 'tasted', 'of', 'desire\\ni', 'hold', 'with', 'those', 'who', 'favor', 'fire\\nbut', 'if', 'it', 'had', 'to', 'perish', 'twice\\ni', 'think', 'i', 'know', 'enough', 'of', 'hate\\nto', 'say', 'that', 'for', 'destruction', 'ice\\nis', 'also', 'great\\nand', 'would', 'sufficepython', 'is', 'awesome!']\n" ] } ], "source": [ "# Your code here\n", + "import string\n", "poem_2 = ''.join(ch for ch in poem if ch not in set(string.punctuation))\n", "poem_2 = poem_2.lower() \n", "poem_2 += \"python is awesome!\"\n", "print(len(poem_2))\n", "\n", - "poem_list = poem_2.split(\"\")" + "poem_list = poem_2.split(\" \")\n", + "print(poem_list)" ] } ],