From ef3fece5bd300b3748de658bbef37b44573e1131 Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 11 Jul 2026 16:28:17 +0100 Subject: [PATCH 1/2] lab 1 done --- lab-python-data-structures.ipynb | 447 +++++++++++++++++++++++++++++++ 1 file changed, 447 insertions(+) create mode 100644 lab-python-data-structures.ipynb diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb new file mode 100644 index 0000000..a624420 --- /dev/null +++ b/lab-python-data-structures.ipynb @@ -0,0 +1,447 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 1: Working with Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students. You are tasked with building the program that will allow the teacher to do this easily.\n", + "\n", + "The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, the program will calculate the total sum of the grades and display it on the screen. Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", + "\n", + "Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Hint:*\n", + "- You can use the input() function to ask the user to enter their information.\n", + "- Look for list methods to perform the tasks. \n", + "- Remember, it is possible to get a part of the sequence using:\n", + "\n", + " ```python\n", + " sequence[x:y:z]\n", + " ```\n", + " where x, y, z are integers.\n", + "\n", + " The above returns a new sequence with the following characteristics:\n", + "\n", + " - A sequence with the same type as the original (a slice of a list is a list, a slice of a tuple is a tuple, and a slice of a string is a string).\n", + " - A sequence with elements from `sequence [x]` to `sequence [y-1]` (does not include a sequence [y]). By skipping `z` elements each time, it can be omitted if ` z = 1`.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources:*\n", + "- *[Python Lists](https://www.w3schools.com/python/python_lists.asp)*\n", + "- *[Python List Methods](https://www.w3schools.com/python/python_ref_list.asp)*\n", + "- *[Python Built-in Functions](https://docs.python.org/3/library/functions.html)*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total sum of grades: 23.5\n", + "Selected and sorted grades: [4.3, 4.4, 5.0]\n", + "Length of the new list: 3\n", + "Number of occurrences of 5: 1\n" + ] + } + ], + "source": [ + "grades = []\n", + "\n", + "for i in range(5):\n", + " grade = float(input(f\"Enter the grade for student {i + 1}: \"))\n", + " grades.append(grade)\n", + "\n", + "total = sum(grades)\n", + "print(\"Total sum of grades:\", total)\n", + "\n", + "selected_grades = grades[0:5:2]\n", + "selected_grades.sort()\n", + "\n", + "print(\"Selected and sorted grades:\", selected_grades)\n", + "print(\"Length of the new list:\", len(selected_grades))\n", + "print(\"Number of occurrences of 5:\", selected_grades.count(5))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 2: Tuples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Imagine you're running a fruit stand and want to keep track of your inventory. Write a Python program that does the following:\n", + "\n", + "- Initializes a tuple with 5 different types of fruit.\n", + "- Outputs the first and last elements of the tuple, so you can see the full range of fruits the store offers.\n", + "- Replaces the second element of the tuple with a new fruit that the store has recently received, and prints the updated tuple so you can see the changes.\n", + "- Concatenates a new tuple containing 2 additional fruits to the original tuple, so you can add them to the store inventory, and prints the resulting tuple to see the updated inventory.\n", + "- Splits the resulting tuple into 2 tuples of 3 elements each (the first tuple contains the first 3 elements, and the second tuple contains the last 3 elements), so you can organize the inventory more effectively.\n", + "- Combines the 2 tuples from the previous step with the original tuple into a new tuple, and prints the resulting tuple and its length, so you can see the final inventory after all the changes." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources: [Python Tuples Examples and Methods](https://www.w3schools.com/python/python_tuples.asp)*\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First fruit: Apple\n", + "Last fruit: Grapes\n", + "Updated tuple: ('Apple', 'Pineapple', 'Orange', 'Mango', 'Grapes')\n", + "Tuple after concatenation: ('Apple', 'Pineapple', 'Orange', 'Mango', 'Grapes', 'Kiwi', 'Peach')\n", + "First tuple: ('Apple', 'Pineapple', 'Orange')\n", + "Second tuple: ('Grapes', 'Kiwi', 'Peach')\n", + "Combined tuple: (('Apple', 'Pineapple', 'Orange'), ('Grapes', 'Kiwi', 'Peach'), ('Apple', 'Pineapple', 'Orange', 'Mango', 'Grapes', 'Kiwi', 'Peach'))\n", + "Length of combined tuple: 3\n" + ] + } + ], + "source": [ + "fruits = (\"Apple\", \"Banana\", \"Orange\", \"Mango\", \"Grapes\")\n", + "\n", + "print(\"First fruit:\", fruits[0])\n", + "print(\"Last fruit:\", fruits[-1])\n", + "\n", + "fruits = (fruits[0], \"Pineapple\", fruits[2], fruits[3], fruits[4])\n", + "print(\"Updated tuple:\", fruits)\n", + "\n", + "new_fruits = (\"Kiwi\", \"Peach\")\n", + "fruits = fruits + new_fruits\n", + "print(\"Tuple after concatenation:\", fruits)\n", + "\n", + "first_part = fruits[:3]\n", + "second_part = fruits[4:] # Last 3 elements\n", + "\n", + "print(\"First tuple:\", first_part)\n", + "print(\"Second tuple:\", second_part)\n", + "\n", + "combined = (first_part, second_part, fruits)\n", + "\n", + "print(\"Combined tuple:\", combined)\n", + "print(\"Length of combined tuple:\", len(combined))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 3: Sets" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Imagine you are a data analyst working for a literature museum. Your manager has given you two poems to analyze, and she wants you to write a Python program to extract useful information from them.\n", + "\n", + "Your program should:\n", + "\n", + "- Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", + "- Print the number of unique words in each set.\n", + "- Identify and print the unique words present in the first poem but not in the second one.\n", + "- Identify and print the unique words present in the second poem but not in the first one.\n", + "- Identify and print the unique words present in both poems and print it in alphabetical order." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources:*\n", + "- *[Python Sets](https://www.w3schools.com/python/python_sets.asp)* \n", + "- *[Python Set Methods](https://www.w3schools.com/python/python_ref_set.asp)*\n", + "- *[Python String Methods](https://www.w3schools.com/python/python_ref_string.asp)*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "new_poem = \"\"\"Some say life is but a dream,\n", + "Some say it's a test.\n", + "From what I've seen and what I deem,\n", + "I side with those who see it as a quest.\n", + "\n", + "But if it had to end today,\n", + "I think I know enough of love,\n", + "To say that though it fades away,\n", + "It's still what we are made of.\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "import string" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique words in poem: 41\n", + "Unique words in new_poem: 42\n", + "\n", + "Words only in poem:\n", + "{'favor', 'perish', 'tasted', 'suffice', 'twice', 'the', 'will', 'ice', 'would', 'i’ve', 'world', 'desire', 'hold', 'great', 'hate', 'also', 'for', 'destruction', 'in', 'fire'}\n", + "\n", + "Words only in new_poem:\n", + "{'today', 'as', 'side', 'ive', 'deem', 'its', 'still', 'away', 'made', 'dream', 'test', 'see', 'quest', 'life', 'love', 'though', 'we', 'are', 'fades', 'a', 'seen'}\n", + "\n", + "Common words (alphabetical order):\n", + "['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], + "source": [ + "translator = str.maketrans('', '', string.punctuation)\n", + "\n", + "set1 = set(poem.lower().translate(translator).split())\n", + "set2 = set(new_poem.lower().translate(translator).split())\n", + "\n", + "print(\"Unique words in poem:\", len(set1))\n", + "print(\"Unique words in new_poem:\", len(set2))\n", + "\n", + "print(\"\\nWords only in poem:\")\n", + "print(set1 - set2)\n", + "\n", + "print(\"\\nWords only in new_poem:\")\n", + "print(set2 - set1)\n", + "\n", + "common_words = sorted(set1 & set2)\n", + "\n", + "print(\"\\nCommon words (alphabetical order):\")\n", + "print(common_words)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise 4: Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider the following dictionary of students with their scores in different subjects. One of the students, Bob, has complained about his score in Philosophy and, after reviewing it, the teacher has decided to update his score to 100. Write a Python program that updates Bob's score in Philosophy to 100 in the dictionary." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources: [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp)*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "grades = {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "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": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[\"Bob\"][\"Philosophy\"] = 100\n", + "grades " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bonus" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Below are the two lists. Write a Python program to convert them into a dictionary in a way that item from list1 is the key and item from list2 is the value." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Hint: Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it. Afterwards, you can use a function that turns a tuple into a dictionary.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources: [Python Zip Function](https://www.w3schools.com/python/ref_func_zip.asp)*\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "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", + "grades = dict(zip(keys, values))\n", + "\n", + "print(grades)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Get the subject with the minimum score from the previous dictionary." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Hint: Use the built-in function min(). Read about the parameter key.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "*Recommended External Resources:*\n", + "- *[Python Min Function Official Documentation](https://docs.python.org/3.8/library/functions.html#min)*\n", + "- *[How to use key function in max and min in Python](https://medium.com/analytics-vidhya/how-to-use-key-function-in-max-and-min-in-python-1fdbd661c59c)*" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Subject with the minimum score: Chemistry\n", + "Minimum score: 60\n" + ] + } + ], + "source": [ + "min_subject = min(grades, key=grades.get)\n", + "\n", + "print(\"Subject with the minimum score:\", min_subject)\n", + "print(\"Minimum score:\", grades[min_subject])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From 6abe8a2151dc064e2d8279ee4fdc363f98ae201c Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 11 Jul 2026 16:43:05 +0100 Subject: [PATCH 2/2] done