diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..8f55ee0 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -61,7 +61,22 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "grades = []\n", + "for i in range(5):\n", + " grades.append(int(input(f\"Grade of student number {i}\")))\n", + "sum_grades = 0\n", + "print (grades)\n", + "for grade in grades:\n", + " sum_grades+= grade\n", + "print(f\"The sum of the grades is: {sum_grades}\")\n", + "new_list = grades[0:5:2]\n", + "# for i in range(len(grades)):\n", + "# if (i % 2 == 0):\n", + "# new_list.append(grades[i])\n", + "new_list.sort()\n", + "print(f\"New list: {new_list}\")\n", + "print(f\"Length of the new list: {len(new_list)}\")\n", + "print(f\"5 appears {grades.count(5)} times\")" ] }, { @@ -95,11 +110,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "grape\n", + "Updated fruits: ('apple', 'strawberry', 'cherry', 'orange', 'grape')\n", + "Added fruits: ('apple', 'strawberry', 'cherry', 'orange', 'grape', 'cherry', 'elderberry')\n", + "subtuple: ('grape', 'cherry', 'elderberry')\n", + "Last tuple: ('apple', 'strawberry', 'cherry', 'orange', 'grape', 'cherry', 'elderberry', 'apple', 'banana', 'cherry', 'orange', 'grape')\n", + "Length last tuple: 12\n" + ] + } + ], "source": [ - "# Your code here" + "fruits = (\"apple\", \"banana\", \"cherry\", \"orange\", \"grape\")\n", + "print(fruits[0])\n", + "print(fruits[len(fruits) - 1])\n", + "\n", + "updatable_fruits = list((fruits))\n", + "updatable_fruits[1] = \"strawberry\"\n", + "new_fruits = tuple((updatable_fruits))\n", + "print(f\"Updated fruits: {new_fruits}\")\n", + "add_fruits = (\"cherry\", \"elderberry\")\n", + "added_fruits = new_fruits + add_fruits\n", + "print(f\"Added fruits: {added_fruits}\")\n", + "\n", + "sub_tuple1 = added_fruits[0:4]\n", + "sub_tuple2 = added_fruits[4:len(added_fruits)]\n", + "print(f\"subtuple: {sub_tuple2}\")\n", + "\n", + "last_tuple = sub_tuple1 + sub_tuple2 + fruits\n", + "print(f\"Last tuple: {last_tuple}\")\n", + "print(f\"Length last tuple: {len(last_tuple)}\")\n" ] }, { @@ -158,16 +205,23 @@ "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "It's still what we are made of.\"\"\"\n", + "\n", + "poem = poem.replace(\".\", \" \")\n", + "poem = poem.replace(\",\", \" \")\n", + "new_poem = new_poem.replace(\".\", \" \")\n", + "new_poem = new_poem.replace(\",\", \" \")\n", + "poem_set1 = set((poem.split()))\n", + "poem_set2 = set((new_poem.split()))\n", + "print(f\"Length of the first set: {len(poem_set1)}\")\n", + "print(f\"Length of the second set: {len(poem_set2)}\")\n", + "print(\"Unique in the first that are not in the second\")\n", + "print(poem_set1.difference(poem_set2))\n", + "print(\"Unique in the second that are not in the first\")\n", + "print(poem_set2.difference(poem_set1))\n", + "poem_set3 = poem_set1.union(poem_set2)\n", + "print(\"Ordered\")\n", + "print(sorted(poem_set3))" ] }, { @@ -191,22 +245,26 @@ "*Recommended External Resources: [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp)*\n" ] }, - { - "cell_type": "code", - "execution_count": 51, - "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": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n", + "100\n" + ] + } + ], "source": [ - "# Your code here" + "grades = {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}\n", + "\n", + "print(grades['Bob']['Philosophy'])\n", + "grades['Bob']['Philosophy'] = 100\n", + "print(grades['Bob']['Philosophy'])" ] }, { @@ -246,7 +304,10 @@ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "result = zip(keys, values)\n", + "result = dict((result))\n", + "\n", + "print(result)\n" ] }, { @@ -275,17 +336,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Chemistry'" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "min(result)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -299,7 +371,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,