From dbdbf1af91d9f324172a1dde857a089bd8e5eb59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Tue, 8 Jul 2025 15:59:03 +0200 Subject: [PATCH] solve lab --- .../cfu-data-types-checkpoint.ipynb | 245 ++++++++++++++++++ cfu-data-types.ipynb | 129 ++++++++- 2 files changed, 365 insertions(+), 9 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..c89f729 --- /dev/null +++ b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb @@ -0,0 +1,245 @@ +{ + "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": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter your name: Pepi\n", + "Please enter your age: 43\n", + "Please enter your address: Pepi Strasse\n", + "Please enter your salary: 1200\n", + "Please enter your expenses: 900\n" + ] + } + ], + "source": [ + "user_info = {\n", + " \"name\":input(\"Please enter your name: \"),\n", + " \"age\":int(input(\"Please enter your age: \")),\n", + " \"address\":input(\"Please enter your address: \"),\n", + " \"salary\":float(input(\"Please enter your salary: \")),\n", + " \"expenses\":float(input(\"Please enter your expenses: \"))}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "salary_after = user_info[\"salary\"] - user_info[\"expenses\"]\n", + "salary_after = round(salary_after, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "goodsalbool = (salary_after >= 500)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pepi, who is 43 years old, and lives in Pepi Strasse has 300.0 dollars left from her salary after expenses. It is False that she has more than $500 left.\n" + ] + } + ], + "source": [ + "print(f\"{user_info[\"name\"]}, who is {user_info[\"age\"]} years old, and lives in {user_info[\"address\"]} has {salary_after} dollars left from her salary after expenses. It is {goodsalbool} that she has more than $500 left.\")" + ] + }, + { + "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": 17, + "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": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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\n" + ] + } + ], + "source": [ + "poem_low = poem.casefold()\n", + "poem_low = poem_low.replace(\",\", \"\")\n", + "poem_low = poem_low.replace(\".\", \"\")\n", + "poem_low = poem_low.replace(\"!\", \"\")\n", + "print(poem_low)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "259\n" + ] + } + ], + "source": [ + "new_string = \"\"\"\n", + "python is awesome!\"\"\"\n", + "poem_added = poem_low + new_string\n", + "print(len(poem_added))" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire', 'some', 'say', 'in', 'ice', 'from', 'what', 'i’ve', 'tasted', 'of', 'desire', 'i', 'hold', 'with', 'those', 'who', 'favor', 'fire', 'but', 'if', 'it', 'had', 'to', 'perish', 'twice', 'i', 'think', 'i', 'know', 'enough', 'of', 'hate', 'to', 'say', 'that', 'for', 'destruction', 'ice', 'is', 'also', 'great', 'and', 'would', 'suffice', 'python', 'is', 'awesome!']\n" + ] + } + ], + "source": [ + "poem_list = poem_added.split()\n", + "print(poem_list)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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 +} diff --git a/cfu-data-types.ipynb b/cfu-data-types.ipynb index e0fee02..c89f729 100644 --- a/cfu-data-types.ipynb +++ b/cfu-data-types.ipynb @@ -49,11 +49,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter your name: Pepi\n", + "Please enter your age: 43\n", + "Please enter your address: Pepi Strasse\n", + "Please enter your salary: 1200\n", + "Please enter your expenses: 900\n" + ] + } + ], + "source": [ + "user_info = {\n", + " \"name\":input(\"Please enter your name: \"),\n", + " \"age\":int(input(\"Please enter your age: \")),\n", + " \"address\":input(\"Please enter your address: \"),\n", + " \"salary\":float(input(\"Please enter your salary: \")),\n", + " \"expenses\":float(input(\"Please enter your expenses: \"))}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "salary_after = user_info[\"salary\"] - user_info[\"expenses\"]\n", + "salary_after = round(salary_after, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "goodsalbool = (salary_after >= 500)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pepi, who is 43 years old, and lives in Pepi Strasse has 300.0 dollars left from her salary after expenses. It is False that she has more than $500 left.\n" + ] + } + ], + "source": [ + "print(f\"{user_info[\"name\"]}, who is {user_info[\"age\"]} years old, and lives in {user_info[\"address\"]} has {salary_after} dollars left from her salary after expenses. It is {goodsalbool} that she has more than $500 left.\")" ] }, { @@ -85,7 +138,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -102,19 +155,77 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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\n" + ] + } + ], + "source": [ + "poem_low = poem.casefold()\n", + "poem_low = poem_low.replace(\",\", \"\")\n", + "poem_low = poem_low.replace(\".\", \"\")\n", + "poem_low = poem_low.replace(\"!\", \"\")\n", + "print(poem_low)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "259\n" + ] + } + ], + "source": [ + "new_string = \"\"\"\n", + "python is awesome!\"\"\"\n", + "poem_added = poem_low + new_string\n", + "print(len(poem_added))" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire', 'some', 'say', 'in', 'ice', 'from', 'what', 'i’ve', 'tasted', 'of', 'desire', 'i', 'hold', 'with', 'those', 'who', 'favor', 'fire', 'but', 'if', 'it', 'had', 'to', 'perish', 'twice', 'i', 'think', 'i', 'know', 'enough', 'of', 'hate', 'to', 'say', 'that', 'for', 'destruction', 'ice', 'is', 'also', 'great', 'and', 'would', 'suffice', 'python', 'is', 'awesome!']\n" + ] + } + ], "source": [ - "# Your code here\n" + "poem_list = poem_added.split()\n", + "print(poem_list)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -126,7 +237,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,