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
29 changes: 7 additions & 22 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
]
"source": "# Exercise 1: Working with Lists\n\n# Input grades for 5 students\ngrades = []\nfor i in range(1, 6):\n grade = int(input(f\"Enter grade for student {i}: \"))\n grades.append(grade)\n\n# Total sum of all grades\nprint(f\"Total sum of grades: {sum(grades)}\")\n\n# Select 1st, 3rd, 5th students (indices 0, 2, 4) using slicing, sort ascending\nselected = sorted(grades[0:5:2])\n\nprint(f\"Selected grades (sorted): {selected}\")\nprint(f\"Length of selected list: {len(selected)}\")\nprint(f\"Occurrences of score 5: {selected.count(5)}\")"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -98,9 +96,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
]
"source": "# Exercise 2: Tuples\n\n# Initialize tuple with 5 fruits\nfruits = (\"apple\", \"banana\", \"cherry\", \"mango\", \"grape\")\n\n# First and last elements\nprint(f\"First fruit: {fruits[0]}, Last fruit: {fruits[-1]}\")\n\n# Replace second element (tuples are immutable — convert via list)\nfruits_list = list(fruits)\nfruits_list[1] = \"strawberry\"\nfruits = tuple(fruits_list)\nprint(f\"Updated tuple: {fruits}\")\n\n# Concatenate 2 additional fruits\nextra_fruits = (\"kiwi\", \"watermelon\")\nextended_fruits = fruits + extra_fruits\nprint(f\"Extended inventory: {extended_fruits}\")\n\n# Split into 2 tuples of 3 elements each\nfirst_half = extended_fruits[:3]\nsecond_half = extended_fruits[-3:]\nprint(f\"First half: {first_half}\")\nprint(f\"Second half: {second_half}\")\n\n# Combine both halves + original tuple\nfinal_tuple = first_half + second_half + fruits\nprint(f\"Final inventory: {final_tuple}\")\nprint(f\"Length of final tuple: {len(final_tuple)}\")"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -166,9 +162,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
]
"source": "# Exercise 3: Sets\nimport string\n\ndef words_in_poem(poem):\n # Remove punctuation and convert to lowercase\n translator = str.maketrans(\"\", \"\", string.punctuation)\n return set(poem.lower().translate(translator).split())\n\nset1 = words_in_poem(poem)\nset2 = words_in_poem(new_poem)\n\nprint(f\"Unique words in poem 1: {len(set1)}\")\nprint(f\"Unique words in poem 2: {len(set2)}\")\n\nprint(f\"\\nWords only in poem 1: {set1 - set2}\")\nprint(f\"\\nWords only in poem 2: {set2 - set1}\")\n\ncommon = sorted(set1 & set2)\nprint(f\"\\nWords in both poems (alphabetical): {common}\")"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -205,9 +199,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
]
"source": "# Exercise 4: Dictionaries\n\n# Update Bob's Philosophy score to 100\ngrades['Bob']['Philosophy'] = 100\n\nprint(grades)"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -242,12 +234,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n",
"values = [75, 85, 60,90]\n",
"\n",
"# Your code here"
]
"source": "# Bonus 1: Convert two lists into a dictionary using zip()\n\nkeys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\nvalues = [75, 85, 60, 90]\n\nsubject_scores = dict(zip(keys, values))\nprint(subject_scores)"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -278,9 +265,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
]
"source": "# Bonus 2: Get the subject with the minimum score\n\nmin_subject = min(subject_scores, key=subject_scores.get)\nprint(f\"Subject with minimum score: {min_subject} ({subject_scores[min_subject]})\")"
}
],
"metadata": {
Expand All @@ -304,4 +289,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}