Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 295 additions & 19 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,29 @@
"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()\n",
"print(\"Length of new list:\", len(new_list))\n",
"print(\"Occurrences of score 5:\", new_list.count(5))"
]
},
{
Expand Down Expand Up @@ -93,13 +115,123 @@
"\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": 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', 'pear', 'mango', 'watermelon', 'orange', 'cherry', 'blueberry')"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"new_fruits=(\"cherry\",\"blueberry\")\n",
"combined=fruits + new_fruits\n",
"combined"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('banana', 'apple', 'mango', 'orange', 'cherry', 'blueberry', 'banana', 'pear', '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)}\")"
]
},
{
Expand Down Expand Up @@ -136,7 +268,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -163,11 +295,123 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 78,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the unique elements from the set1 41\n"
]
}
],
"source": [
"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": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the unique elements from set2 42\n"
]
}
],
"source": [
"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)}\")"
]
},
{
"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": [
"# Your code here"
"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": 80,
"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": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"both_poem=poem_set & new_poem_set\n",
"both_poem\n"
]
},
{
Expand All @@ -193,7 +437,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +446,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 72,
"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": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here"
"grades[\"Bob\"][\"Philosophy\"] = 100\n",
"grades"
]
},
{
Expand Down Expand Up @@ -239,14 +496,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 75,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n"
]
}
],
"source": [
"\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"
]
},
{
Expand Down Expand Up @@ -275,17 +542,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 76,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chemistry\n"
]
}
],
"source": [
"# Your code here"
"min_subject = min(combined, key=combined.get)\n",
"print(min_subject)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +575,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down