diff --git a/your-code/lab_for_loops.ipynb b/your-code/lab_for_loops.ipynb new file mode 100644 index 0000000..1ace31d --- /dev/null +++ b/your-code/lab_for_loops.ipynb @@ -0,0 +1,474 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Words" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Print every word in upper case**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PLAY\n", + "FILLING\n", + "BAR\n", + "THEATRE\n", + "EASYGOING\n", + "DATE\n", + "LEAD\n", + "THAT\n", + "STORY\n", + "ISLAND\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "for list_words in words:\n", + " print(list_words.upper())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Create a new list containing only words with 5 or more letters**" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La palabra play tiene menos de 5 letras\n", + "La palabra bar tiene menos de 5 letras\n", + "La palabra date tiene menos de 5 letras\n", + "La palabra lead tiene menos de 5 letras\n", + "La palabra that tiene menos de 5 letras\n", + "\n", + "La lista final es: ['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ], + "source": [ + "# your code here\n", + "listofwords= []\n", + "for list_words in words:\n", + " if len(list_words) >= 5:\n", + " listofwords.append(list_words)\n", + " else:\n", + " print(f\"La palabra {list_words} tiene menos de 5 letras\")\n", + "\n", + "print(f\"\\nLa lista final es: {listofwords}\" )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Print the first word starting with \"t\"**" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La primera palabra que empieza con 't' es: theatre\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "for word in words:\n", + " word = word.lower()\n", + " if word.startswith(\"t\"):\n", + " print(f\"La primera palabra que empieza con 't' es: {word}\")\n", + " break " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Create a list containing the square of every number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "# your code here\n", + "import math as mt\n", + "number = []\n", + "a = 0\n", + "for i in range(1,11):\n", + " a = i**2\n", + " number.append(a)\n", + "number.sort()\n", + "print(number)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Print a list containing the square of every odd number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 9, 25, 49, 81]\n" + ] + } + ], + "source": [ + "# your code here\n", + "import math as mt\n", + "number = []\n", + "a = 0\n", + "for i in range(1,11):\n", + " if i % 2 !=0:\n", + " a = i**2\n", + " number.append(a)\n", + "number.sort()\n", + "print(number)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Create a list with the squares of all multiples of 8 below 1000**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " [0, 64, 256, 576, 1024, 1600, 2304, 3136, 4096, 5184, 6400, 7744, 9216, 10816, 12544, 14400, 16384, 18496, 20736, 23104, 25600, 28224, 30976, 33856, 36864, 40000, 43264, 46656, 50176, 53824, 57600, 61504, 65536, 69696, 73984, 78400, 82944, 87616, 92416, 97344, 102400, 107584, 112896, 118336, 123904, 129600, 135424, 141376, 147456, 153664, 160000, 166464, 173056, 179776, 186624, 193600, 200704, 207936, 215296, 222784, 230400, 238144, 246016, 254016, 262144, 270400, 278784, 287296, 295936, 304704, 313600, 322624, 331776, 341056, 350464, 360000, 369664, 379456, 389376, 399424, 409600, 419904, 430336, 440896, 451584, 462400, 473344, 484416, 495616, 506944, 518400, 529984, 541696, 553536, 565504, 577600, 589824, 602176, 614656, 627264, 640000, 652864, 665856, 678976, 692224, 705600, 719104, 732736, 746496, 760384, 774400, 788544, 802816, 817216, 831744, 846400, 861184, 876096, 891136, 906304, 921600, 937024, 952576, 968256, 984064]\n" + ] + } + ], + "source": [ + "# your code here\n", + "import math as mt\n", + "number = []\n", + "a = 0\n", + "for i in range(0,1000,8):\n", + " a = i**2\n", + " number.append(a)\n", + "number.sort()\n", + "print(number)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## People" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "people = [\n", + " {\n", + " \"name\": \"Juan\",\n", + " \"age\": 34,\n", + " \"n_kids\": 2\n", + " },\n", + " {\n", + " \"name\": \"Pepe\",\n", + " \"age\": 27,\n", + " \"n_kids\": 0\n", + " },\n", + " {\n", + " \"name\": \"Sonia\",\n", + " \"age\": 41,\n", + " \"n_kids\": 1\n", + " },\n", + " {\n", + " \"name\": \"Lucía\",\n", + " \"age\": 22,\n", + " \"n_kids\": 2\n", + " },\n", + " {\n", + " \"name\": \"Leo\",\n", + " \"age\": 55,\n", + " \"n_kids\": 5\n", + " }\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**How many people are there?**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "[{'name': 'Juan', 'age': 34, 'n_kids': 2}, {'name': 'Pepe', 'age': 27, 'n_kids': 0}, {'name': 'Sonia', 'age': 41, 'n_kids': 1}, {'name': 'Lucía', 'age': 22, 'n_kids': 2}, {'name': 'Leo', 'age': 55, 'n_kids': 5}]\n" + ] + } + ], + "source": [ + "# your code here\n", + "personas = []\n", + "for i in people:\n", + " personas.append(i)\n", + "print(len(personas))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**How many people have kids**?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "# your code here\n", + "personas_con_hijos= 0\n", + "\n", + "for i in people:\n", + " if i[\"n_kids\"] > 0:\n", + " personas_con_hijos +=1\n", + "print(personas_con_hijos)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**How many kids do they have in total?**" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "# your code here\n", + "personas_con_hijos= 0\n", + "\n", + "for i in people:\n", + " if i[\"n_kids\"] > 0:\n", + " personas_con_hijos = personas_con_hijos + i[\"n_kids\"]\n", + "print(personas_con_hijos)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**In a year's time, names ending with \"a\" will have an extra kid. Create a list of dictionaries with people's info in a year's time**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'name': 'Juan', 'age': 34, 'n_kids': 2},\n", + " {'name': 'Pepe', 'age': 27, 'n_kids': 0},\n", + " {'name': 'Sonia', 'age': 41, 'n_kids': 2},\n", + " {'name': 'Lucía', 'age': 22, 'n_kids': 3},\n", + " {'name': 'Leo', 'age': 55, 'n_kids': 5}]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "un_hijo_mas = 0\n", + "nuevo_diccionario = []\n", + "\n", + "for i in people:\n", + " lista_actualizada = i.copy()\n", + " \n", + " if lista_actualizada[\"name\"].endswith(\"a\"):\n", + " lista_actualizada[\"n_kids\"] +=1\n", + " \n", + " nuevo_diccionario.append(lista_actualizada)\n", + "\n", + "nuevo_diccionario" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}