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
181 changes: 165 additions & 16 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total sum of grades: 30.0\n",
"Summary of selected grades: [4.0, 5.0, 9.0]\n",
"Number of selected students: 3\n",
"Number of students who got a 5: 1\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"grade1 = float(input(\"Grade student 1: \"))\n",
"grade2 = float(input(\"Grade student 2: \"))\n",
"grade3 = float(input(\"Grade student 3: \"))\n",
"grade4 = float(input(\"Grade student 4: \"))\n",
"grade5 = float(input(\"Grade student 5: \"))\n",
"\n",
"grades = [grade1, grade2, grade3, grade4, grade5]\n",
"\n",
"total_sum = sum(grades)\n",
"print(f\"Total sum of grades: {total_sum}\")\n",
"\n",
"selected_grades = grades[0:5:2]\n",
"selected_grades.sort()\n",
"\n",
"print(f\"Summary of selected grades: {selected_grades}\")\n",
"print(f\"Number of selected students: {len(selected_grades)}\")\n",
"print(f\"Number of students who got a 5: {selected_grades.count(5)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -95,11 +130,88 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first fruits is: platan\n",
"The last fruit is: cirera\n",
"Fruits after adding mango: ('platan', 'poma', 'kiwi', 'llimona', 'taronger', 'pera', 'maduixa', 'albercoc', 'cirera')\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"fruits = (\"platan\", \"poma\", \"kiwi\", \"llimona\", \"taronger\", \"pera\", \"maduixa\", \"albercoc\", \"cirera\")\n",
"print(f\"The first fruits is: {fruits[0]}\")\n",
"print(f\"The last fruit is: {fruits[-1]}\")\n",
"\n",
"new_fruit = (\"mango\",)\n",
"fruits1= (fruits[0],) + new_fruit + fruits[2:]\n",
"print(f\"Fruits after adding mango: {fruits}\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fruits after adding meló and síndria: ('platan', 'poma', 'kiwi', 'llimona', 'taronger', 'pera', 'maduixa', 'albercoc', 'cirera', 'meló', 'síndria')\n"
]
}
],
"source": [
"additional_fruits = (\"meló\", \"síndria\")\n",
"fruits += additional_fruits\n",
"print(f\"Fruits after adding meló and síndria: {fruits}\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First three fruits: ('platan', 'poma', 'kiwi')\n",
"Last three fruits: ('cirera', 'meló', 'síndria')\n"
]
}
],
"source": [
"tuple1 = fruits[:3]\n",
"tuple2 = fruits[-3:]\n",
"print(f\"First three fruits: {tuple1}\")\n",
"print(f\"Last three fruits: {tuple2}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Final inventory of fruits: ('platan', 'poma', 'kiwi', 'cirera', 'meló', 'síndria', 'platan', 'poma', 'kiwi', 'llimona', 'taronger', 'pera', 'maduixa', 'albercoc', 'cirera', 'meló', 'síndria')\n",
"Total number of fruits in inventory: 17\n"
]
}
],
"source": [
"final_inventory = tuple1 + tuple2 + fruits\n",
"print(f\"Final inventory of fruits: {final_inventory}\")\n",
"print(f\"Total number of fruits in inventory: {len(final_inventory)}\")"
]
},
{
Expand Down Expand Up @@ -136,7 +248,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -163,11 +275,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unique words in the first poem: 42\n",
"Unique words in the second poem: 45\n",
"Words in the first poem but not in the second: {'great', 'suffice', 'favor', 'desire', 'perish', 'for', 'hate', 'world', 'tasted', 'twice', 'And', 'ice', 'also', 'hold', 'in', 'fire', 'the', 'I’ve', 'would', 'destruction', 'will', 'Is'}\n",
"Words in the second poem but not in the first: {'we', 'test', 'as', 'fades', 'life', 'see', 'still', 'seen', 'and', 'is', 'are', \"I've\", 'dream', 'though', 'today', 'love', \"It's\", 'but', 'made', 'side', 'deem', 'a', 'quest', 'away', \"it's\"}\n",
"Words common to both poems: {'To', 'think', 'that', 'But', 'Some', 'it', 'who', 'enough', 'to', 'had', 'I', 'know', 'with', 'those', 'of', 'end', 'say', 'if', 'what', 'From'}\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"cleaned_poem1 = poem.replace(\",\", \"\").replace(\".\", \"\").split()\n",
"cleaned_poem2 = new_poem.replace(\",\", \"\").replace(\".\", \"\").split()\n",
"set1 = set(cleaned_poem1)\n",
"set2 = set(cleaned_poem2)\n",
"print(f\"Unique words in the first poem: {len(set1)}\")\n",
"print(f\"Unique words in the second poem: {len(set2)}\")\n",
"\n",
"diff1 = set1.difference(set2)\n",
"print(f\"Words in the first poem but not in the second: {diff1}\")\n",
"\n",
"diff2 = set2.difference(set1)\n",
"print(f\"Words in the second poem but not in the first: {diff2}\")\n",
"\n",
"common_words = set1.intersection(set2)\n",
"print(f\"Words common to both poems: {common_words}\")"
]
},
{
Expand All @@ -193,7 +332,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +341,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob's updated grades: {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"grades['Bob']['Philosophy'] = 100\n",
"print(f\"Bob's updated grades: {grades['Bob']}\")"
]
},
{
Expand Down Expand Up @@ -285,7 +434,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.3"
}
},
"nbformat": 4,
Expand Down