From 84627d60bcbb80169ac2de94848ac3c4512632e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my-Morgan=20PATOLE?= Date: Wed, 8 Oct 2025 10:46:59 +0200 Subject: [PATCH] lab done --- lab-python-data-structures.ipynb | 187 +++++++++++++++++++++++++++---- 1 file changed, 167 insertions(+), 20 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..b259593 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -59,9 +59,45 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[89.0, 98.0, 76.0, 99.0, 67.0]\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# We're starting by asking the teacher inputs for his students grades\n", + "grades = []\n", + "for i in range(5):\n", + " grade = float(input(\"Enter student grade: \"))\n", + " grades.append(grade)\n", + "\n", + "# Then we're calculating the total of the grades\n", + "total_grades = sum(grades)\n", + "print(grades)\n", + "print(total_grades)\n", + "\n", + "# Now we want to select only the grades of first, third and fifth students\n", + "new_list = []\n", + "for i in range(len(grades)):\n", + " if i % 2 == 0: # This condition checks for even indices (0, 2, 4)\n", + " new_list.append(grades[i])\n", + " new_list.sort() # Sorting the new list in ascending order\n", + "\n", + "print(new_list)\n", + "print(len(new_list))\n", + "\n", + "occurences = 0\n", + "for grade in new_list:\n", + " if grade == 5:\n", + " occurences += 1\n", + "\n", + "print(occurences)" ] }, { @@ -97,9 +133,43 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "kiwi\n", + "('apple', 'orange', 'ananas', 'watermelon', 'kiwi', 'grape', 'cherry')\n", + "('apple', 'orange', 'ananas', 'watermelon', 'kiwi', 'apple', 'orange', 'ananas', 'kiwi', 'grape', 'cherry')\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# We're starting by initializing a tuple with some fruits\n", + "fruits = (\"apple\", \"banana\", \"ananas\", \"watermelon\", \"kiwi\")\n", + "\n", + "# We're now outputting the first and last fruits in the tuple to see the full range\n", + "print(fruits[0]) # First fruit\n", + "print(fruits[-1]) # Last fruit\n", + "\n", + "# Now we want to replace the second element, so we need to convert the tuple to a list first\n", + "fruits_list = list(fruits)\n", + "fruits_list[1] = \"orange\" # Replacing the second element\n", + "updated_fruits = tuple(fruits_list) # Converting back to tuple\n", + "new_fruits = updated_fruits + (\"grape\", \"cherry\") # Adding 2 new fruits to the original tuple\n", + "print(new_fruits)\n", + "\n", + "# We now want to splits the tuple into two different tuples of 3 elements each\n", + "tuple1 = new_fruits[:3] # First three elements\n", + "tuple2 = new_fruits[4:] # Next three elements\n", + "\n", + "# Finally, we combine both tuples with the original tuple\n", + "combined_tuple = updated_fruits + tuple1 + tuple2\n", + "print(combined_tuple)\n", + "\n" ] }, { @@ -136,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +233,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of unique words in the first poem is: 41\n", + "The number of unique words in the second poem is: 42\n", + "Words unique to the first poem: {'ice', 'would', 'fire', 'desire', 'will', 'the', 'destruction', 'hold', 'hate', 'i’ve', 'in', 'tasted', 'great', 'favor', 'for', 'suffice', 'twice', 'perish', 'also', 'world'}\n", + "Words unique to the second poem: {'a', 'away', 'side', 'love', 'we', 'test', \"it's\", 'deem', 'quest', 'fades', 'life', 'made', 'today', 'though', 'see', \"i've\", 'seen', 'dream', 'still', 'as', 'are'}\n", + "Words common to both poems: {'some', 'but', 'what', 'and', 'of', 'know', 'i', 'who', 'think', 'if', 'from', 'say', 'is', 'those', 'it', 'with', 'end', 'had', 'that', 'enough', 'to'}\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# We want to create two sets to store each poem's unique words\n", + "set_poem = set()\n", + "set_new_poem = set()\n", + "\n", + "# We now split each poem into words and add them to their respective sets\n", + "for word in poem.split():\n", + " set_poem.add(word.strip(\".,!?;\\\"'\").lower())\n", + "\n", + "for word in new_poem.split():\n", + " set_new_poem.add(word.strip(\".,!?;\\\"'\").lower())\n", + "\n", + "# print(\"Unique words in the first poem:\", set_poem)\n", + "# print(\"Unique words in the second poem:\", set_new_poem)\n", + "\n", + "# We are now printing the number of unique words in each poem\n", + "print(\"The number of unique words in the first poem is:\", len(set_poem))\n", + "print(\"The number of unique words in the second poem is:\", len(set_new_poem))\n", + "\n", + "# We want to find unique words that are in the first poem but not in the second\n", + "unique_to_first = set_poem - set_new_poem\n", + "print(\"Words unique to the first poem:\", unique_to_first)\n", + "\n", + "# We're now doing the same for the second poem\n", + "unique_to_second = set_new_poem - set_poem\n", + "print(\"Words unique to the second poem:\", unique_to_second)\n", + "\n", + "# Finally, we want to find the words that are common to both poems\n", + "unique_to_both = set_poem & set_new_poem\n", + "print(\"Words common to both poems:\", unique_to_both)\n" ] }, { @@ -193,7 +305,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +314,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# We want to change Bob's Philosophy grade to 100\n", + "grades['Bob']['Philosophy'] = 100\n", + "\n", + "# Finally, we print the updated grades of Bob's dictionary to see the changes\n", + "print(grades['Bob'])\n" ] }, { @@ -239,14 +365,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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" + "# Your code here\n", + "\n", + "new_dict = dict(zip(keys, values))\n", + "print(new_dict)" ] }, { @@ -275,17 +412,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The minimum score is 60 in Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "print(f\"The minimum score is {min(values)} in {keys[values.index(min(values))]}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +446,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,