From 7219945e1eb260ea3ec17c0c0b882ba974e8d629 Mon Sep 17 00:00:00 2001 From: Priyanka Marmath Date: Tue, 2 Sep 2025 10:34:18 +0200 Subject: [PATCH 1/2] lab solved --- lab-python-data-structures.ipynb | 191 ++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..892a8e2d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,198 @@ "\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": 8, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {} #Create an empty dictionary called inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the qty t-shirt:, 4\n", + "Enter the qty mug:, 5\n", + "Enter the qty hat:, 6\n", + "Enter the qty book:, 7\n", + "Enter the qty keychain:, 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 3}\n" + ] + } + ], + "source": [ + "for product in products: #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", + " quantity = int(input(f\"Enter the qty {product}:, \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set() #Create an empty set called customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the item to order 1:, mug\n", + "Enter the item to order 2:, hat\n", + "Enter the item to order 3:, book\n" + ] + } + ], + "source": [ + "print(\"The list of products\", products) #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", + "for x in range(3):\n", + " product = input(f\"Enter the item to order {x+1}:, \").lower()\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Invalid product\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "print(customer_orders) #Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = len(customer_orders) #total product ordered\n", + "percentage_total_products_ordered = (total_products_ordered / len(products)) * 100 #percentage of total products ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (total_products_ordered, percentage_total_products_ordered) #tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "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:\"). #Print the 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": 33, + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders: #Update the inventory by subtracting 1\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -=1" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory\n", + "t-shirt : 4\n", + "mug : 4\n", + "hat : 5\n", + "book : 6\n", + "keychain : 3\n" + ] + } + ], + "source": [ + "print(\"\\nUpdated Inventory\") #Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product} : {quantity}\")" + ] } ], "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 +253,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From da01d655255afa9a34177af79c7dd8a1cc59a24f Mon Sep 17 00:00:00 2001 From: Priyanka Marmath Date: Tue, 2 Sep 2025 10:35:37 +0200 Subject: [PATCH 2/2] solved lab --- ...ab-python-data-structures-checkpoint.ipynb | 261 ++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb 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..892a8e2d --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,261 @@ +{ + "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": 8, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {} #Create an empty dictionary called inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the qty t-shirt:, 4\n", + "Enter the qty mug:, 5\n", + "Enter the qty hat:, 6\n", + "Enter the qty book:, 7\n", + "Enter the qty keychain:, 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 3}\n" + ] + } + ], + "source": [ + "for product in products: #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", + " quantity = int(input(f\"Enter the qty {product}:, \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set() #Create an empty set called customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the item to order 1:, mug\n", + "Enter the item to order 2:, hat\n", + "Enter the item to order 3:, book\n" + ] + } + ], + "source": [ + "print(\"The list of products\", products) #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", + "for x in range(3):\n", + " product = input(f\"Enter the item to order {x+1}:, \").lower()\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Invalid product\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "print(customer_orders) #Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = len(customer_orders) #total product ordered\n", + "percentage_total_products_ordered = (total_products_ordered / len(products)) * 100 #percentage of total products ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (total_products_ordered, percentage_total_products_ordered) #tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "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:\"). #Print the 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": 33, + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders: #Update the inventory by subtracting 1\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -=1" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory\n", + "t-shirt : 4\n", + "mug : 4\n", + "hat : 5\n", + "book : 6\n", + "keychain : 3\n" + ] + } + ], + "source": [ + "print(\"\\nUpdated Inventory\") #Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product} : {quantity}\")" + ] + } + ], + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}