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
204 changes: 182 additions & 22 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Total sum of grades: 24\n",
"Selected sorted grades: [2, 6, 8]\n",
"Length of new list: 3\n",
"Number of occurrences of grade 5: 0\n"
]
}
],
"source": [
"# Your code here"
"# Exercise 1: Working with Lists\n",
"\n",
"grades = [] # empty list to store the grades\n",
"\n",
"# Ask the teacher to input grades\n",
"for i in range(5):\n",
" grade = int(input(f\"Enter grade for student {i + 1}: \"))\n",
" grades.append(grade)\n",
"\n",
"# Step 1: Calculate total sum\n",
"total_sum = sum(grades)\n",
"print(\"\\nTotal sum of grades:\", total_sum)\n",
"\n",
"# Step 2: Create a new list (first, third and fifth students)\n",
"selected_grades = grades[0:5:2]\n",
"\n",
"# Step 3: Sort the new list in ascending order\n",
"selected_grades.sort()\n",
"\n",
"# Step 4: Print results\n",
"print(\"Selected sorted grades:\", selected_grades)\n",
"print(\"Length of new list:\", len(selected_grades))\n",
"print(\"Number of occurrences of grade 5:\", selected_grades.count(5))"
]
},
{
Expand Down Expand Up @@ -95,11 +129,56 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First fruit: Apple\n",
"Last fruit: Orange\n",
"Updated tuple: ('Apple', 'Kiwi', 'Cherry', 'Mango', 'Orange')\n",
"Extended inventory: ('Apple', 'Kiwi', 'Cherry', 'Mango', 'Orange', 'Grapes', 'Pineapple')\n",
"Tuple 1: ('Apple', 'Kiwi', 'Cherry')\n",
"Tuple 2: ('Mango', 'Orange', 'Grapes')\n",
"Final inventory: ('Apple', 'Kiwi', 'Cherry', 'Mango', 'Orange', 'Grapes', 'Apple', 'Kiwi', 'Cherry', 'Mango', 'Orange', 'Grapes', 'Pineapple')\n",
"Length of final inventory: 13\n"
]
}
],
"source": [
"# Your code here"
"# Exercise 2: Tuples\n",
"\n",
"# Step 1: Initialize tuple\n",
"fruits = (\"Apple\", \"Banana\", \"Cherry\", \"Mango\", \"Orange\")\n",
"\n",
"# Step 2: Output first and last elements\n",
"print(\"First fruit:\", fruits[0])\n",
"print(\"Last fruit:\", fruits[-1])\n",
"\n",
"# Step 3: Replace the second element\n",
"# Tuples are immutable, so we convert to a list first\n",
"fruit_list = list(fruits)\n",
"fruit_list[1] = \"Kiwi\" # new fruit\n",
"fruits = tuple(fruit_list)\n",
"print(\"Updated tuple:\", fruits)\n",
"\n",
"# Step 4: Concatenate new tuple\n",
"extra_fruits = (\"Grapes\", \"Pineapple\")\n",
"fruits = fruits + extra_fruits\n",
"print(\"Extended inventory:\", fruits)\n",
"\n",
"# Step 5: Split tuple into two tuples of 3 elements each\n",
"t1 = fruits[:3]\n",
"t2 = fruits[3:6]\n",
"print(\"Tuple 1:\", t1)\n",
"print(\"Tuple 2:\", t2)\n",
"\n",
"# Step 6: Combine t1 and t2 with the original tuple\n",
"new_inventory = t1 + t2 + fruits\n",
"print(\"Final inventory:\", new_inventory)\n",
"print(\"Length of final inventory:\", len(new_inventory))\n"
]
},
{
Expand Down Expand Up @@ -136,7 +215,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -163,11 +242,56 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unique words in poem 1: 41\n",
"Unique words in poem 2: 42\n",
"\n",
"Words in poem 1 but not in poem 2:\n",
"{'perish', 'great', 'favor', 'hold', 'tasted', 'twice', 'destruction', 'i’ve', 'will', 'the', 'desire', 'ice', 'for', 'hate', 'would', 'in', 'also', 'suffice', 'world', 'fire'}\n",
"\n",
"Words in poem 2 but not in poem 1:\n",
"{'dream', 'made', 'away', 'ive', 'its', 'seen', 'still', 'test', 'as', 'quest', 'a', 'we', 'deem', 'are', 'fades', 'life', 'though', 'love', 'today', 'see', 'side'}\n",
"\n",
"Words in both poems (alphabetical):\n",
"['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n"
]
}
],
"source": [
"# Your code here"
"# Exercise 3: Sets\n",
"\n",
"import string\n",
"\n",
"# 1) Clean text and create sets\n",
"def clean_text(text):\n",
" text = text.lower()\n",
" text = text.translate(str.maketrans(\"\", \"\", string.punctuation))\n",
" return set(text.split())\n",
"\n",
"words_poem1 = clean_text(poem)\n",
"words_poem2 = clean_text(new_poem)\n",
"\n",
"# 2) Print number of unique words\n",
"print(\"Unique words in poem 1:\", len(words_poem1))\n",
"print(\"Unique words in poem 2:\", len(words_poem2))\n",
"\n",
"# 3) Unique in first poem\n",
"print(\"\\nWords in poem 1 but not in poem 2:\")\n",
"print(words_poem1 - words_poem2)\n",
"\n",
"# 4) Unique in second poem\n",
"print(\"\\nWords in poem 2 but not in poem 1:\")\n",
"print(words_poem2 - words_poem1)\n",
"\n",
"# 5) Words in both poems (sorted)\n",
"print(\"\\nWords in both poems (alphabetical):\")\n",
"print(sorted(words_poem1 & words_poem2))\n"
]
},
{
Expand All @@ -193,7 +317,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +326,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"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"
"# Exercise 4: Dictionaries\n",
"\n",
"grades = {\n",
" 'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n",
" 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n",
"}\n",
"\n",
"# Update Bob's Philosophy score\n",
"grades['Bob']['Philosophy'] = 100\n",
"\n",
"print(grades)\n"
]
},
{
Expand Down Expand Up @@ -239,14 +381,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"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"
"subject_scores = dict(zip(keys, values))\n",
"print(subject_scores)\n"
]
},
{
Expand Down Expand Up @@ -275,17 +426,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Subject with minimum score: Chemistry\n"
]
}
],
"source": [
"# Your code here"
"minimum_subject = min(subject_scores, key=subject_scores.get)\n",
"print(\"Subject with minimum score:\", minimum_subject)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +459,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down