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..1791fb57 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,256 @@ +{ + "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": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# 1\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# 2\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number or t-shirt available: 2\n", + "Please provide number or mug available: 3\n", + "Please provide number or hat available: 4\n", + "Please provide number or book available: 5\n", + "Please provide number or keychain available: 6\n" + ] + } + ], + "source": [ + "# 3\n", + "for product in products:\n", + " inventory[product] = input(f\"Please provide number of {product} available: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "# 4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add product number 1 to the order: book\n", + "Please add product number 2 to the order: hat\n", + "Please add product number 3 to the order: keychain\n" + ] + } + ], + "source": [ + "# 5\n", + "for i in range(3):\n", + " customer_orders.add(input(f\"Please add product number {i + 1} to the order: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The products in the order are: {'keychain', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "# 6\n", + "print(f\"The products in the order are: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of products in the order is 3.\n" + ] + } + ], + "source": [ + "# 7\n", + "order_length = len(customer_orders)\n", + "print(f\"The number of products in the order is {order_length}.\")\n", + "\n", + "products_length = len(products)\n", + "\n", + "total_products = 0\n", + "for product in products:\n", + " total_products += int(inventory[product])\n", + "\n", + "perc_ordered = (order_length / total_products) * 100\n", + "\n", + "stats_tuple = (products_length, perc_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 17.647058823529413%\n" + ] + } + ], + "source": [ + "# 8\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_length}\")\n", + "print(f\"Percentage of Products Ordered: {perc_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "# 9\n", + "for product in customer_orders:\n", + " if product in inventory.keys():\n", + " prod = int(inventory[product])\n", + " prod -= 1\n", + " inventory[product] = str(prod)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 2\n", + "mug: 3\n", + "hat: 3\n", + "book: 4\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "# 10\n", + "for product in products:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + } + ], + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..de644361 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,204 @@ "\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": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# 1\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# 2\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number or t-shirt available: 2\n", + "Please provide number or mug available: 3\n", + "Please provide number or hat available: 4\n", + "Please provide number or book available: 5\n", + "Please provide number or keychain available: 6\n" + ] + } + ], + "source": [ + "# 3\n", + "for product in products:\n", + " inventory[product] = input(f\"Please provide number of {product} available: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "# 4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add product number 1 to the order: book\n", + "Please add product number 2 to the order: hat\n", + "Please add product number 3 to the order: keychain\n" + ] + } + ], + "source": [ + "# 5\n", + "for i in range(3):\n", + " customer_orders.add(input(f\"Please add product number {i + 1} to the order: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The products in the order are: {'keychain', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "# 6\n", + "print(f\"The products in the order are: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of products in the order is 3.\n" + ] + } + ], + "source": [ + "# 7\n", + "order_length = len(customer_orders)\n", + "print(f\"The number of products in the order is {order_length}.\")\n", + "\n", + "products_length = len(products)\n", + "\n", + "total_products = 0\n", + "for product in products:\n", + " total_products += int(inventory[product])\n", + "\n", + "perc_ordered = (order_length / total_products) * 100\n", + "\n", + "stats_tuple = (products_length, perc_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'order_length' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# 8\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOrder Statistics:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTotal Products Ordered: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00morder_length\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPercentage of Products Ordered: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mperc_ordered\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'order_length' is not defined" + ] + } + ], + "source": [ + "# 8\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_length}\")\n", + "print(f\"Percentage of Products Ordered: {perc_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "# 9\n", + "for product in customer_orders:\n", + " if product in inventory.keys():\n", + " prod = int(inventory[product])\n", + " prod -= 1\n", + " inventory[product] = str(prod)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 2\n", + "mug: 3\n", + "hat: 3\n", + "book: 4\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "# 10\n", + "for product in products:\n", + " print(f\"{product}: {inventory[product]}\")" + ] } ], "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 +259,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,