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..3780e786 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,448 @@ +{ + "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": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#2. Create an empty dictionary called inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt: 4\n", + "Enter the quantity of mug: 5\n", + "Enter the quantity of hat: 3\n", + "Enter the quantity of book: 7\n", + "Enter the quantity of keychain: 9\n" + ] + } + ], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 4, 'mug': 5, 'hat': 3, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(\"\\nInventory:\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty set called customer_orders." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Ask the user to input the name of three products that a customer wants to order \n", + "#(from those in the products list, meaning three products out of \n", + "#\"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the customer_orders set.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# List of available products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): book\n", + "Enter product 2 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): keychain\n", + "Enter product 3 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): mug\n" + ] + } + ], + "source": [ + "for i in range(3):\n", + " product = input(f\"Enter product {i+1} (choose from {products}): \").strip().lower()\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(f\"'{product}' is not a valid product. Please choose from the list.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#6. Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer order:\n", + "{'keychain', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "print(\"\\nCustomer order:\")\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#7. Calculate the following order statistics:\n", + "\n", + "#a- Total Products Ordered: The total number of products in the customer_orders set.\n", + "#b- 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": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# a) Total Products Ordered\n", + "total_ordered = len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(total_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# b) Percentage of Products Ordered\n", + "percentage_ordered = (total_ordered / len(products)) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0\n" + ] + } + ], + "source": [ + "print(percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "# Store in a tuple\n", + "order_status = (total_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Status: (3, 60.0)\n" + ] + } + ], + "source": [ + "# Show results\n", + "print(\"Order Status:\", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#8. Print the order statistics using the following format:\n", + "\n", + "#Order Statistics:\n", + "#Total Products Ordered: \n", + "#Percentage of Products Ordered: % " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.00%\n" + ] + } + ], + "source": [ + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory:\n", + " if inventory[product] > 0: # prevent negative quantities\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 3\n", + "mug: 4\n", + "hat: 2\n", + "book: 6\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "# Print the updated inventory\n", + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "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..3780e786 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,385 @@ "\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": [], + "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": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#2. Create an empty dictionary called inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt: 4\n", + "Enter the quantity of mug: 5\n", + "Enter the quantity of hat: 3\n", + "Enter the quantity of book: 7\n", + "Enter the quantity of keychain: 9\n" + ] + } + ], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory:\n", + "{'t-shirt': 4, 'mug': 5, 'hat': 3, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(\"\\nInventory:\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty set called customer_orders." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Ask the user to input the name of three products that a customer wants to order \n", + "#(from those in the products list, meaning three products out of \n", + "#\"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the customer_orders set.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# List of available products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): book\n", + "Enter product 2 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): keychain\n", + "Enter product 3 (choose from ['t-shirt', 'mug', 'hat', 'book', 'keychain']): mug\n" + ] + } + ], + "source": [ + "for i in range(3):\n", + " product = input(f\"Enter product {i+1} (choose from {products}): \").strip().lower()\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(f\"'{product}' is not a valid product. Please choose from the list.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#6. Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer order:\n", + "{'keychain', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "print(\"\\nCustomer order:\")\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#7. Calculate the following order statistics:\n", + "\n", + "#a- Total Products Ordered: The total number of products in the customer_orders set.\n", + "#b- 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": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# a) Total Products Ordered\n", + "total_ordered = len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(total_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# b) Percentage of Products Ordered\n", + "percentage_ordered = (total_ordered / len(products)) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0\n" + ] + } + ], + "source": [ + "print(percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "# Store in a tuple\n", + "order_status = (total_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Status: (3, 60.0)\n" + ] + } + ], + "source": [ + "# Show results\n", + "print(\"Order Status:\", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#8. Print the order statistics using the following format:\n", + "\n", + "#Order Statistics:\n", + "#Total Products Ordered: \n", + "#Percentage of Products Ordered: % " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.00%\n" + ] + } + ], + "source": [ + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory:\n", + " if inventory[product] > 0: # prevent negative quantities\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 3\n", + "mug: 4\n", + "hat: 2\n", + "book: 6\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "# Print the updated inventory\n", + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "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 +440,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.5" } }, "nbformat": 4,