From 8b1dabffe3619be2add4d44da965eb8280dc4286 Mon Sep 17 00:00:00 2001 From: DavyN25 Date: Sun, 23 Nov 2025 22:38:47 +0100 Subject: [PATCH] lab complete --- lab-python-data-structures.ipynb | 214 ++++++++++++++++++++++++++++++- 1 file changed, 211 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..b2d6116d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,221 @@ "\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": 18, + "metadata": {}, + "outputs": [], + "source": [ + "#1. Define a list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#2. Create an empty dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt available in the inventory: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mug available in the inventory: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hat available in the inventory: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'hat': 6}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of book available in the inventory: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'hat': 6, 'book': 1}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychain available in the inventory: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'hat': 6, 'book': 1, 'keychain': 8}\n" + ] + } + ], + "source": [ + "## 3. ask the users to input the quantity of each product available in the inventory\n", + "for i in products:\n", + " product_quantity = int(input(f\"Enter the quantity of {i} available in the inventory: \"))\n", + " inventory [i] = product_quantity\n", + " print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of product 1 to order: hat\n", + "Enter the name of product 2 to order: mug\n", + "Enter the name of product 3 to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "## 4. Create an empty set called customer_orders\n", + "customer_orders = set()\n", + "\n", + "#5. Ask the user to input the name of three products \n", + "\n", + "for i in range(3):\n", + " product_name = input(f\"Enter the name of product {i+1} to order: \")\n", + " customer_orders.add (product_name)\n", + "\n", + "#6. Display the customer orders\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "#7. Calculate the statistics\n", + "\n", + "# Calculate the total of the number of products ordered ( length of all)\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "\n", + "\n", + "# Calculate the total available products ( length of all)\n", + "total_available_products = len(products)\n", + "#print(f\"The total of the available products is: {total_available_products}\")\n", + "\n", + "# Calculate the percentage of the products ordered\n", + "percentage_ordered = (total_products_ordered / total_available_products)*100\n", + "\n", + "# store the statistics in a tuple\n", + "order_status = (total_products_ordered,percentage_ordered)\n", + "#print(f\"the order status is: {order_status}\")\n", + "\n", + "#8. Order Statistics:\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered} %\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The new inventory of t-shirt is 4\n", + "The new inventory of mug is 2\n", + "The new inventory of hat is 5\n", + "The new inventory of book is 0\n", + "The new inventory of keychain is 7\n" + ] + } + ], + "source": [ + "#9. Update the inventory by subtracting 1 from the quantity of each product. \n", + "# inventory base on point 3. of the exercice\n", + "inventory = {'t-shirt': 5, 'mug': 3, 'hat': 6, 'book': 1, 'keychain': 8}\n", + "for i in inventory : \n", + " inventory[i] -= 1 \n", + "\n", + "#10. Print the updated inventory\n", + "for i, product_quantity in inventory.items():\n", + " print(f\"The new inventory of {i} is {product_quantity}\")\n", + " \n" + ] + }, + { + "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": { @@ -68,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,