diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb new file mode 100644 index 00000000..c78d8635 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,423 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "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": [ + "### 1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"T-shirt\", \"Mug\", \"Hat\", \"Book\", \"Keychain\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2 Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {} " + ] + }, + { + "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. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Input T-shirts quantity: 4\n", + "Input Mug quantity: 3\n", + "Input Hat quantity: 2\n", + "Input Book quantity: 1\n", + "Input Keychain quantity: 5\n" + ] + } + ], + "source": [ + "inventory[products[0]] = int(input(\"Input T-shirts quantity: \"))\n", + "inventory[products[1]] = int(input(\"Input Mug quantity: \"))\n", + "inventory[products[2]] = int(input(\"Input Hat quantity: \"))\n", + "inventory[products[3]] = int(input(\"Input Book quantity: \"))\n", + "inventory[products[4]] = int(input(\"Input Keychain quantity: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'T-shirt': 4, 'Mug': 3, 'Hat': 2, 'Book': 1, 'Keychain': 5}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4 Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain mug\n", + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain hat\n", + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Successe\n" + ] + } + ], + "source": [ + "Item1 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "Item2 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "Item3 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "if Item1 in products and Item2 in products and Item3 in products:\n", + " customer_orders.update([Item1,Item2,Item3])\n", + " print(\"Successe\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "# customer_orders.difference_update([\"Mug\", \"Hat\", \"Book\"]) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. Print the products in the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Book', 'Mug', 'Hat'}\n" + ] + } + ], + "source": [ + "print(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." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "Total_Products_Ordered = len(customer_orders)\n", + "print(Total_Products_Ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20%\n" + ] + } + ], + "source": [ + "Percentage_of_Products_Ordered = Total_Products_Ordered / sum(inventory.values()) * 100\n", + "print(str(round(Percentage_of_Products_Ordered)) + \"%\" )" + ] + }, + { + "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": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 20%\n" + ] + } + ], + "source": [ + "print(f\"Order Statistics:\\nTotal Products Ordered: {Total_Products_Ordered}\\nPercentage of Products Ordered: {round(Percentage_of_Products_Ordered)}%\")\n" + ] + }, + { + "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": 32, + "metadata": {}, + "outputs": [], + "source": [ + "# for item in customer_orders & inventory.keys():\n", + " # inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "if Item1 in inventory and Item1 in customer_orders:\n", + " inventory[Item1] -= 1\n", + "if Item2 in inventory and Item2 in customer_orders:\n", + " inventory[Item2] -= 1\n", + "if Item3 in inventory and Item3 in customer_orders:\n", + " inventory[Item3] -= 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": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('T-shirt', 4), ('Mug', 3), ('Hat', 2), ('Book', 1), ('Keychain', 5)])\n" + ] + } + ], + "source": [ + "# Before\n", + "print(inventory.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('T-shirt', 4), ('Mug', 2), ('Hat', 1), ('Book', 0), ('Keychain', 5)])\n" + ] + } + ], + "source": [ + "# After:\n", + "print(inventory.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirt: 4\n", + "Mug: 2\n", + "Hat: 1\n", + "Book: 0\n", + "Keychain: 5\n" + ] + } + ], + "source": [ + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..c78d8635 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,360 @@ "\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": [ + "### 1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"T-shirt\", \"Mug\", \"Hat\", \"Book\", \"Keychain\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2 Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {} " + ] + }, + { + "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. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Input T-shirts quantity: 4\n", + "Input Mug quantity: 3\n", + "Input Hat quantity: 2\n", + "Input Book quantity: 1\n", + "Input Keychain quantity: 5\n" + ] + } + ], + "source": [ + "inventory[products[0]] = int(input(\"Input T-shirts quantity: \"))\n", + "inventory[products[1]] = int(input(\"Input Mug quantity: \"))\n", + "inventory[products[2]] = int(input(\"Input Hat quantity: \"))\n", + "inventory[products[3]] = int(input(\"Input Book quantity: \"))\n", + "inventory[products[4]] = int(input(\"Input Keychain quantity: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'T-shirt': 4, 'Mug': 3, 'Hat': 2, 'Book': 1, 'Keychain': 5}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4 Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain mug\n", + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain hat\n", + "Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Successe\n" + ] + } + ], + "source": [ + "Item1 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "Item2 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "Item3 = input(\"Please chose up to 3 of these products: t-shirt , mug, hat, book, keychain\").title()\n", + "if Item1 in products and Item2 in products and Item3 in products:\n", + " customer_orders.update([Item1,Item2,Item3])\n", + " print(\"Successe\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "# customer_orders.difference_update([\"Mug\", \"Hat\", \"Book\"]) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. Print the products in the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Book', 'Mug', 'Hat'}\n" + ] + } + ], + "source": [ + "print(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." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "Total_Products_Ordered = len(customer_orders)\n", + "print(Total_Products_Ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20%\n" + ] + } + ], + "source": [ + "Percentage_of_Products_Ordered = Total_Products_Ordered / sum(inventory.values()) * 100\n", + "print(str(round(Percentage_of_Products_Ordered)) + \"%\" )" + ] + }, + { + "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": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 20%\n" + ] + } + ], + "source": [ + "print(f\"Order Statistics:\\nTotal Products Ordered: {Total_Products_Ordered}\\nPercentage of Products Ordered: {round(Percentage_of_Products_Ordered)}%\")\n" + ] + }, + { + "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": 32, + "metadata": {}, + "outputs": [], + "source": [ + "# for item in customer_orders & inventory.keys():\n", + " # inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "if Item1 in inventory and Item1 in customer_orders:\n", + " inventory[Item1] -= 1\n", + "if Item2 in inventory and Item2 in customer_orders:\n", + " inventory[Item2] -= 1\n", + "if Item3 in inventory and Item3 in customer_orders:\n", + " inventory[Item3] -= 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": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('T-shirt', 4), ('Mug', 3), ('Hat', 2), ('Book', 1), ('Keychain', 5)])\n" + ] + } + ], + "source": [ + "# Before\n", + "print(inventory.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('T-shirt', 4), ('Mug', 2), ('Hat', 1), ('Book', 0), ('Keychain', 5)])\n" + ] + } + ], + "source": [ + "# After:\n", + "print(inventory.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirt: 4\n", + "Mug: 2\n", + "Hat: 1\n", + "Book: 0\n", + "Keychain: 5\n" + ] + } + ], + "source": [ + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -68,7 +415,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.5" } }, "nbformat": 4,