From 6f468fad8e4cb3e325ee907be2519b6bf5464c06 Mon Sep 17 00:00:00 2001 From: shirleyamponsah5-maker Date: Thu, 21 May 2026 15:53:45 +0200 Subject: [PATCH] Add files via upload --- lab-python-data-structures-extra.ipynb | 409 +++++++++++++++++++++++++ 1 file changed, 409 insertions(+) create mode 100644 lab-python-data-structures-extra.ipynb diff --git a/lab-python-data-structures-extra.ipynb b/lab-python-data-structures-extra.ipynb new file mode 100644 index 0000000..e145972 --- /dev/null +++ b/lab-python-data-structures-extra.ipynb @@ -0,0 +1,409 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "dcf8d4f9-f31f-4efb-8a5b-8371683c24cd", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter grade for student 1: 80\n", + "Enter grade for student 2: 65\n", + "Enter grade for student 3: 90\n", + "Enter grade for student 4: 99\n", + "Enter grade for student 5: 52\n" + ] + } + ], + "source": [ + "grades = []\n", + "for i in range(5):\n", + " grade = int(input(f\"Enter grade for student {i+1}: \"))\n", + " grades.append(grade)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "4b97d89c-0afa-4423-97bc-946ac0439f4b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total sum of grades: 386\n" + ] + } + ], + "source": [ + "total_sum = sum(grades)\n", + "print(\"\\nTotal sum of grades:\", total_sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3c6632aa-fe1d-4832-97be-a89fa14fdc9e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Selected grades (sorted): [52, 80, 90]\n", + "Length of selected list: 3\n", + "Number of occurrences of score 5: 0\n" + ] + } + ], + "source": [ + "selected = grades[0:5:2]\n", + "selected.sort()\n", + "print(\"Selected grades (sorted):\", selected)\n", + "print(\"Length of selected list:\", len(selected))\n", + "print(\"Number of occurrences of score 5:\", selected.count(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e3a78b0e-3be1-4e63-9f69-bf07933ec478", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original tuple: ('apple', 'banana', 'orange', 'mango', 'grape')\n" + ] + } + ], + "source": [ + "fruits = (\"apple\", \"banana\", \"orange\", \"mango\", \"grape\")\n", + "print(\"Original tuple:\", fruits)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "33d0b551-71fb-44a3-94ed-ee67dd89b279", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First fruit: apple\n", + "Last fruit: grape\n" + ] + } + ], + "source": [ + "print(\"First fruit:\", fruits[0])\n", + "print(\"Last fruit:\", fruits[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "dccfad14-7036-443e-82f7-8c80ee5bba45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated tuple: ('apple', 'strawberry', 'orange', 'mango', 'grape')\n" + ] + } + ], + "source": [ + "new_fruits = (\"apple\", \"strawberry\") + fruits[2:]\n", + "print(\"Updated tuple:\", new_fruits)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "c6438126-a738-46e2-8474-37b84ab27956", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "After adding more fruits: ('apple', 'strawberry', 'orange', 'mango', 'grape', 'kiwi', 'pineapple')\n" + ] + } + ], + "source": [ + "extra = (\"kiwi\", \"pineapple\")\n", + "combined = new_fruits + extra\n", + "print(\"After adding more fruits:\", combined)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c81ff96a-5993-40d0-a1a9-8272b0b138a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First split tuple: ('apple', 'strawberry', 'orange')\n", + "Second split tuple: ('mango', 'grape', 'kiwi')\n" + ] + } + ], + "source": [ + "tuple1 = combined[:3]\n", + "tuple2 = combined[3:6]\n", + "print(\"First split tuple:\", tuple1)\n", + "print(\"Second split tuple:\", tuple2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6fe9a545-32f6-4e81-be6a-112f4fce71f5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Final combined tuple: ('apple', 'strawberry', 'orange', 'mango', 'grape', 'kiwi', 'apple', 'banana', 'orange', 'mango', 'grape')\n", + "Length of final tuple: 11\n" + ] + } + ], + "source": [ + "final_tuple = tuple1 + tuple2 + fruits\n", + "print(\"Final combined tuple:\", final_tuple)\n", + "print(\"Length of final tuple:\", len(final_tuple))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "a135b87a-ebaa-409d-b44b-da6cf453938b", + "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": 22, + "id": "b0bf7dd6-2408-45d8-baa2-e11eba273f6b", + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "def clean_text(text):\n", + " text = text.lower()\n", + " text = text.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + " return text.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "feb8e103-ad6e-4b61-bb19-555bf7d53d65", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique words in poem 1: 41\n", + "Unique words in poem 2: 42\n" + ] + } + ], + "source": [ + "set1 = set(clean_text(poem))\n", + "set2 = set(clean_text(new_poem))\n", + "print(\"Unique words in poem 1:\", len(set1))\n", + "print(\"Unique words in poem 2:\", len(set2))" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "03a9d70c-6833-4518-940d-95adae5c8904", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Words only in poem 1: {'desire', 'for', 'hold', 'world', 'i’ve', 'hate', 'twice', 'fire', 'perish', 'favor', 'the', 'destruction', 'also', 'would', 'suffice', 'tasted', 'in', 'ice', 'great', 'will'}\n" + ] + } + ], + "source": [ + "unique_to_poem1 = set1 - set2\n", + "print(\"\\nWords only in poem 1:\", unique_to_poem1)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "8ac0114e-a478-43da-98b3-28929f803469", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Words only in poem 2: {'fades', 'away', 'still', 'test', 'dream', 'life', 'we', 'today', 'made', 'quest', 'seen', 'deem', 'see', 'its', 'ive', 'a', 'are', 'though', 'love', 'side', 'as'}\n" + ] + } + ], + "source": [ + "unique_to_poem2 = set2 - set1\n", + "print(\"\\nWords only in poem 2:\", unique_to_poem2)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "44df96df-f2a4-4d77-992b-45e349986665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Words in both poems (alphabetical): ['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], + "source": [ + "common_words = sorted(set1 & set2)\n", + "print(\"\\nWords in both poems (alphabetical):\", common_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "24b60269-9149-438f-8c23-4b0757ba6087", + "metadata": {}, + "outputs": [], + "source": [ + "grades = {\n", + " 'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "92e06c53-7636-4014-a2ca-ddc03b238376", + "metadata": {}, + "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": [ + "grades['Bob']['Philosophy'] = 100\n", + "print(grades)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "c13b035f-a26b-40b0-93e8-97f577ddfb27", + "metadata": {}, + "outputs": [], + "source": [ + "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", + "values = [75, 85, 60, 90]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "43ab7db5-f955-4f4d-8ac6-57d52b520b5d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], + "source": [ + "subject_scores = dict(zip(keys, values))\n", + "print(subject_scores)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61423523-b429-41a0-aae7-09f9e4b4fcd3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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": 5 +}