diff --git a/your-code/main.ipynb b/your-code/main.ipynb index de27676..b59922d 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -1,297 +1,769 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Words" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "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": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Create a new list containing only words with 5 or more letters**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Print the first word starting with \"t\"**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Print a list containing the square of every odd number from 1 to 10**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## People" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "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": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**How many people have kids**?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**How many kids do they have in total?**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": [], - "source": [ - "# your code here" - ] - } - ], - "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" - }, - "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()) " + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "BoG0L3g0By_d" + }, + "source": [ + "## Words" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "nZpM5MVCBy_f" + }, + "outputs": [], + "source": [ + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qcp_1K8jBy_h" + }, + "source": [ + "**Print every word in upper case**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "id": "yUj1bLSgBy_h", + "outputId": "b10f5da1-31ba-46f1-c07c-8d980dc95083", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "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", + "#USING FOR LOOP\n", + "# Loop through the list and print each word in uppercase\n", + "for word in words:\n", + " print(word.upper())" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "\n", + "long_words = [word for word in words if len(word)>=5]\n", + "print(long_words)" + ], + "metadata": { + "id": "d9RD4R5-JMQo", + "outputId": "065c3c61-5f45-4678-b3d1-4165879511dc", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rxh9yrHJBy_i" + }, + "source": [ + "**Create a new list containing only words with 5 or more letters**" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "YieW_LUABy_i", + "outputId": "2500c07c-9895-4196-b883-8c253c282a12", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "#Initialize an empty list to store the words\n", + "long_words = []\n", + "\n", + "#Iterate through the list\n", + "for word in words:\n", + " #Check if the word has 5 or more letters\n", + " if len(word) >= 5:\n", + " long_words.append(word) #Add the word to the new list\n", + "\n", + "print(long_words)" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "\n", + "long_words = [word for word in words if len(word)>=5]\n", + "print(long_words)" + ], + "metadata": { + "id": "nV7C1TZLJXLr", + "outputId": "8d88f1e2-379b-4678-837b-2c35f19b7e6a", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "klm5UuVrBy_i" + }, + "source": [ + "**Print the first word starting with \"t\"**" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "id": "iqM5LsXCBy_j", + "outputId": "f5fc41a8-ab07-4e6b-bb24-7e9e7f8180aa", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "theatre\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "for word in words:\n", + " if word[0] == 't': #Check if the first letter is 't'\n", + " print(word)\n", + " break #Stop after finding the first match\n", + "" + ] + }, + { + "cell_type": "code", + "source": [ + "# USING COMPREHENSION LIST\n", + "first_word_starting_with_t = [word for word in words if word[0] == 't']\n", + "if first_word_starting_with_t:\n", + " print(first_word_starting_with_t[0])" + ], + "metadata": { + "id": "D0aZAH8NKfjV", + "outputId": "22e14009-737c-4338-c77b-2e3a3c541da0", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "theatre\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3mjuwN-EBy_j" + }, + "source": [ + "## Numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xMIeXXxkBy_j" + }, + "source": [ + "**Create a list containing the square of every number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "NzEj6xMLBy_k", + "outputId": "1d9aed25-2afb-4ccd-bb04-282c518e65ca", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "squares = []\n", + "for number in range(1,11): #Numbers from 1 to 10\n", + " squares.append(number**2)\n", + "print(squares)" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "squares = [number**2 for number in range(1,11)]\n", + "print(squares)" + ], + "metadata": { + "id": "44BGDB_rOI7I", + "outputId": "e4cb0ff3-cc73-45da-ad2c-ebefe4792378", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XuEiN-87By_k" + }, + "source": [ + "**Print a list containing the square of every odd number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "y6yF2PhrBy_k" + }, + "outputs": [], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "#Initialize an empty list to store the square of every odd number from 1 to 10\n", + "odd_squares = []\n", + "for number in range(1,11): #Numbers from 1 to 10\n", + " if number % 2 != 0: #Check if the number is odd\n", + " odd_squares.append(number**2)\n", + "print(odd_squares)" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "odd_squares = [number**2 for number in range(1,11) if number % 2 != 0]\n", + "print(odd_squares)" + ], + "metadata": { + "id": "EFCqtt3lPIZu" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2O33Kvy-By_k" + }, + "source": [ + "**Create a list with the squares of all multiples of 8 below 1000**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "LD6YM-NoBy_k" + }, + "outputs": [], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "# Initialize an empty list for the squares of multiples of 8 below 1000\n", + "squares_of_multiples_of_8 = []\n", + "\n", + "# Iterate through multiples of 8 below 1000\n", + "for number in range(8, 1000, 8):\n", + " # Calculate and append the square of the number\n", + " squares_of_multiples_of_8.append(number ** 2)\n", + "\n", + "# Print the final list\n", + "print(squares_of_multiples_of_8)" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "\n", + "squares_of_multiples_of_8 = [number ** 2 for number in range(8, 1000, 8) if number < 1000]\n", + "print(squares_of_multiples_of_8)" + ], + "metadata": { + "id": "uacPA8dySeqB" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EZiQqce_By_l" + }, + "source": [ + "## People" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "PSr4_rZ8By_l" + }, + "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": { + "id": "CH2JYeqyBy_l" + }, + "source": [ + "**How many people are there?**" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "IVJYZHeWBy_m", + "outputId": "9f87a921-843d-4801-c5e9-5e7f7d3f8f90", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "5\n" + ] + } + ], + "source": [ + "# your code here\n", + "number_of_people = len(people)\n", + "print(number_of_people)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GhCmCnhVBy_m" + }, + "source": [ + "**How many people have kids**?" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "xVUaFJDNBy_m", + "outputId": "0b2de497-fee8-4756-d192-28f85238c848", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "People with kids: 4\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "# Initialize a counter to track the number of people with kids\n", + "people_with_kids = 0\n", + "\n", + "# Loop through each person in the people list\n", + "for person in people:\n", + " # Check if the current person has at least one kid\n", + " if person[\"n_kids\"] > 0:\n", + " # If they have kids, increment the counter\n", + " people_with_kids += 1\n", + "\n", + "print(\"People with kids:\", people_with_kids)" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "people_with_kids = len([person for person in people if person[\"n_kids\"] > 0])\n", + "print(\"People with kids:\", people_with_kids)" + ], + "metadata": { + "id": "N1czn5evVWbx", + "outputId": "df3445e5-5ff0-47cc-ca88-a1e0fdd970ea", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "People with kids: 4\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "m4byD2spBy_n" + }, + "source": [ + "**How many kids do they have in total?**" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "atoTMq9LBy_n", + "outputId": "1d0474f2-7979-4875-f2ee-4256529be09a", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Total number of kids: 10\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "# Initialize a counter to sum the total number of kids\n", + "total_kids = 0\n", + "\n", + "# Loop through each person in the people list\n", + "for person in people:\n", + " # Add the number of kids the person has to the total count\n", + " total_kids += person[\"n_kids\"]\n", + "\n", + "# Print the total number of kids\n", + "print(\"Total number of kids:\", total_kids)\n" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "total_kids = sum([person[\"n_kids\"] for person in people])\n", + "print(\"Total number of kids:\", total_kids)" + ], + "metadata": { + "id": "HYMsreqAVf6I", + "outputId": "05f04ebe-a27c-4463-d814-663a86639491", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Total number of kids: 10\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "P8vqQcmEBy_n" + }, + "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": 19, + "metadata": { + "id": "D3UOxjhbBy_n", + "outputId": "da228d83-13ac-4b1b-d119-6afb8a29833d", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "People in a year's time: [{'name': 'Juan', 'age': 34, 'n_kids': 2}, {'name': 'Pepe', 'age': 27, 'n_kids': 0}, {'name': 'Sonia', 'age': 41, 'n_kids': 2}, {'name': 'Lucía', 'age': 22, 'n_kids': 3}, {'name': 'Leo', 'age': 55, 'n_kids': 5}]\n" + ] + } + ], + "source": [ + "#your code here\n", + "\n", + "#USING FOR LOOP\n", + "\n", + "# Initialize an empty list to store the future people data\n", + "future_people = []\n", + "\n", + "# Loop through each person in the people list\n", + "for person in people:\n", + " # Check if the person's name ends with the letter \"a\"\n", + " if person[\"name\"].endswith(\"a\"):\n", + " # Create a copy of the current person's data to avoid modifying the original list\n", + " future_person = person.copy()\n", + " # Increase the number of kids by 1 for those whose names end with \"a\"\n", + " future_person[\"n_kids\"] += 1\n", + " # Add the updated data to the future_people list\n", + " future_people.append(future_person)\n", + " else:\n", + " # If the name doesn't end with \"a\", simply add the person as they are\n", + " future_people.append(person)\n", + "\n", + "# Print the list of people's data in a year's time\n", + "print(\"People in a year's time:\", future_people)\n" + ] + }, + { + "cell_type": "code", + "source": [ + "#USING COMPREHENSION LIST\n", + "\n", + "future_people = [\n", + " {**person, \"n_kids\": person[\"n_kids\"] + 1} if person[\"name\"].endswith(\"a\") else person\n", + " for person in people\n", + "]\n", + "\n", + "# Print the updated list of people in a year's time\n", + "print(\"People in a year's time:\", future_people)\n" + ], + "metadata": { + "id": "lmVbAtyBV3cH", + "outputId": "bc090841-050a-4275-e3f9-7ab437f670d1", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "People in a year's time: [{'name': 'Juan', 'age': 34, 'n_kids': 2}, {'name': 'Pepe', 'age': 27, 'n_kids': 0}, {'name': 'Sonia', 'age': 41, 'n_kids': 2}, {'name': 'Lucía', 'age': 22, 'n_kids': 3}, {'name': 'Leo', 'age': 55, 'n_kids': 5}]\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" + }, + "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 + }, + "vscode": { + "interpreter": { + "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + } + }, + "colab": { + "provenance": [] } - }, - "types_to_exclude": [ - "module", - "function", - "builtin_function_or_method", - "instance", - "_Feature" - ], - "window_display": false }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file