From 486558838d718542aa253f3db9a117056e6eefda Mon Sep 17 00:00:00 2001 From: mithilamp Date: Mon, 23 Mar 2026 15:58:13 +0100 Subject: [PATCH] solved lab 2 exercises --- your-code/main.ipynb | 196 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 38 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index de27676..37e9c2e 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -25,11 +25,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['PLAY', 'FILLING', 'BAR', 'THEATRE', 'EASYGOING', 'DATE', 'LEAD', 'THAT', 'STORY', 'ISLAND']\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "upper_case = []\n", + "for word in words:\n", + " upper_case.append(word.upper())\n", + "print(upper_case)\n" ] }, { @@ -41,11 +53,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "new_list = []\n", + "for word in words:\n", + " if len(word) >= 5:\n", + " new_list.append(word)\n", + "print(new_list)" ] }, { @@ -57,11 +82,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['filling']\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "new_list = []\n", + "for word in words:\n", + " if word.startswith('f'):\n", + " new_list.append(word)\n", + "print(new_list)" ] }, { @@ -80,11 +118,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "square = []\n", + "for i in range(1, 11):\n", + " square.append(i**2)\n", + "print(square)\n" ] }, { @@ -98,9 +148,22 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 9, 25, 49, 81]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "odd_square = []\n", + "for i in range(1, 11):\n", + " if i % 2 != 0:\n", + " odd_square.append(i**2)\n", + "print(odd_square)" ] }, { @@ -112,11 +175,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 256, 264, 272, 280, 288, 296, 304, 312, 320, 328, 336, 344, 352, 360, 368, 376, 384, 392, 400, 408, 416, 424, 432, 440, 448, 456, 464, 472, 480, 488, 496, 504, 512, 520, 528, 536, 544, 552, 560, 568, 576, 584, 592, 600, 608, 616, 624, 632, 640, 648, 656, 664, 672, 680, 688, 696, 704, 712, 720, 728, 736, 744, 752, 760, 768, 776, 784, 792, 800, 808, 816, 824, 832, 840, 848, 856, 864, 872, 880, 888, 896, 904, 912, 920, 928, 936, 944, 952, 960, 968, 976, 984, 992]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "result = []\n", + "for i in range(1,1000):\n", + " if i % 8 == 0:\n", + " result.append(i)\n", + "print(result)" ] }, { @@ -128,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -170,11 +246,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "there are 5 people in the list\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(f\"there are {len(people)} people in the list\")" ] }, { @@ -186,11 +271,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "there are 4 people with kids in the list\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "people_with_kids = 0\n", + "for person in people:\n", + " if person['n_kids'] > 0:\n", + " people_with_kids += 1\n", + "print(f\"there are {people_with_kids} people with kids in the list\")" ] }, { @@ -202,11 +300,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "there are 10 kids in the list\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "total_kids = 0\n", + "for person in people:\n", + " total_kids += person['n_kids']\n", + "print(f\"there are {total_kids} kids in the list\")\n" ] }, { @@ -218,17 +328,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "people next year: [{'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" + "# your code here\n", + "people_next_year = []\n", + "for person in people:\n", + " new_person = person.copy()\n", + " if new_person['name'].endswith('a'):\n", + " new_person['n_kids'] += 1\n", + " people_next_year.append(new_person)\n", + "print(f\"people next year: {people_next_year}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -242,7 +367,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" }, "toc": { "base_numbering": 1, @@ -285,11 +410,6 @@ "_Feature" ], "window_display": false - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } } }, "nbformat": 4,