diff --git a/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb new file mode 100644 index 0000000..d6cc5ef --- /dev/null +++ b/.ipynb_checkpoints/cfu-data-types-checkpoint.ipynb @@ -0,0 +1,205 @@ +{ + "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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "You are managing your finances well.\n" + ] + } + ], + "source": [ + "name=\"Sara\"\n", + "age=35\n", + "address= \"24 Peña Sendra Crevillent\"\n", + "salary= 2600\n", + "expenses= 2000\n", + "\n", + "print(type(name))\n", + "print(type(age))\n", + "print(type(address)) \n", + "print(type(salary))\n", + "print(type(expenses))\n", + "\n", + "if salary >= expenses:\n", + " print(\"You are managing your finances well.\")\n", + "else:\n", + " print(\"You need to cut down on your expenses.\")\n", + " \n", + "name = input(\"Name: \")\n", + "age = int(input(\"Age: \"))\n", + "address = input(\"Address: \")\n", + "salary = float(input(\"Salary: \"))\n", + "expenses = float(input(\"Expenses: \"))\n", + "\n", + "\n", + "print(\"You are managing your finances well.\")\n", + "\n", + "\n", + "input(\"Press Enter to exit.\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# User information\n", + "name = input(\"Enter your name: \")\n", + "age = int(input(\"Enter your age: \"))\n", + "address = input(\"Enter your address: \")\n", + "salary = float(input(\"Enter your salary: \"))\n", + "expenses = float(input(\"Enter your expenses: \"))\n", + "\n", + "# Calculations salary vs expenses\n", + "remaining_salary = salary - expenses\n", + "is_salary_good = remaining_salary > 500\n", + "\n", + "#final message\n", + "print(\n", + " f\"{name}, who is {age} years old, and lives in {address} \"\n", + " f\"has {remaining_salary} dollars left from her salary after expenses. \"\n", + " f\"It is {is_salary_good} that she has more than $500 left.\"\n", + ")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "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": null, + "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", + "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.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/cfu-data-types.ipynb b/cfu-data-types.ipynb index e0fee02..b0f842c 100644 --- a/cfu-data-types.ipynb +++ b/cfu-data-types.ipynb @@ -1,14 +1,16 @@ { "cells": [ { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "# CFU | Data Types" + "#PRACTICE EXTRA " ] }, { - "cell_type": "markdown", + "cell_type": "raw", "metadata": {}, "source": [ "## Working with Data Types" @@ -51,11 +53,101 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "You are managing your finances well.\n" + ] + } + ], "source": [ - "# Your code here\n" + "name=\"Sara\"\n", + "age=35\n", + "address= \"24 Peña Sendra Crevillent\"\n", + "salary= 2600\n", + "expenses= 2000\n", + "\n", + "print(type(name))\n", + "print(type(age))\n", + "print(type(address)) \n", + "print(type(salary))\n", + "print(type(expenses))\n", + "\n", + "if salary >= expenses:\n", + " print(\"You are managing your finances well.\")\n", + "else:\n", + " print(\"You need to cut down on your expenses.\")\n", + " \n", + "name = input(\"Name: \")\n", + "age = int(input(\"Age: \"))\n", + "address = input(\"Address: \")\n", + "salary = float(input(\"Salary: \"))\n", + "expenses = float(input(\"Expenses: \"))\n", + "\n", + "\n", + "print(\"You are managing your finances well.\")\n", + "\n", + "\n", + "input(\"Press Enter to exit.\")\n", + " " ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter your name: SARA\n", + "Enter your age: 35\n", + "Enter your address: Partida Peña Sendra 24 Crevillente\n", + "Enter your salary: 2600\n", + "Enter your expenses: 2000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SARA, who is 35 years old, and lives in Partida Peña Sendra 24 Crevillente has 600.0 dollars left from her salary after expenses. It is True that she has more than $500 left.\n" + ] + } + ], + "source": [ + "# User information\n", + "name = input(\"Enter your name: \")\n", + "age = int(input(\"Enter your age: \"))\n", + "address = input(\"Enter your address: \")\n", + "salary = float(input(\"Enter your salary: \"))\n", + "expenses = float(input(\"Enter your expenses: \"))\n", + "\n", + "# Calculations salary vs expenses\n", + "remaining_salary = salary - expenses\n", + "is_salary_good = remaining_salary > 500\n", + "\n", + "#final message\n", + "print(\n", + " f\"{name}, who is {age} years old, and lives in {address} \"\n", + " f\"has {remaining_salary} dollars left from her salary after expenses. \"\n", + " f\"It is {is_salary_good} that she has more than $500 left.\"\n", + ")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,10 +177,21 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "259\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', 'suffice', 'python', 'is', 'awesome!']\n" + ] + } + ], "source": [ + "import string\n", + "\n", "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", "From what I’ve tasted of desire\n", @@ -97,7 +200,26 @@ "I think I know enough of hate\n", "To say that for destruction ice\n", "Is also great\n", - "And would suffice.\"\"\"" + "And would suffice.\"\"\"\n", + " \n", + "# 1. Convert to lowercase\n", + "poem_clean = poem.lower()\n", + "\n", + "# 2. Remove punctuation\n", + "poem_clean = poem_clean.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + "\n", + "# 3. Concatenate with the given string\n", + "new_string = poem_clean + \" python is awesome!\"\n", + "\n", + "# 4. Print the length\n", + "print(len(new_string))\n", + "\n", + "# 5. Split into a list\n", + "poem_list = new_string.split(\" \")\n", + "\n", + "# 6. Print the list\n", + "print(poem_list)\n", + "\n" ] }, { @@ -105,9 +227,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here\n" - ] + "source": [] } ], "metadata": { @@ -126,7 +246,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,