diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..33fa331e 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -6,7 +6,8 @@ "tags": [] }, "source": [ - "# Lab | Data Structures " + "# Lab | Data Structures \n", + "-----" ] }, { @@ -50,11 +51,644 @@ "\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", + "# Managing Customer Orders\n", + "-----\n", + "By Paulina Mamiaga Iyanga / Ironhack Data Analytics Bootcamp Student\n", + "\n", + "--------" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## STEPS TO FOLLOW:\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": 17, + "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": [ + "-----\n", + "2. Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n", + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" + ] + } + ], + "source": [ + "# create empty dict inventory\n", + "inventory = {}\n", + "print(inventory)\n", + "\n", + "# Manual initalize dict inventory with products as keys and quantities as values\n", + "inventory = { \n", + " \"t-shirt\": 10,\n", + " \"mug\": 20,\n", + " \"hat\": 30,\n", + " \"book\": 40,\n", + " \"keychain\": 50\n", + "}\n", + "\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": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- t-shirt quantity updated to 56.\n", + "- mug quantity updated to 67.\n", + "- hat quantity updated to 87.\n", + "- book quantity updated to 90.\n", + "- keychain quantity updated to 90.\n", + "\n", + "Updated Inventory: {'t-shirt': 56, 'mug': 67, 'hat': 87, 'book': 90, 'keychain': 90}\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", + "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", + "4. Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# initize 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": 22, + "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", + "- mug has been added to your order!\n", + "- hat has been added to your order!\n", + "- t-shirt has been added to your order!\n", + "\n", + "Updated Customer's orders: {'hat', 'mug', '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(\"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": 23, + "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", + "- mug has been added to your order!\n", + "- hat has been added to your order!\n", + "- t-shirt has been added to your order!\n", + "- book has been added to your order!\n", + "- keychain has been added to your order!\n", + "Thank you for your purchase!\n", + "\n", + "Updated Customer's orders: {'book', 't-shirt', 'hat', 'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "# 5. using while instead of for loop---------------------------------------------------------------------------------\n", + "\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: \").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(\"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": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Customer's orders: {'book', 't-shirt', 'hat', 'mug', 'keychain'}\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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Products Ordered: 5\n", + "Percentage of Products Ordered: 100.00%\n", + "\n", + "Order Status (Total Products Ordered, Percentage): (5, 100.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(\"\\nOrder 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": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 5\n", + "Percentage of Products Ordered: 100.00% \n" + ] + } + ], + "source": [ + "# 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": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- book inventory decreased by 1. New quantity: 89\n", + "- t-shirt inventory decreased by 1. New quantity: 55\n", + "- hat inventory decreased by 1. New quantity: 86\n", + "- mug inventory decreased by 1. New quantity: 66\n", + "- keychain inventory decreased by 1. New quantity: 89\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": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after Customer's orders: {'t-shirt': 55, 'mug': 66, 'hat': 86, 'book': 89, 'keychain': 89}\n", + "- t-shirt: 55 units left\n", + "- mug: 66 units left\n", + "- hat: 86 units left\n", + "- book: 89 units left\n", + "- keychain: 89 units left\n" + ] + } + ], + "source": [ + "# Print the updated inventory\n", + "print(\"\\nUpdated Inventory after Customer's orders:\", inventory)\n", + "for product in products:\n", + " print(f\"- {product}: {inventory[product]} units left\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after Customer's orders:\n", + "- t-shirt: 55 units left\n", + "- mug: 66 units left\n", + "- hat: 86 units left\n", + "- book: 89 units left\n", + "- keychain: 89 units left\n" + ] + } + ], + "source": [ + "# Another way to print the updated inventory\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", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "### HOW TO ?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How to copy? CTRL + C\n", + "\n", + "How to cut? CTRL + X \n", + "\n", + "How to paste? CTRL + V" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Another way to print the updated inventory dict using .items()\n", + "# for key, value in dictionary.items():\n", + "# print(f\"{key}: {value}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Different Ways to write mathematical operations----------------------------- \n", + "# Subtraction\n", + "inventory[product] -= 1\n", + "inventory[product] = inventory[product] - 1\n", + "\n", + "# Addition\n", + "inventory[product] += 1\n", + "inventory[product] = inventory[product] + 1\n", + "\n", + "# Division\n", + "inventory[product] /= 1\n", + "inventory[product] = inventory[product] / 1\n", + "\n", + "# Multiplication\n", + "inventory[product] *= 1\n", + "inventory[product] = inventory[product] * 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create an empty set called \"customers_orders\n", + "customers_orders = set()\n", + "print(customers_orders)\n", + "\n", + "#-----------------------------------------------------------------------------------------\n", + "# How to define EMPTY different data structures in Python :\n", + "# set_name = set()\n", + "# list_name = []\n", + "# dict_name = {}\n", + "# tuple_name = {}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create an empty set called \"customers_orders\n", + "customer_orders = set()\n", + "print(customer_orders)\n", + "\n", + "#-----------------------------------------------------------------------------------------\n", + "# How to define EMPTY different data structures in Python :\n", + "# set_name = set()\n", + "# list_name = []\n", + "# dict_name = {}\n", + "# tuple_name = {}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# The different way to add between set and list---------------\n", + "\n", + "# How to add to a set\n", + "customer_orders.add(product)\n", + "\n", + "# How to add to a list\n", + "customer_orders.append(product)" + ] + }, + { + "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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "range(0, 3)\n", + "Line number : 0\n", + "Line number : 1\n", + "Line number : 2\n" + ] + } + ], + "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", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +702,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,