diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..610bfdd 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,127 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student number 1\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for the student: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student number 2\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for the student: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student number 3\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for the student: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student number 4\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for the student: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student number 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for the student: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average of grades: 5.8\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "add_grade = 0\n", + "students_grades = []\n", + "for i in range(1,6):\n", + " print(f\"Student number {i}\")\n", + " stgrades= int(input(\"Enter the grade for the student: \"))\n", + " students_grades.append(stgrades)\n", + "for grade in students_grades:\n", + " add_grade += grade\n", + "\n", + "average_grades = add_grade/len(students_grades)\n", + "print(f\"The average of grades: {average_grades}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List: [4, 5, 5]\n", + "Lenght: 3\n", + "Occurrence of 5: 2\n" + ] + } + ], + "source": [ + "# Your code here\n", + "new_list = []\n", + "for grade in students_grades[0:6:2]:\n", + " new_list.append(grade)\n", + " \n", + "print(f\"List: {sorted(new_list)}\")\n", + "print(f\"Lenght: {len(new_list)}\")\n", + "\n", + "print(f\"Occurrence of 5: {new_list.count(5)}\")\n" ] }, { @@ -95,11 +211,123 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "banana\n" + ] + } + ], + "source": [ + "# Your code here\n", + "# 5 types of fruit in a tuple\n", + "fruits = (\"apple\",\"mango\",\"orange\",\"peach\",\"banana\")\n", + "\n", + "#first and last element\n", + "print(fruits[0])\n", + "print(fruits[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[15], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Updated fruit\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# Tuples are immutables, once you create a tuple their elements cannot be modified\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m fruits[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;241m=\u001b[39m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwatermelon\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "# Updated fruit\n", + "# Tuples are immutables, once you create a tuple their elements cannot be modified\n", + "fruits[1] = (\"watermelon\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'mango', 'orange', 'peach', 'banana', 'watermelon', 'blueberry')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Concatenate a new tuple\n", + "fruits2 = (\"watermelon\", \"blueberry\")\n", + "fruits_updated = fruits + fruits2\n", + "fruits_updated" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'mango', 'orange')\n", + "('peach', 'banana', 'watermelon')\n" + ] + } + ], + "source": [ + "# Separete in two tuples\n", + "tuple1_, tuple2_ = tuple(zip(*[iter(fruits_updated)]*3))\n", + "print(tuple1_)\n", + "print(tuple2_)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'mango', 'orange', 'peach', 'banana', 'watermelon', 'apple', 'mango', 'orange', 'peach', 'banana', 'watermelon', 'blueberry')\n" + ] + }, + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "# Separete in two tuples\n", + "tuple3_ = tuple1_ + tuple2_ + fruits_updated\n", + "print(tuple3_)\n", + "len(tuple3_)" ] }, { @@ -136,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +391,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'great', 'desire', 'tasted', 'for', 'will', 'ice', 'fire', 'hate', 'in', 'also', 'favor', 'i’ve', 'world', 'the', 'twice', 'perish', 'would', 'destruction', 'hold', 'suffice'}\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "# Create one set for each poem\n", + "set_poem1 = set(poem.replace(\".\", \" \").replace(\",\", \" \").lower().split())\n", + "set_poem2 = set(new_poem.replace(\".\", \" \").replace(\",\", \" \").lower().split())\n", + "\n", + "# Unique words present in poem1 but not in poem2\n", + "print(set_poem1.difference(set_poem2))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'though', 'dream', 'a', 'deem', 'today', \"it's\", 'we', 'made', 'love', 'still', 'see', 'away', 'seen', 'quest', 'as', 'side', 'test', \"i've\", 'life', 'are', 'fades'}\n" + ] + } + ], + "source": [ + "# Unique words present in poem2 but not in poem1\n", + "print(set_poem2.difference(set_poem1))" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], + "source": [ + "# Unique words present in poem2 and in poem1 printed in alphabetical order\n", + "print(sorted((set_poem2.intersection(set_poem1))))" ] }, { @@ -193,7 +471,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +480,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "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": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "grades[\"Bob\"][\"Philosophy\"] = 100\n", + "grades" ] }, { @@ -239,14 +531,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "# Your code here\n", + "\n", + "new_dict = dict(zip(keys, values))\n", + "new_dict" ] }, { @@ -275,19 +581,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print(min(new_dict.values()))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -299,7 +621,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,