From 305214437e1e2c4da7bffae80699efaeef40f469 Mon Sep 17 00:00:00 2001 From: Rachel Vianna Date: Wed, 18 Feb 2026 18:09:45 +0100 Subject: [PATCH] Solved Lab: Python Data Structures --- ...ab-python-data-structures-checkpoint.ipynb | 298 ++++++++++++++++++ lab-python-data-structures.ipynb | 228 +++++++++++++- 2 files changed, 523 insertions(+), 3 deletions(-) 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..ce85ffde --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,298 @@ +{ + "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": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Create an empty dictionary called inventory.\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 391, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 500\n", + "Please input the amount of hats: 300\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1350\n" + ] + } + ], + "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.\n", + "\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called customer_orders.\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: mug\n", + "Please input the product you wish to order: book\n", + "Please input the product you wish to order: hat\n" + ] + } + ], + "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\".\n", + "# Add each product name to the customer_orders set.\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 271, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer orders: mug, hat, book\n" + ] + } + ], + "source": [ + "# 6. Print the products in the customer_orders set.\n", + "\n", + "print(f\"Products in customer orders: {', '.join(customer_orders)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 337, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 3\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics:\n", + "# Total Products Ordered: The total number of products in the customer_orders set.\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "print(f\"Total products ordered: {total_products_ordered}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 355, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Percentage of products ordered: 15%\n" + ] + } + ], + "source": [ + "# Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + "\n", + "percentage_of_products_ordered = (total_products_ordered/inventory_amount) *100\n", + "print(f\"Percentage of products ordered: {percentage_of_products_ordered:.0f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 385, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics:\n", + "Total products ordered: 3\n", + "Percentage of products ordered: 15.00%\n" + ] + } + ], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# Order Statistics:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % \n", + "\n", + "print(\"Order statistics:\")\n", + "print(f\"Total products ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of products ordered: {percentage_of_products_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 369, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 401, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory:\n", + "Remaining t-shirt: 400\n", + "Remaining mug: 500\n", + "Remaining hat: 300\n", + "Remaining book: 100\n", + "Remaining keychain: 50\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "print(\"Updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"Remaining {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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..ce85ffde 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,235 @@ "\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": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Create an empty dictionary called inventory.\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 391, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 500\n", + "Please input the amount of hats: 300\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1350\n" + ] + } + ], + "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.\n", + "\n", + "for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + "total_items = sum(inventory.values())\n", + "print(f\"The total number of items in stock is: {total_items}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called customer_orders.\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the product you wish to order: mug\n", + "Please input the product you wish to order: book\n", + "Please input the product you wish to order: hat\n" + ] + } + ], + "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\".\n", + "# Add each product name to the customer_orders set.\n", + "\n", + "customer_orders = set()\n", + "for product in range(3):\n", + " orders = input(f\"Please input the product you wish to order:\")\n", + " if orders in products:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 271, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer orders: mug, hat, book\n" + ] + } + ], + "source": [ + "# 6. Print the products in the customer_orders set.\n", + "\n", + "print(f\"Products in customer orders: {', '.join(customer_orders)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 337, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 3\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics:\n", + "# Total Products Ordered: The total number of products in the customer_orders set.\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "print(f\"Total products ordered: {total_products_ordered}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 355, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Percentage of products ordered: 15%\n" + ] + } + ], + "source": [ + "# Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + "\n", + "percentage_of_products_ordered = (total_products_ordered/inventory_amount) *100\n", + "print(f\"Percentage of products ordered: {percentage_of_products_ordered:.0f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 385, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics:\n", + "Total products ordered: 3\n", + "Percentage of products ordered: 15.00%\n" + ] + } + ], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# Order Statistics:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % \n", + "\n", + "print(\"Order statistics:\")\n", + "print(f\"Total products ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of products ordered: {percentage_of_products_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 369, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 401, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory:\n", + "Remaining t-shirt: 400\n", + "Remaining mug: 500\n", + "Remaining hat: 300\n", + "Remaining book: 100\n", + "Remaining keychain: 50\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "print(\"Updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"Remaining {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 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,