diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..15cf1dde 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -6,7 +6,8 @@ "tags": [] }, "source": [ - "# Lab | Data Structures " + "# Lab 01 | Data Structures\n", + "----" ] }, { @@ -50,11 +51,716 @@ "\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": [ + "----\n", + "# Solved Lab 01 | Data Structures" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "#### 1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\". " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# define list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create an empty dictionary called `inventory`\n", + "-----" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# create empty dict inventory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\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": 24, + "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", + " 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": [ + "-----\n", + "#### 4. Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# create empty set for customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "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." + ] + }, + { + "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": [ + { + "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. 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", + "# 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", + " 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", + " 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": "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": [ + "-----\n", + "#### 6. Print the products in the `customer_orders` set" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Customer's orders: {'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# 6. printing the Updated Customer's orders: ----------------------------------------------------------------\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\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`." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "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. Calculate the following order statistics: ------------------------------------------------------------\n", + "# 7.1. Total products ordered\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "print(f\"\\nTotal Products Ordered: {total_products_ordered}\")\n", + "\n", + "# 7.2. Percentage products ordered from total products ordered\n", + "percentage = total_products_ordered / len(products) * 100\n", + "\n", + "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": [ + "-----\n", + "#### 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": null, + "metadata": {}, + "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", + "\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": [ + "-----\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()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict_name = { 'key' : 'value'}\n", + "for key in dict_name.keys():\n", + " print(f\"{key}: {key.get()}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### How to print the updated dict using `for` loop AND `items()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +774,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,