From aaa041ed27c554f17e43affd4af48dc858345ba1 Mon Sep 17 00:00:00 2001 From: Charul Mathur Date: Thu, 20 Nov 2025 15:24:43 +0100 Subject: [PATCH] Lab Solved --- ...ab-python-data-structures-checkpoint.ipynb | 278 ++++++++++++++++++ lab-python-data-structures.ipynb | 204 ++++++++++++- 2 files changed, 481 insertions(+), 1 deletion(-) 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..3b27b7e4 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,278 @@ +{ + "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": 78, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Create an empty dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 5\n", + "Quantity of mug: 10\n", + "Quantity of hat: 15\n", + "Quantity of book: 20\n", + "Quantity of keychain: 25\n" + ] + } + ], + "source": [ + "# Step 3: Ask the user for the quantity of each product\n", + "print(\"Enter the quantity for each product: \")\n", + "for item in products:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " inventory[item] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Create an empty set\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter three products the customer wants to order: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: hat\n", + "Product 2: mug\n", + "Product 3: book\n" + ] + } + ], + "source": [ + "# Step 5: Ask the user to input the name of three products\n", + "\n", + "print(\"Enter three products the customer wants to order: \")\n", + "for i in range(3):\n", + " order = input(f\"Product {i+1}: \").strip().lower()\n", + "\n", + " #if user adds an invalid product\n", + " \n", + " while order not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " order = input(f\"Product {i+1}: \").strip().lower()\n", + " customer_orders.add(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "# Step 6: Print ordered products\n", + "\n", + "print(\"Customer Orders: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Calculate order statistics\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Storing in tuple\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n" + ] + } + ], + "source": [ + "# Step 8: Print the statistics\n", + "\n", + "print(\"Order statistics: \")\n", + "print(\"Total products ordered: \", order_status[0])\n", + "print(\"Percentage of products ordered: \", f\"{order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 9: Update the inventory (subtracting 1 from the quantity of each product)\n", + "\n", + "for item in inventory:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: \n", + "t-shirt: 4\n", + "mug: 9\n", + "hat: 14\n", + "book: 19\n", + "keychain: 24\n" + ] + } + ], + "source": [ + "# Updated inventory\n", + "\n", + "print(\"Updated inventory: \")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..3b27b7e4 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,208 @@ "\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": 78, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Create an empty dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 5\n", + "Quantity of mug: 10\n", + "Quantity of hat: 15\n", + "Quantity of book: 20\n", + "Quantity of keychain: 25\n" + ] + } + ], + "source": [ + "# Step 3: Ask the user for the quantity of each product\n", + "print(\"Enter the quantity for each product: \")\n", + "for item in products:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " inventory[item] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Create an empty set\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter three products the customer wants to order: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: hat\n", + "Product 2: mug\n", + "Product 3: book\n" + ] + } + ], + "source": [ + "# Step 5: Ask the user to input the name of three products\n", + "\n", + "print(\"Enter three products the customer wants to order: \")\n", + "for i in range(3):\n", + " order = input(f\"Product {i+1}: \").strip().lower()\n", + "\n", + " #if user adds an invalid product\n", + " \n", + " while order not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " order = input(f\"Product {i+1}: \").strip().lower()\n", + " customer_orders.add(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "# Step 6: Print ordered products\n", + "\n", + "print(\"Customer Orders: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Calculate order statistics\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Storing in tuple\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n" + ] + } + ], + "source": [ + "# Step 8: Print the statistics\n", + "\n", + "print(\"Order statistics: \")\n", + "print(\"Total products ordered: \", order_status[0])\n", + "print(\"Percentage of products ordered: \", f\"{order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "# Step 9: Update the inventory (subtracting 1 from the quantity of each product)\n", + "\n", + "for item in inventory:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: \n", + "t-shirt: 4\n", + "mug: 9\n", + "hat: 14\n", + "book: 19\n", + "keychain: 24\n" + ] + } + ], + "source": [ + "# Updated inventory\n", + "\n", + "print(\"Updated inventory: \")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +270,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,