Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 262 additions & 0 deletions cfu-data-types-extra.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"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": 10,
"metadata": {},
"outputs": [],
"source": [
"nombre = input(\"Introduce tu nombre: \")\n",
"edad = int(input(\"Introduce tu edad: \"))\n",
"direccion = input(\"Introduce tu dirección: \")\n",
"salario = float(input(\"Introduce tu salario: \"))\n",
"gastos = float(input(\"Introduce tus gastos: \"))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"700.0\n"
]
}
],
"source": [
"salario_restante = salario - gastos\n",
"print(salario_restante)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"buen_salario = salario_restante >= 500"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"juan, quien tiene 32 años y vive en madrid, tiene un 1300.0€ y unos 600.0€ gastos, con lo que tiene 700.0€. Es True€ que le quedan más de 500€ \n"
]
}
],
"source": [
"print(f\"{nombre}, quien tiene {edad} años y vive en {direccion}, tiene un {salario}€ y unos {gastos}€ gastos, con lo que tiene {salario_restante}€. Es {buen_salario}€ que le quedan más de 500€ \")"
]
},
{
"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": 32,
"metadata": {},
"outputs": [],
"source": [
"import string"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"new_poem = poem.replace(\",\", \"\")\n",
"new_poem = poem.replace(\".\", \"\")\n",
"new_poem = poem.replace(\"'\", \"\")\n",
"new_poem = poem.replace(\"’\", \"\")\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"some say the world will end in fire\n",
"some say in ice\n",
"from what ive 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": [
"print(poem)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"239\n"
]
}
],
"source": [
"print(len(new_poem))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"poem_list = new_poem.split(\" \")"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire\\nsome', 'say', 'in', 'ice\\nfrom', 'what', 'ive', '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']\n"
]
}
],
"source": [
"print(poem_list)"
]
}
],
"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.13.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}