From d0115e78dd1abf661f89d0ebd0b55a20cb0e9eae Mon Sep 17 00:00:00 2001 From: Raquel Marques Date: Tue, 15 Sep 2026 22:47:24 +0100 Subject: [PATCH] Exercise done --- lab-python-data-structures.ipynb | 225 ++++++++++++++++++++++++++++--- 1 file changed, 203 insertions(+), 22 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..0ab0b62 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 5, 5, 5, 5]\n", + "The total sum of the grades: 25\n", + "New list: [5, 5, 5] Length 3 Number of occurrences of the score 5: 5\n" + ] + } + ], "source": [ - "# Your code here" + "student_list = []\n", + "\n", + "student_1 = int(input(\"Please, enter the grade of the first student:\"))\n", + "student_2 = int(input(\"Please, enter the grade of the second student:\"))\n", + "student_3 = int(input(\"Please, enter the grade of the third student:\"))\n", + "student_4 = int(input(\"Please, enter the grade of the fourth student:\"))\n", + "student_5 = int(input(\"Please, enter the grade of the fifth student:\"))\n", + "\n", + "student_list.append(student_1)\n", + "student_list.append(student_2)\n", + "student_list.append(student_3)\n", + "student_list.append(student_4)\n", + "student_list.append(student_5)\n", + "\n", + "print(student_list)\n", + "\n", + "total = sum(student_list)\n", + "\n", + "print(\"The total sum of the grades:\",total)\n", + "\n", + "new_list = student_list[0:6:2]\n", + "\n", + "new_list.sort()\n", + "\n", + "\n", + "print(\"New list:\",new_list,\"Length\",len(new_list),\"Number of occurrences of the score 5:\",student_list.count(5))\n", + " \n" ] }, { @@ -95,11 +131,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "orange\n", + "('apple', 'lemon', 'melon', 'banana', 'orange')\n", + "('apple', 'grape', 'melon', 'banana', 'orange', 'watermelon', 'blueberry')\n", + "('apple', 'grape', 'melon')\n", + "('orange', 'watermelon', 'blueberry')\n", + "('apple', 'grape', 'melon', 'banana', 'orange', ['orange', 'watermelon', 'blueberry'], ['apple', 'grape', 'melon'])\n", + "7\n" + ] + } + ], "source": [ - "# Your code here" + "fruits_original = ('apple','grape','melon','banana','orange')\n", + "\n", + "print(fruits_original[0])\n", + "print(fruits_original[-1])\n", + "\n", + "fruits_list_1 = list(fruits_original)\n", + "\n", + "fruits_list_1[1] = \"lemon\"\n", + "\n", + "fruits_tuple_1 = tuple(fruits_list_1)\n", + "\n", + "print(fruits_tuple_1)\n", + "\n", + "fruits_list_2 = list(fruits_original)\n", + "\n", + "fruits_list_2.append(\"watermelon\")\n", + "fruits_list_2.append(\"blueberry\")\n", + "\n", + "fruits = tuple(fruits_list_2)\n", + "\n", + "print(fruits)\n", + "\n", + "fruits_list_3 = list(fruits)\n", + "\n", + "fruits_list_3 = fruits_list_3[:3]\n", + "\n", + "fruits_tuple_2 = tuple(fruits_list_3)\n", + "\n", + "print(fruits_tuple_2)\n", + "\n", + "fruits_list_4 = list(fruits)\n", + "\n", + "fruits_list_4 = fruits_list_4[4:]\n", + "\n", + "fruits_tuple_3 = tuple(fruits_list_4)\n", + "\n", + "print(fruits_tuple_3)\n", + "\n", + "final_1 = list(fruits_original)\n", + "final_2 = list(fruits_tuple_3)\n", + "final_3 = list(fruits_tuple_2)\n", + "\n", + "final_1.append(final_2)\n", + "final_1.append(final_3)\n", + "\n", + "final_tuple = tuple(final_1)\n", + "\n", + "print(final_tuple)\n", + "print(len(final_tuple))\n" ] }, { @@ -136,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +262,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hate', 'perish', 'for', 'also', 'twice', 'great', 'I', 'will', 'say', 'end', 'know', 'with', 'And', 'suffice', 'would', 'tasted', 'fire', 'who', 'ice', 'that', 'think', 'From', 'what', 'world', 'Ive', 'Some', 'to', 'had', 'favor', 'the', 'hold', 'To', 'But', 'those', 'desire', 'it', 'enough', 'in', 'Is', 'of', 'destruction', 'if'}\n", + "42\n", + "{'we', 'are', 'I', 'see', 'and', 'say', 'dream', 'quest', 'end', 'know', 'though', 'test', 'today', 'with', 'away', 'a', 'fades', 'is', 'side', 'who', 'as', 'that', 'deem', 'made', 'think', 'From', 'what', 'Ive', 'love', 'but', 'Some', 'its', 'to', 'had', 'life', 'To', 'But', 'still', 'those', 'it', 'enough', 'Its', 'of', 'seen', 'if'}\n", + "45\n", + "{'hate', 'perish', 'for', 'also', 'twice', 'great', 'will', 'And', 'suffice', 'would', 'tasted', 'fire', 'ice', 'world', 'favor', 'the', 'hold', 'desire', 'in', 'Is', 'destruction'}\n", + "{'we', 'are', 'see', 'and', 'dream', 'quest', 'though', 'test', 'today', 'seen', 'away', 'fades', 'a', 'is', 'side', 'deem', 'made', 'love', 'but', 'its', 'life', 'still', 'Its', 'as'}\n", + "['And', 'Is', 'Its', 'a', 'also', 'and', 'are', 'as', 'away', 'but', 'deem', 'desire', 'destruction', 'dream', 'fades', 'favor', 'fire', 'for', 'great', 'hate', 'hold', 'ice', 'in', 'is', 'its', 'life', 'love', 'made', 'perish', 'quest', 'see', 'seen', 'side', 'still', 'suffice', 'tasted', 'test', 'the', 'though', 'today', 'twice', 'we', 'will', 'world', 'would']\n", + "{'hate', 'perish', 'we', 'for', 'also', 'twice', 'great', 'are', 'see', 'will', 'and', 'dream', 'quest', 'though', 'test', 'today', 'And', 'seen', 'away', 'fades', 'a', 'suffice', 'would', 'is', 'fire', 'side', 'tasted', 'as', 'ice', 'deem', 'made', 'world', 'love', 'but', 'its', 'life', 'favor', 'hold', 'the', 'still', 'desire', 'in', 'Is', 'Its', 'destruction'}\n" + ] + } + ], "source": [ - "# Your code here" + "poem = poem.replace(\",\",\"\")\n", + "poem = poem.replace(\".\",\"\")\n", + "poem = poem.replace(\"'\",\"\")\n", + "poem = poem.replace(\"’\",\"\")\n", + "\n", + "poem_set = set(poem.split())\n", + "\n", + "print(poem_set)\n", + "print(len(poem_set))\n", + "\n", + "\n", + "new_poem = new_poem.replace(\",\",\"\")\n", + "new_poem = new_poem.replace(\".\",\"\")\n", + "new_poem = new_poem.replace(\"'\",\"\")\n", + "new_poem = new_poem.replace(\"’\",\"\")\n", + "\n", + "new_poem_set = set(new_poem.split())\n", + "\n", + "print(new_poem_set)\n", + "print(len(new_poem_set))\n", + "\n", + "\n", + "print(poem_set - new_poem_set)\n", + "print(new_poem_set - poem_set)\n", + "\n", + "result = (poem_set - new_poem_set) | (new_poem_set - poem_set)\n", + "\n", + "result =sorted(result)\n", + "\n", + "print(result)\n", + "\n", + "result = set(result)\n", + "\n", + "print(result)\n" ] }, { @@ -193,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +349,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" + ] + } + ], "source": [ - "# Your code here" + "grades[\"Bob\"][\"Philosophy\"] = 100\n", + "\n", + "print(grades)" ] }, { @@ -239,14 +396,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "tuple_1 = zip(keys,values)\n", + "\n", + "my_dict = dict(tuple_1)\n", + "\n", + "print(my_dict)" ] }, { @@ -275,17 +444,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "min_dict = min(values)\n", + "\n", + "for key,value in my_dict.items():\n", + " if value == min(values):\n", + " print(key)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -299,7 +480,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,