From 0eafbe8dfdea03a3002026d3a5b228fc802a0d0a Mon Sep 17 00:00:00 2001 From: prodduturisindhurdy Date: Sat, 11 Oct 2025 16:10:35 +0100 Subject: [PATCH 1/2] sloved lab --- lab-python-data-structures.ipynb | 297 +++++++++++++++++++++++++++++-- 1 file changed, 278 insertions(+), 19 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..6a558f0 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -61,7 +61,27 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "student1_grades=int(input(\"Enter the grades of the student1\"))\n", + "student2_grades=int(input(\"Enter the grades of the student2\"))\n", + "student3_grades=int(input(\"Enter the grades of the student3\"))\n", + "student4_grades=int(input(\"Enter the grades of the student4\"))\n", + "student5_grades=int(input(\"Enter the grades of the student5\"))\n", + "grades=[]\n", + "grades.append(student1_grades)\n", + "grades.append(student2_grades)\n", + "grades.append(student3_grades)\n", + "grades.append(student4_grades)\n", + "grades.append(student5_grades)\n", + "total_sum=sum(grades)\n", + "print(f\"total sum of the grades {total_sum}\")\n", + "new_list=[]\n", + "new_list.append(student1_grades)\n", + "new_list.append(student3_grades)\n", + "new_list.append(student5_grades)\n", + "\n", + "print(new_list)\n", + "\n", + "new_list.sort()" ] }, { @@ -93,13 +113,112 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "fruits=(\"banana\",\"apple\",\"mango\",\"watermelon\",\"orange\")\n", + "fruits[0]\n", + "fruits[-1]\n", + "\n", + "new_fruits=(\"cherry\",\"blueberry\")\n", + "combined=fruits + new_fruits\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'banana'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruits[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'orange'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruits[-1]" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "#tuple cannot be modified it is immutable it will raise an typeerror" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('banana', 'apple', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "new_fruits=(\"cherry\",\"blueberry\")\n", + "combined=fruits + new_fruits\n", + "combined" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('banana', 'apple', 'mango', 'orange', 'cherry', 'blueberry', 'banana', 'apple', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')\n", + "length of the tuple 13\n" + ] + } + ], + "source": [ + "first_tuple=('banana', 'apple', 'mango')\n", + "second_tuple=('orange', 'cherry', 'blueberry')\n", + "last_tuple=first_tuple + second_tuple + combined\n", + "print(last_tuple)\n", + "print(f\"length of the tuple {len(last_tuple)}\")" ] }, { @@ -136,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +282,117 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the unique elements from the set1 43\n" + ] + } + ], + "source": [ + "set1=poem.lower().split()\n", + "poem_set=set(set1)\n", + "print(f\"the unique elements from the set1 {len(poem_set)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the unique elements from set2 43\n" + ] + } + ], "source": [ - "# Your code here" + "set2=new_poem.lower().split()\n", + "new_poem_set=set(set2)\n", + "print(f\"the unique elements from set2 {len(new_poem_set)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the unique number of words present in the first poem not in second poem {'also', 'for', 'fire,', 'in', 'will', 'hate', 'ice', 'suffice.', 'destruction', 'ice.', 'perish', 'desire', 'great', 'hold', 'would', 'tasted', 'the', 'favor', 'twice,', 'fire.', 'world', 'i’ve'}\n" + ] + } + ], + "source": [ + "unique_words_poem_set=poem_set- new_poem_set\n", + "print(\"the unique number of words present in the first poem not in second poem {}\".format(unique_words_poem_set))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the unique number of words persent in the second poem not in the first poem {'a', 'life', 'dream,', 'are', 'as', 'deem,', 'fades', 'we', 'though', \"it's\", 'away,', \"i've\", 'seen', 'today,', 'still', 'see', 'of.', 'love,', 'test.', 'quest.', 'side', 'made'}\n" + ] + } + ], + "source": [ + "unique_words_new_poem_set=new_poem_set - poem_set\n", + "print(f\"the unique number of words persent in the second poem not in the first poem {unique_words_new_poem_set}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'and',\n", + " 'but',\n", + " 'end',\n", + " 'enough',\n", + " 'from',\n", + " 'had',\n", + " 'i',\n", + " 'if',\n", + " 'is',\n", + " 'it',\n", + " 'know',\n", + " 'of',\n", + " 'say',\n", + " 'some',\n", + " 'that',\n", + " 'think',\n", + " 'those',\n", + " 'to',\n", + " 'what',\n", + " 'who',\n", + " 'with'}" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "both_poem=poem_set & new_poem_set\n", + "both_poem\n" ] }, { @@ -193,7 +418,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +427,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': '100'}}" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "grades[\"Bob\"][\"Philosophy\"] = \"100\"\n", + "grades" ] }, { @@ -239,14 +477,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ + "del dict\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", - "\n", - "# Your code here" + "combined = dict(zip(keys, values))\n", + "print(combined)\n", + "\n" ] }, { @@ -275,17 +523,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Chemistry'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "min(combined)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +558,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.9" } }, "nbformat": 4, From 23ba9e41e0ab73ea90a09ae0f15ac586eb15eab1 Mon Sep 17 00:00:00 2001 From: prodduturisindhurdy Date: Sat, 11 Oct 2025 16:24:43 +0100 Subject: [PATCH 2/2] lab completed --- lab-python-data-structures.ipynb | 81 +++++++++++++++++++------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 6a558f0..f6a3206 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -81,7 +81,9 @@ "\n", "print(new_list)\n", "\n", - "new_list.sort()" + "new_list.sort()\n", + "print(\"Length of new list:\", len(new_list))\n", + "print(\"Occurrences of score 5:\", new_list.count(5))" ] }, { @@ -178,16 +180,27 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "fruits_list = list(fruits)\n", + "fruits_list[1] = \"pear\" # new fruit\n", + "fruits = tuple(fruits_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "('banana', 'apple', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')" + "('banana', 'pear', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')" ] }, - "execution_count": 13, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -201,14 +214,14 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "('banana', 'apple', 'mango', 'orange', 'cherry', 'blueberry', 'banana', 'apple', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')\n", + "('banana', 'apple', 'mango', 'orange', 'cherry', 'blueberry', 'banana', 'pear', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')\n", "length of the tuple 13\n" ] } @@ -282,39 +295,45 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "the unique elements from the set1 43\n" + "the unique elements from the set1 41\n" ] } ], "source": [ - "set1=poem.lower().split()\n", - "poem_set=set(set1)\n", + "import string\n", + "poem_clean = poem.lower()\n", + "for p in string.punctuation:\n", + " poem_clean = poem_clean.replace(p, \"\")\n", + "poem_set = set(poem_clean.split())\n", "print(f\"the unique elements from the set1 {len(poem_set)}\")" ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "the unique elements from set2 43\n" + "the unique elements from set2 42\n" ] } ], "source": [ - "set2=new_poem.lower().split()\n", - "new_poem_set=set(set2)\n", + "import string\n", + "new_poem_clean = new_poem.lower()\n", + "for p in string.punctuation:\n", + " new_poem_clean = new_poem_clean.replace(p, \"\")\n", + "new_poem_set = set(new_poem_clean.split())\n", "print(f\"the unique elements from set2 {len(new_poem_set)}\")" ] }, @@ -356,7 +375,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 80, "metadata": {}, "outputs": [ { @@ -385,7 +404,7 @@ " 'with'}" ] }, - "execution_count": 52, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } @@ -427,23 +446,23 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", - " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': '100'}}" + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}" ] }, - "execution_count": 29, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "grades[\"Bob\"][\"Philosophy\"] = \"100\"\n", + "grades[\"Bob\"][\"Philosophy\"] = 100\n", "grades" ] }, @@ -477,7 +496,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 75, "metadata": {}, "outputs": [ { @@ -489,7 +508,7 @@ } ], "source": [ - "del dict\n", + "\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "combined = dict(zip(keys, values))\n", @@ -523,22 +542,20 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 76, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Chemistry'" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Chemistry\n" + ] } ], "source": [ - "min(combined)" + "min_subject = min(combined, key=combined.get)\n", + "print(min_subject)" ] } ],