From 721e2e83120d2659e7b3553805722d4e93d8a2d5 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sat, 18 Oct 2025 09:54:40 +0200 Subject: [PATCH 1/8] git commit -m "Solved Lab" --- lab-python-data-structures.ipynb | 251 ++++++++++++++++++++++++++++++- 1 file changed, 243 insertions(+), 8 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..2215c9b8 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -15,17 +15,247 @@ "source": [ "## Exercise: Managing Customer Orders\n", "\n", - "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", "\n", - "Follow the steps below to complete the exercise:\n", "\n", - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - 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", "\n", - "2. Create an empty dictionary called `inventory`.\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", - "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", "\n", - "4. Create an empty set called `customer_orders`.\n", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1.defining the list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# 2. creating an empty dictionary invetory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "# 3. input of the user for quantity of each product\n", + "for i in range(len(products)):\n", + " quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n", + " inventory[products[i]] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 2, 'hat': 1, 'book': 3, 'keychain': 2}\n" + ] + } + ], + "source": [ + "# 3.5 checking our dictionary inventory\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# 4. creating an empty set customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the list of products:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Enter the items of the 3 products to order:\n", + "Invalid product. Please choose from the list.\n", + "Invalid product. Please choose from the list.\n", + "Invalid product. Please choose from the list.\n" + ] + } + ], + "source": [ + "# 5. user input for 3 products to order\n", + "print(\"Here is the list of products:\")\n", + "print(products)\n", + "\n", + "print(\"Enter the items of the 3 products to order:\")\n", + "\n", + "for i in range(3):\n", + " item = input(\"Enter a product name: \").lower()\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " else:\n", + " print(\"Invalid product. Please choose from the list.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The ordered products by the customer: {'t-shirt', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6. Printing the ordered products\n", + "print(\"The ordered products by the customer are:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total unique products ordered: 3\n", + "Percentage of products ordered: 60.0 %\n", + "Order Status (Total, Percentage): (3, 60.0)\n" + ] + } + ], + "source": [ + "# 7. Order statistics\n", + "\n", + "# total of products ordered\n", + "total_products_ordered = len(customer_orders)\n", + "print(\"Total unique products ordered:\", total_products_ordered)\n", + "\n", + "# percentage of products ordered\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", + "\n", + "# order status as a tuple\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "print(\"Order Status (Total, Percentage):\", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "# 8. Print order statistics\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", order_status[0])\n", + "print(\"Percentage of Products Ordered:\", order_status[1], \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 for each ordered product\n", + "for ordered_product in customer_orders:\n", + " if inventory[ordered_product] > 0:\n", + " inventory[ordered_product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 10. Print updated inventory\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "\n", "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", "\n", @@ -50,11 +280,16 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +303,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From fda6e69948fe9210b2eb57d9768c16acfac31f38 Mon Sep 17 00:00:00 2001 From: Samia Date: Sat, 18 Oct 2025 11:02:47 +0200 Subject: [PATCH 2/8] Delete lab-python-data-structures.ipynb --- lab-python-data-structures.ipynb | 311 ------------------------------- 1 file changed, 311 deletions(-) delete mode 100644 lab-python-data-structures.ipynb diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb deleted file mode 100644 index 2215c9b8..00000000 --- a/lab-python-data-structures.ipynb +++ /dev/null @@ -1,311 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "tags": [] - }, - "source": [ - "# Lab | Data Structures " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise: Managing Customer Orders\n", - "\n", - "\n", - "\n", - "7. Calculate the following order statistics:\n", - " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", - " - 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", - "\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", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\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": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" - ] - } - ], - "source": [ - "# 1.defining the list products\n", - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "print(products)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] - } - ], - "source": [ - "# 2. creating an empty dictionary invetory\n", - "inventory = {}\n", - "print(inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "# 3. input of the user for quantity of each product\n", - "for i in range(len(products)):\n", - " quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n", - " inventory[products[i]] = quantity" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 2, 'hat': 1, 'book': 3, 'keychain': 2}\n" - ] - } - ], - "source": [ - "# 3.5 checking our dictionary inventory\n", - "print(inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set()\n" - ] - } - ], - "source": [ - "# 4. creating an empty set customer orders\n", - "customer_orders = set()\n", - "print(customer_orders)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Here is the list of products:\n", - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "Enter the items of the 3 products to order:\n", - "Invalid product. Please choose from the list.\n", - "Invalid product. Please choose from the list.\n", - "Invalid product. Please choose from the list.\n" - ] - } - ], - "source": [ - "# 5. user input for 3 products to order\n", - "print(\"Here is the list of products:\")\n", - "print(products)\n", - "\n", - "print(\"Enter the items of the 3 products to order:\")\n", - "\n", - "for i in range(3):\n", - " item = input(\"Enter a product name: \").lower()\n", - " if item in products:\n", - " customer_orders.add(item)\n", - " else:\n", - " print(\"Invalid product. Please choose from the list.\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The ordered products by the customer: {'t-shirt', 'mug', 'hat'}\n" - ] - } - ], - "source": [ - "# 6. Printing the ordered products\n", - "print(\"The ordered products by the customer are:\", customer_orders)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Total unique products ordered: 3\n", - "Percentage of products ordered: 60.0 %\n", - "Order Status (Total, Percentage): (3, 60.0)\n" - ] - } - ], - "source": [ - "# 7. Order statistics\n", - "\n", - "# total of products ordered\n", - "total_products_ordered = len(customer_orders)\n", - "print(\"Total unique products ordered:\", total_products_ordered)\n", - "\n", - "# percentage of products ordered\n", - "percentage_ordered = (total_products_ordered / len(products)) * 100\n", - "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", - "\n", - "# order status as a tuple\n", - "order_status = (total_products_ordered, percentage_ordered)\n", - "print(\"Order Status (Total, Percentage):\", order_status)" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Order Statistics:\n", - "Total Products Ordered: 3\n", - "Percentage of Products Ordered: 60.0 %\n" - ] - } - ], - "source": [ - "# 8. Print order statistics\n", - "print(\"Order Statistics:\")\n", - "print(\"Total Products Ordered:\", order_status[0])\n", - "print(\"Percentage of Products Ordered:\", order_status[1], \"%\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# 9. Update the inventory by subtracting 1 for each ordered product\n", - "for ordered_product in customer_orders:\n", - " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] -= 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# 10. Print updated inventory\n", - "print(\"\\nUpdated Inventory:\")\n", - "for product, qty in inventory.items():\n", - " print(f\"{product}: {qty}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", - "\n", - "6. Print the products in the `customer_orders` set.\n", - "\n", - "7. Calculate the following order statistics:\n", - " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", - " - 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", - "\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", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\n", - "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "language": "python", - "name": "python3" - }, - "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.5" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} From 66c45f9fc5e03a1a7848fd916a1b6e873f41b705 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sat, 18 Oct 2025 11:08:55 +0200 Subject: [PATCH 3/8] Update lab-python-data-structures.ipynb --- lab-python-data-structures.ipynb | 149 ++++++++----------------------- 1 file changed, 37 insertions(+), 112 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 2215c9b8..b2745fdc 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -6,40 +6,12 @@ "tags": [] }, "source": [ - "# Lab | Data Structures " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise: Managing Customer Orders\n", - "\n", - "\n", - "\n", - "7. Calculate the following order statistics:\n", - " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", - " - 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", - "\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", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\n", - "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + "# Lab | Data Structures | Samia" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -58,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -77,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -89,14 +61,14 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 5, 'mug': 2, 'hat': 1, 'book': 3, 'keychain': 2}\n" + "{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}\n" ] } ], @@ -107,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -126,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -136,9 +108,9 @@ "Here is the list of products:\n", "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", "Enter the items of the 3 products to order:\n", - "Invalid product. Please choose from the list.\n", - "Invalid product. Please choose from the list.\n", - "Invalid product. Please choose from the list.\n" + "mug\n", + "book\n", + "hat\n" ] } ], @@ -151,6 +123,7 @@ "\n", "for i in range(3):\n", " item = input(\"Enter a product name: \").lower()\n", + " print(item)\n", " if item in products:\n", " customer_orders.add(item)\n", " else:\n", @@ -159,40 +132,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The ordered products by the customer: {'t-shirt', 'mug', 'hat'}\n" + "Customer Orders: {'hat', 'book', 'mug'}\n" ] } ], "source": [ "# 6. Printing the ordered products\n", - "print(\"The ordered products by the customer are:\", customer_orders)" + "print(\"Customer Orders:\", customer_orders)" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Order Statistics:\n", "Total unique products ordered: 3\n", - "Percentage of products ordered: 60.0 %\n", - "Order Status (Total, Percentage): (3, 60.0)\n" + "Percentage of products ordered: 60.0 %\n" ] } ], "source": [ - "# 7. Order statistics\n", - "\n", + "# 7+8. Calculating order statistics and Printing order statistics\n", + "print(\"Order Statistics:\")\n", "# total of products ordered\n", "total_products_ordered = len(customer_orders)\n", "print(\"Total unique products ordered:\", total_products_ordered)\n", @@ -201,90 +174,42 @@ "percentage_ordered = (total_products_ordered / len(products)) * 100\n", "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", "\n", - "# order status as a tuple\n", + "# Storing order status\n", "order_status = (total_products_ordered, percentage_ordered)\n", - "print(\"Order Status (Total, Percentage):\", order_status)" + "\n", + "# Printing order status\n", + "#print(\"Order Status (Total, Percentage):\", order_status)" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Order Statistics:\n", - "Total Products Ordered: 3\n", - "Percentage of Products Ordered: 60.0 %\n" + "Updated Inventory:\n", + "t-shirt : 5\n", + "mug : 9\n", + "hat : 14\n", + "book : 19\n", + "keychain : 25\n" ] } ], "source": [ - "# 8. Print order statistics\n", - "print(\"Order Statistics:\")\n", - "print(\"Total Products Ordered:\", order_status[0])\n", - "print(\"Percentage of Products Ordered:\", order_status[1], \"%\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# 9. Update the inventory by subtracting 1 for each ordered product\n", + "# 9. Updating the inventory by subtracting 1 for each ordered product\n", "for ordered_product in customer_orders:\n", " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] -= 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# 10. Print updated inventory\n", - "print(\"\\nUpdated Inventory:\")\n", - "for product, qty in inventory.items():\n", - " print(f\"{product}: {qty}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", - "\n", - "6. Print the products in the `customer_orders` set.\n", - "\n", - "7. Calculate the following order statistics:\n", - " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", - " - 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", - "\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", + " inventory[ordered_product] = inventory[ordered_product] - 1\n", "\n", - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", - "\n", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\n", - "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + "# 10. Printing the updated inventory\n", + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] } ], "metadata": { From f81939d160ba252664de848dd6e9773d9a89e82e Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Tue, 4 Nov 2025 00:33:32 +0100 Subject: [PATCH 4/8] commit --- lab-python-data-structures.ipynb | 256 ++++++++++++++++++++++++------- 1 file changed, 201 insertions(+), 55 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index b2745fdc..b079848a 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -6,12 +6,70 @@ "tags": [] }, "source": [ - "# Lab | Data Structures | Samia" + "# Lab 01 | Data Structures\n", + "----" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders\n", + "\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "2. Create an empty dictionary called `inventory`.\n", + "\n", + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "\n", + "4. Create an empty set called `customer_orders`.\n", + "\n", + "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", + "\n", + "6. Print the products in the `customer_orders` set.\n", + "\n", + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - 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", + "\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", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SOLUTION\n", + "----" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -28,9 +86,16 @@ "print(products)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Create an empty dictionary called `inventory`." + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 36, "metadata": {}, "outputs": [ { @@ -48,38 +113,51 @@ ] }, { - "cell_type": "code", - "execution_count": 4, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# 3. input of the user for quantity of each product\n", - "for i in range(len(products)):\n", - " quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n", - " inventory[products[i]] = quantity" + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}\n" + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" ] } ], "source": [ + "# 3. input of the user for quantity of each product\n", + "for i in range(len(products)):\n", + " while True: # keep asking until valid\n", + " quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n", + " if quantity.isdigit(): # checks if input is a positive integer\n", + " quantity = int(quantity)\n", + " inventory[products[i]] = quantity\n", + " break # valid → exit loop\n", + " else:\n", + " print(f\"❌ Invalid input! Please enter a numeric quantity for {products[i]}.\")\n", + "\n", "# 3.5 checking our dictionary inventory\n", "print(inventory)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Create an empty set called `customer_orders`." + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -96,9 +174,16 @@ "print(customer_orders)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set." + ] + }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -107,39 +192,47 @@ "text": [ "Here is the list of products:\n", "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "Enter the items of the 3 products to order:\n", - "mug\n", - "book\n", - "hat\n" + "Enter the names of 3 products to order:\n", + "✅ Added 'mug' to customer orders.\n", + "✅ Added 'book' to customer orders.\n", + "✅ Added 'hat' to customer orders.\n" ] } ], "source": [ - "# 5. user input for 3 products to order\n", + "# 5. User input for 3 products to order\n", "print(\"Here is the list of products:\")\n", "print(products)\n", "\n", - "print(\"Enter the items of the 3 products to order:\")\n", + "print(\"Enter the names of 3 products to order:\")\n", "\n", - "for i in range(3):\n", + "while len(customer_orders) < 3:\n", " item = input(\"Enter a product name: \").lower()\n", - " print(item)\n", + "\n", " if item in products:\n", " customer_orders.add(item)\n", + " print(f\"✅ Added '{item}' to customer orders.\")\n", " else:\n", - " print(\"Invalid product. Please choose from the list.\")" + " print(\"❌ Invalid product! Please choose from:\", products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6. Print the products in the `customer_orders` set." ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Customer Orders: {'hat', 'book', 'mug'}\n" + "Customer Orders: {'book', 'mug', 'hat'}\n" ] } ], @@ -148,9 +241,52 @@ "print(\"Customer Orders:\", customer_orders)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - 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`." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Calculating order statistics\n", + "\n", + "# Total Products Ordered\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "# Percentage of Products Ordered\n", + "percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Calculate Order Status\n", + "order_status = (total_products_ordered, percentage_products_ordered)\n", + "# Print Order Status\n", + "#print(\"Order Status (Total, Percentage):\", order_status)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```" + ] + }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -158,55 +294,65 @@ "output_type": "stream", "text": [ "Order Statistics:\n", - "Total unique products ordered: 3\n", - "Percentage of products ordered: 60.0 %\n" + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60%\n" ] } ], "source": [ - "# 7+8. Calculating order statistics and Printing order statistics\n", + "# 8. Storing the statistics in a tuple order_status\n", "print(\"Order Statistics:\")\n", - "# total of products ordered\n", - "total_products_ordered = len(customer_orders)\n", - "print(\"Total unique products ordered:\", total_products_ordered)\n", - "\n", - "# percentage of products ordered\n", - "percentage_ordered = (total_products_ordered / len(products)) * 100\n", - "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", - "\n", - "# Storing order status\n", - "order_status = (total_products_ordered, percentage_ordered)\n", - "\n", - "# Printing order status\n", - "#print(\"Order Status (Total, Percentage):\", order_status)" + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Updating the inventory by subtracting 1 for each ordered product\n", + "for ordered_product in customer_orders:\n", + " if inventory[ordered_product] > 0:\n", + " inventory[ordered_product] = inventory[ordered_product] - 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Updated Inventory:\n", - "t-shirt : 5\n", - "mug : 9\n", - "hat : 14\n", - "book : 19\n", - "keychain : 25\n" + "Updated Inventory :\n", + "t-shirt : 10\n", + "mug : 17\n", + "hat : 27\n", + "book : 37\n", + "keychain : 50\n" ] } ], "source": [ - "# 9. Updating the inventory by subtracting 1 for each ordered product\n", - "for ordered_product in customer_orders:\n", - " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] = inventory[ordered_product] - 1\n", - "\n", "# 10. Printing the updated inventory\n", - "print(\"Updated Inventory:\")\n", + "print(\"Updated Inventory :\")\n", "for product, quantity in inventory.items():\n", " print(product, \":\", quantity)" ] From e54aeef0ca0f0dd2bf42882f6dd53e2c2a60291d Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Tue, 4 Nov 2025 01:14:37 +0100 Subject: [PATCH 5/8] commit --- lab-python-data-structures.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index b079848a..15fa13d8 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -56,7 +56,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# SOLUTION\n", + "# Solved Lab 01 | Data Structures\n", "----" ] }, From 6fdbcc98ff24baf1043ecb81a7a52ae6a5816a1f Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Thu, 6 Nov 2025 00:00:56 +0100 Subject: [PATCH 6/8] commit --- lab-python-data-structures.ipynb | 103 ++++++++----------------------- 1 file changed, 25 insertions(+), 78 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 15fa13d8..d0c5723d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -64,12 +64,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -95,17 +95,9 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] - } - ], + "outputs": [], "source": [ "# 2. creating an empty dictionary invetory\n", "inventory = {}\n", @@ -121,19 +113,24 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "{}\n", "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" ] } ], "source": [ "# 3. input of the user for quantity of each product\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "print(inventory)\n", + "\n", "for i in range(len(products)):\n", " while True: # keep asking until valid\n", " quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n", @@ -157,17 +154,9 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set()\n" - ] - } - ], + "outputs": [], "source": [ "# 4. creating an empty set customer orders\n", "customer_orders = set()\n", @@ -183,22 +172,9 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Here is the list of products:\n", - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "Enter the names of 3 products to order:\n", - "✅ Added 'mug' to customer orders.\n", - "✅ Added 'book' to customer orders.\n", - "✅ Added 'hat' to customer orders.\n" - ] - } - ], + "outputs": [], "source": [ "# 5. User input for 3 products to order\n", "print(\"Here is the list of products:\")\n", @@ -225,17 +201,9 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Customer Orders: {'book', 'mug', 'hat'}\n" - ] - } - ], + "outputs": [], "source": [ "# 6. Printing the ordered products\n", "print(\"Customer Orders:\", customer_orders)" @@ -254,7 +222,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -286,19 +254,9 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Order Statistics:\n", - "Total Products Ordered: 3\n", - "Percentage of Products Ordered: 60%\n" - ] - } - ], + "outputs": [], "source": [ "# 8. Storing the statistics in a tuple order_status\n", "print(\"Order Statistics:\")\n", @@ -315,14 +273,16 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 9. Updating the inventory by subtracting 1 for each ordered product\n", "for ordered_product in customer_orders:\n", " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] = inventory[ordered_product] - 1" + " inventory[ordered_product] = inventory[ordered_product] - 1\n", + " else:\n", + " " ] }, { @@ -334,22 +294,9 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Updated Inventory :\n", - "t-shirt : 10\n", - "mug : 17\n", - "hat : 27\n", - "book : 37\n", - "keychain : 50\n" - ] - } - ], + "outputs": [], "source": [ "# 10. Printing the updated inventory\n", "print(\"Updated Inventory :\")\n", From e04ec6ef15670ca752fbc7e92ba1466ea11e5bf1 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sun, 16 Nov 2025 22:08:59 +0100 Subject: [PATCH 7/8] commit --- lab-python-data-structures.ipynb | 575 +++++++++++++++++++++++++++---- 1 file changed, 514 insertions(+), 61 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index d0c5723d..056d6fc7 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -56,20 +56,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Solved Lab 01 | Data Structures\n", - "----" + "----\n", + "# Solved Lab 01 | Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n" + "----\n", + "#### 1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\". " ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -81,7 +82,7 @@ } ], "source": [ - "# 1.defining the list products\n", + "# define list products\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "print(products)" ] @@ -90,16 +91,25 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "2. Create an empty dictionary called `inventory`." + "#### 2. Create an empty dictionary called `inventory`\n", + "-----" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], "source": [ - "# 2. creating an empty dictionary invetory\n", + "# create empty dict inventory\n", "inventory = {}\n", "print(inventory)" ] @@ -108,12 +118,58 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." + "-----\n", + "#### 3. Ask the user to input the quantity of each product available in the inventory. \n", + "Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Before updating the inventory: {}\n", + "\n", + "Products quantities input:\n", + "- t-shirt quantity updated to 20.\n", + "- mug quantity updated to 30.\n", + "- hat quantity updated to 40.\n", + "- book quantity updated to 1.\n", + "- keychain quantity updated to 2.\n", + "\n", + "Updated Inventory: {'t-shirt': 20, 'mug': 30, 'hat': 40, 'book': 1, 'keychain': 2}\n" + ] + } + ], + "source": [ + "#3. user inputs the quantity of each product available in the inventory:\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "print(\"Before updating the inventory:\", inventory)\n", + "print(\"\\nProducts quantities input:\")\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " print(f\"- {product} quantity updated to {quantity}.\")\n", + "print(\"\\nUpdated Inventory:\", inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "##### Error handling the input" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -121,7 +177,7 @@ "output_type": "stream", "text": [ "{}\n", - "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" + "{'t-shirt': 3, 'mug': 4, 'hat': 5, 'book': 6, 'keychain': 5}\n" ] } ], @@ -149,16 +205,25 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "4. Create an empty set called `customer_orders`." + "-----\n", + "#### 4. Create an empty set called `customer_orders`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], "source": [ - "# 4. creating an empty set customer orders\n", + "# create empty set for customer orders\n", "customer_orders = set()\n", "print(customer_orders)" ] @@ -167,53 +232,203 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set." + "-----\n", + "#### 5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " Empty Customer's orders: set()\n", + "Error! Please input product from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "- t-shirt has been added to your order!\n", + "- book has been added to your order!\n", + "\n", + "Updated Customer's orders: {'book', 't-shirt'}\n" + ] + } + ], + "source": [ + "# 5. Ask the user to input 3 product names to add to a set called `customers_orders`.----------------\n", + "# define list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Here are the available products :\", products)\n", + "\n", + "# Empty customer orders set for testing\n", + "customer_orders = set()\n", + "print(\"\\n Empty Customer's orders:\", customer_orders)\n", + "\n", + "# INPUT 3 PRODUCTS using loop\n", + "for i in range(3):\n", + "\n", + " product = input(\"Enter product name: \").lower() # convert input to lowercase\n", + " if product in products:\n", + " print(f\"- {product} has been added to your order!\")\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Error! Please input product from the list: \", products)\n", + " continue # skip to next iteration if product not in list\n", + " \n", + "\n", + "# updated customer orders set with products for testing\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " Empty Customer's orders: set() \n", + "\n", + "Thank you for your purchase!\n", + "\n", + "Updated Customer's orders: set()\n" + ] + } + ], "source": [ - "# 5. User input for 3 products to order\n", - "print(\"Here is the list of products:\")\n", - "print(products)\n", + "# 5. using while instead of for loop---------------------------------------------------------------------------------\n", + "# unlimited adding of products until We enter 'BUY'\n", + "# define list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Here are the available products :\", products)\n", + "\n", + "# Empty customer orders set for testing\n", + "customer_orders = set()\n", + "print(\"\\n Empty Customer's orders:\", customer_orders, \"\\n\")\n", "\n", - "print(\"Enter the names of 3 products to order:\")\n", + "# must initialize product variable before while loop\n", + "product = \"\"\n", + "# INPUT 3 PRODUCTS using loop\n", + "while product !=\"buy\": # keep asking for product input until we write buy\n", "\n", - "while len(customer_orders) < 3:\n", - " item = input(\"Enter a product name: \").lower()\n", + " product = input(\"Enter product name or BUY to complete your purchase: \").lower() # convert input to lowercase\n", + " if product in products:\n", + " print(f\"- {product} has been added to your order!\")\n", + " customer_orders.add(product)\n", + " \n", + " elif product == \"buy\":\n", + " print(\"Thank you for your purchase!\")\n", + " break\n", "\n", - " if item in products:\n", - " customer_orders.add(item)\n", - " print(f\"✅ Added '{item}' to customer orders.\")\n", " else:\n", - " print(\"❌ Invalid product! Please choose from:\", products)" + " print(f\"Error! {product} is not listed in our products list. Please input product from the list: {products}\")\n", + " continue # skip to next iteration if product not in list\n", + " \n", + "\n", + "# updated customer orders set with products for testing\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " Empty Customer's orders: set() \n", + "\n", + "Error! tshirt is not listed in our products list. Please input product from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Error! tshirt is not listed in our products list. Please input product from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Error! boook is not listed in our products list. Please input product from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Error! boooook is not listed in our products list. Please input product from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "- book has been added to your order!\n", + "- mug has been added to your order!\n", + "- hat has been added to your order!\n", + "\n", + "Updated Customer's orders: {'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# 5. using while instead of for loop---------------------------------------------------------------------------------\n", + "# limited adding of only 3 products until We enter 'BUY'\n", + "# define list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Here are the available products :\", products)\n", + "\n", + "# Empty customer orders set for testing\n", + "customer_orders = set()\n", + "print(\"\\n Empty Customer's orders:\", customer_orders, \"\\n\")\n", + "\n", + "# must initialize product variable before while loop\n", + "product = \"\"\n", + "product_nb = 0\n", + "# INPUT 3 PRODUCTS using loop\n", + "while product !=\"buy\" and product_nb<3: # keep asking for product input until we write buy\n", + "\n", + " product = input(\"Enter product name or BUY to complete your purchase: \").lower() # convert input to lowercase\n", + " if product in products:\n", + " print(f\"- {product} has been added to your order!\")\n", + " customer_orders.add(product)\n", + " product_nb+=1\n", + " \n", + " elif product == \"buy\":\n", + " print(\"Thank you for your purchase!\")\n", + " break\n", + "\n", + " else:\n", + " print(f\"Error! {product} is not listed in our products list. Please input product from the list: {products}\")\n", + " continue # skip to next iteration if product not in list\n", + " \n", + "\n", + "# updated customer orders set with products for testing\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "6. Print the products in the `customer_orders` set." + "-----\n", + "#### 6. Print the products in the `customer_orders` set" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Customer's orders: {'book', 'hat', 'mug'}\n" + ] + } + ], "source": [ - "# 6. Printing the ordered products\n", - "print(\"Customer Orders:\", customer_orders)" + "# 6. printing the Updated Customer's orders: ----------------------------------------------------------------\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "7. Calculate the following order statistics:\n", + "-----\n", + "#### 7. Calculate the following order statistics:\n", " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", " \n", @@ -222,29 +437,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.00%\n", + "Order Status (Total Products Ordered, Percentage): (3, 60.0)\n" + ] + } + ], "source": [ - "# 7. Calculating order statistics\n", + "# 7. Calculate the following order statistics: ------------------------------------------------------------\n", + "# 7.1. Total products ordered\n", "\n", - "# Total Products Ordered\n", "total_products_ordered = len(customer_orders)\n", + "print(f\"\\nTotal Products Ordered: {total_products_ordered}\")\n", "\n", - "# Percentage of Products Ordered\n", - "percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + "# 7.2. Percentage products ordered from total products ordered\n", + "percentage = total_products_ordered / len(products) * 100\n", "\n", - "# Calculate Order Status\n", - "order_status = (total_products_ordered, percentage_products_ordered)\n", - "# Print Order Status\n", - "#print(\"Order Status (Total, Percentage):\", order_status)" + "print(f\"Percentage of Products Ordered: {percentage:.2f}%\")\n", + "\n", + "# Store these statistics in a tuple called `order_status`.\n", + "\n", + "order_status = (total_products_ordered, percentage)\n", + "print(\"Order Status (Total Products Ordered, Percentage):\", order_status)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "8. Print the order statistics using the following format:\n", + "-----\n", + "#### 8. Print the order statistics using the following format:\n", " ```\n", " Order Statistics:\n", " Total Products Ordered: \n", @@ -256,19 +486,135 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.00% \n" + ] + } + ], "source": [ "# 8. Storing the statistics in a tuple order_status\n", - "print(\"Order Statistics:\")\n", - "print(\"Total Products Ordered:\", total_products_ordered)\n", - "print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")" + "\n", + "# Print the order statistics using the following format--------------------\n", + "\n", + "# Order Statistics:\n", + "print(\"\\nOrder Statistics:\")\n", + "\n", + "# Total Products Ordered: \n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "\n", + "# Percentage of Products Ordered: % \n", + "print(f\"Percentage of Products Ordered: {percentage:.2f}% \")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + "-----\n", + "#### 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- book inventory decreased by 1. New quantity: 4\n", + "- hat inventory decreased by 1. New quantity: 3\n", + "- mug inventory decreased by 1. New quantity: 2\n" + ] + } + ], + "source": [ + "# 9. Update the inventory dict by substracting 1 from products in customer_orders set ---------------------------------------------\n", + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(f\"- {product} inventory decreased by 1. New quantity: {inventory[product]}\")\n", + " else:\n", + " print(f\"- {product} is out of stock!\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "#### 10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after Customer's orders: {'t-shirt': 3, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n", + "\n", + "Updated Inventory after Customer's orders:\n", + "- t-shirt: 3 units left\n", + "- mug: 2 units left\n", + "- hat: 3 units left\n", + "- book: 4 units left\n", + "- keychain: 5 units left\n", + "\n", + "Updated Inventory after Customer's orders:\n", + "- t-shirt: 3 units left\n", + "- mug: 2 units left\n", + "- hat: 3 units left\n", + "- book: 4 units left\n", + "- keychain: 5 units left\n" + ] + } + ], + "source": [ + "# Print the updated inventory\n", + "\n", + "# print way 1: call dict\n", + "print(\"\\nUpdated Inventory after Customer's orders:\", inventory)\n", + "\n", + "# print way 2: bullet points using key[i]\n", + "print(\"\\nUpdated Inventory after Customer's orders:\")\n", + "for product in products:\n", + " print(f\"- {product}: {inventory[product]} units left\")\n", + "\n", + "# print way 3: using items()\n", + "print(\"\\nUpdated Inventory after Customer's orders:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"- {product}: {quantity} units left\")\n", + "\n", + "# for key, value in dictionary.items():\n", + "# print(f\"{key}: {value}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "# ***HOW TO ?***" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### How to print the updated dict using `for` loop AND `get()`" ] }, { @@ -277,19 +623,16 @@ "metadata": {}, "outputs": [], "source": [ - "# 9. Updating the inventory by subtracting 1 for each ordered product\n", - "for ordered_product in customer_orders:\n", - " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] = inventory[ordered_product] - 1\n", - " else:\n", - " " + "dict_name = { 'key' : 'value'}\n", + "for key in dict_name.keys():\n", + " print(f\"{key}: {key.get()}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "10. Print the updated inventory, displaying the quantity of each product on separate lines." + "##### How to print the updated dict using `for` loop AND `items()`" ] }, { @@ -298,10 +641,120 @@ "metadata": {}, "outputs": [], "source": [ - "# 10. Printing the updated inventory\n", - "print(\"Updated Inventory :\")\n", - "for product, quantity in inventory.items():\n", - " print(product, \":\", quantity)" + "for key, value in dict_name.items():\n", + " print(f\"{key}: {value}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Different Ways to write mathematical operations----------------------------- \n", + "# Subtraction\n", + "x -= 1\n", + "x = x - 1\n", + "\n", + "# Addition\n", + "x += 1\n", + "x = x + 1\n", + "\n", + "# Division\n", + "x /= 1\n", + "x = x / 1\n", + "\n", + "# Multiplication\n", + "x *= 1\n", + "x = x * 1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### How to define EMPTY different data structures in Python :" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "set_name = set()\n", + "list_name = []\n", + "dict_name = {}\n", + "tuple_name = {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### How to add to an EMPTY data Structure:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "variable = 'whatever'\n", + "# How to add to a set\n", + "set_name.add(variable)\n", + "\n", + "# How to add to a list\n", + "list_name.append(variable)\n", + "\n", + "# How to add to a dict\n", + "# missing\n", + "# How to add to a tuple\n", + "# missing" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### How to input into an empty set" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# customer orders empty set\n", + "customer_orders = set()\n", + "print(\"\\nCustomer's orders:\", customer_orders)\n", + "\n", + "# INPUT 3 PRODUCTS using loop\n", + "print(range(3))\n", + "for i in range(3):\n", + " product = input(\"Enter product name: \")\n", + " print(f\"- {product}\")\n", + " customer_orders.add(product)\n", + "\n", + "# customer orders set with products\n", + "print(\"\\nCustomer's orders:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# input 1 product\n", + "product = input(\"Enter product name: \")\n", + "\n", + "# INPUT 3 PRODUCTS using loop\n", + "print(range(3))\n", + "for i in range(3):\n", + " print(\"Line number :\", i)\n" ] } ], From 13e0aee7cf0a27fc6f8e634166613dcdb0d60bd5 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sun, 16 Nov 2025 22:15:51 +0100 Subject: [PATCH 8/8] commit --- lab-python-data-structures.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 056d6fc7..15cf1dde 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -169,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -177,7 +177,7 @@ "output_type": "stream", "text": [ "{}\n", - "{'t-shirt': 3, 'mug': 4, 'hat': 5, 'book': 6, 'keychain': 5}\n" + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" ] } ],