From 4113532b23796cbb8ccafc686c380bd924612ffe Mon Sep 17 00:00:00 2001 From: Alessia Mattera Date: Thu, 15 Jan 2026 21:32:03 +0100 Subject: [PATCH 1/2] week1Lab --- lab-python-data-structures.ipynb | 234 ++++++++++++++++++++++++++++++- 1 file changed, 232 insertions(+), 2 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..50ae9822 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,241 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #1" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "products = ['t-shirt','mug', 'hat','book','keychain']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Exercise #2" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = { }" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: ''", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[58], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m inventory \u001b[38;5;241m=\u001b[39m { }\n\u001b[1;32m 2\u001b[0m Key1 \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124menter a product\u001b[39m\u001b[38;5;124m'\u001b[39m)]\n\u001b[0;32m----> 3\u001b[0m inventory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt-shirt\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124menter a value\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m 4\u001b[0m Key2 \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124menter a product\u001b[39m\u001b[38;5;124m'\u001b[39m)]\n\u001b[1;32m 5\u001b[0m inventory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmug\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124menter a value\u001b[39m\u001b[38;5;124m\"\u001b[39m)) \n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ], + "source": [ + "inventory = { }\n", + "Key1 = [input('enter a product')]\n", + "inventory['t-shirt'] =int(input(\"enter a value\"))\n", + "Key2 = [input('enter a product')]\n", + "inventory['mug'] =int(input(\"enter a value\")) \n", + "Key3 = [input('enter a product')]\n", + "inventory['hat'] =int(input(\"enter a value\"))\n", + "Key4 = [input('enter a product')]\n", + "inventory['book'] =int(input(\"enter a value\"))\n", + "Key5 = [input('enter a product')]\n", + "inventory['keychain'] =int(input(\"enter a value\"))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mtype\u001b[39m(inventory)\n", + "\u001b[0;31mNameError\u001b[0m: name 'inventory' is not defined" + ] + } + ], + "source": [ + "type(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Exercise #3" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: ''", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[53], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m inventory[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmug\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter the available quantity for mug: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ], + "source": [ + "inventory[\"mug\"] =int(input(\"Enter the available quantity for mug: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "mug = 10\n", + "tshirt = 25\n", + "hat =15\n", + "book = 8\n", + "keychain = 50\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "inventory[mug]=10\n", + "inventory[tshirt]=25\n", + "inventory[hat]=15\n", + "inventory[book]=8\n", + "inventory[keychain]=50" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #4" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "customer_order = []" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #5" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "product = input(\"name three products the customer wants to order: \")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #7" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #8" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #9" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #10" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From ce67bb39112ea60f4c8a015bcc7e3309c37dea3a Mon Sep 17 00:00:00 2001 From: Alessia Mattera Date: Sat, 17 Jan 2026 15:35:04 +0100 Subject: [PATCH 2/2] Lab2 Flow Control --- lab-python-data-structures.ipynb | 166 ++++++++++++++++++++++++++++--- 1 file changed, 154 insertions(+), 12 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 50ae9822..e0c5abc8 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -36,13 +36,12 @@ " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", " \n", " Store these statistics in a tuple called `order_status`.\n", + "inventory = {\n", + " \"apple\": 10,\n", + " \"banana\": 20,\n", + " \"orange\": 15\n", + " \n", "\n", - "8. Print the order statistics using the following format:\n", - " ```\n", - " Order Statistics:\n", - " Total Products Ordered: \n", - " Percentage of Products Ordered: % \n", - " ```\n", "\n", "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", "\n", @@ -171,15 +170,32 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m book \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m8\u001b[39m\n\u001b[1;32m 5\u001b[0m keychain \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m50\u001b[39m\n\u001b[0;32m----> 6\u001b[0m inventory\u001b[38;5;241m.\u001b[39mappend(mug)\n\u001b[1;32m 7\u001b[0m inventory\u001b[38;5;241m.\u001b[39mappend(shirt)\n\u001b[1;32m 8\u001b[0m inventory\u001b[38;5;241m.\u001b[39mappend(hat)\n", + "\u001b[0;31mNameError\u001b[0m: name 'inventory' is not defined" + ] + } + ], "source": [ "mug = 10\n", "tshirt = 25\n", "hat =15\n", "book = 8\n", - "keychain = 50\n" + "keychain = 50\n", + "inventory.append(mug)\n", + "inventory.append(shirt)\n", + "inventory.append(hat)\n", + "inventory.append(book)\n", + "inventory.append(keychain)" ] }, { @@ -224,11 +240,20 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (2961256186.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[3], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m product = input(\"name three products the customer wants to order from the inventory separated by commas:\") \")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" + ] + } + ], "source": [ - "product = input(\"name three products the customer wants to order: \")" + "product = input(\"name three products the customer wants to order from the inventory separated by commas:\") \")" ] }, { @@ -245,6 +270,27 @@ "Excercise #6" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'product' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(product)\n", + "\u001b[0;31mNameError\u001b[0m: name 'product' is not defined" + ] + } + ], + "source": [ + "print(product)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -254,6 +300,38 @@ "Excercise #7" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order status: (2, 4.444444444444445)\n" + ] + } + ], + "source": [ + "inventory = {\n", + " \"apple\": 10,\n", + " \"banana\": 20,\n", + " \"orange\": 15\n", + "}\n", + "\n", + "customer_orders = {\"apple\", \"banana\", \"apple\"}\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "available_products = sum(inventory.values())\n", + "\n", + "percentage_ordered = (total_products_ordered / available_products) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "print(\"Order status:\", order_status)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -263,6 +341,43 @@ "Excercise #8" ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 4.44%\n" + ] + } + ], + "source": [ + "inventory = {\n", + " \"apple\": 10,\n", + " \"banana\": 20,\n", + " \"orange\": 15\n", + "}\n", + "\n", + "customer_orders = {\"apple\", \"banana\", \"apple\"}\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "available_products = sum(inventory.values())\n", + "\n", + "percentage_ordered = (total_products_ordered / available_products) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")" + ] + }, { "cell_type": "code", "execution_count": null, @@ -272,6 +387,33 @@ "Excercise #9" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: {'apple': 9, 'banana': 19, 'orange': 14}\n" + ] + } + ], + "source": [ + "inventory = {\n", + " \"apple\": 10,\n", + " \"banana\": 20,\n", + " \"orange\": 15\n", + "}\n", + "\n", + "# Subtract 1 from the quantity of each product\n", + "for product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"Updated inventory:\", inventory)" + ] + }, { "cell_type": "code", "execution_count": null,