From aaaa1c7a96c30fab379adf04159368b57e7f25e9 Mon Sep 17 00:00:00 2001 From: D V Date: Thu, 6 Nov 2025 22:19:00 +0100 Subject: [PATCH] last 2 my brain gave up --- your-code/LAB_flow_control.ipynb | 458 +++++++++++++++++++++++++++++++ 1 file changed, 458 insertions(+) create mode 100644 your-code/LAB_flow_control.ipynb diff --git a/your-code/LAB_flow_control.ipynb b/your-code/LAB_flow_control.ipynb new file mode 100644 index 0000000..0e5665c --- /dev/null +++ b/your-code/LAB_flow_control.ipynb @@ -0,0 +1,458 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "4k_56LlPSuPt" + }, + "source": [ + "## Words" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "gpWjBcr2SuPv" + }, + "outputs": [], + "source": [ + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O-J4b6AkSuPw" + }, + "source": [ + "**Print every word in upper case**" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "AlsWNiiuSuPw", + "outputId": "d4b14579-0b4e-4013-f27f-e59c56e37592" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['PLAY', 'FILLING', 'BAR', 'THEATRE', 'EASYGOING', 'DATE', 'LEAD', 'THAT', 'STORY', 'ISLAND']\n" + ] + } + ], + "source": [ + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']\n", + "words = [w.upper() for w in words]\n", + "print(words)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "w90H9GbVSuPx" + }, + "source": [ + "Create a new list containing only words with 5 or more letters" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "JgY6YfM8SuPx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "82ab060f-c717-47de-b6ca-8bdd3fd10585" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['FILLING', 'THEATRE', 'EASYGOING', 'STORY', 'ISLAND']\n" + ] + } + ], + "source": [ + "long_words = [w for w in words if len(w) >= 5]\n", + "print(long_words)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "89tV7nwPSuPx" + }, + "source": [ + "Print the first word starting with \"t\"**" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "id": "z7GnU-12SuPx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "45192213-7fc8-4b55-9cbd-213e253d0f5b" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "theatre\n" + ] + } + ], + "source": [ + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']\n", + "word_t = [w for w in words if w[0] == 't']\n", + "print(word_t[0])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AzUiVDxjSuPy" + }, + "source": [ + "## Numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9_QzDcSTSuPy" + }, + "source": [ + "**Create a list containing the square of every number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "kROTxVw2SuPy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "fefb9b76-b615-419f-f619-4a65c28bbd6c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squares = [i**2 for i in range(1, 11)]\n", + "print(squares)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YjoTCdXhSuPy" + }, + "source": [ + "**Print a list containing the square of every odd number from 1 to 10**" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "51kLupPkSuPy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "49fce301-bad5-4d77-8165-99c58d0d973c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 9, 25, 49, 81]\n" + ] + } + ], + "source": [ + "odd_squares = [i**2 for i in range(1, 11) if i % 2 != 0]\n", + "print(odd_squares)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3jzcNv6DSuPz" + }, + "source": [ + "**Create a list with the squares of all multiples of 8 below 1000**" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "Pklx_9VJSuPz", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "eb4665b0-2bd6-45f8-a5f0-09867eaa7ab9" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[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, 1000000]\n" + ] + } + ], + "source": [ + "squares_multiples_of_8 = [i**2 for i in range(1, 1001) if i % 8 == 0]\n", + "print(squares_multiples_of_8)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hzrsltJkSuPz" + }, + "source": [ + "## People" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "p7pvACtWSuPz" + }, + "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": "hNZYXpd7SuPz" + }, + "source": [ + "**How many people are there?**" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "VYxrpRvaSuPz", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c83f9a56-66cd-4ae7-ff99-e588a2b7ec3b" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "5\n" + ] + } + ], + "source": [ + "persons = len(people)\n", + "print(persons)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G0SDMhp9SuPz" + }, + "source": [ + "**How many people have kids**?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "Sag9qdsoSuPz", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ea27df14-ff90-49b8-d3d3-0e32d6fd4572" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "5\n", + "5\n", + "5\n", + "5\n" + ] + } + ], + "source": [ + "for persons in people:\n", + " if persons[\"n_kids\"] > 0:\n", + " print(len(people))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "etHrZ_wOSuPz" + }, + "source": [ + "**How many kids do they have in total?**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "VZ33Zd-NSuP0" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dyXNnKvbSuP0" + }, + "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": { + "id": "MuuP2sFxSuP0" + }, + "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()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + }, + "vscode": { + "interpreter": { + "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + } + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file