From b9fd3db22ee36b204502cc4ecf6f79d283b1ab75 Mon Sep 17 00:00:00 2001 From: Moci Date: Mon, 15 Jun 2026 17:40:24 +0200 Subject: [PATCH] Week 1 Lab 1 done --- lab-python-data-structures.ipynb | 330 +++++++++++++++++++++++++++---- 1 file changed, 297 insertions(+), 33 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..180c6242 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -1,62 +1,326 @@ { "cells": [ { - "cell_type": "markdown", - "metadata": { - "tags": [] - }, + "cell_type": "code", + "execution_count": null, + "id": "c361112f-13bd-410f-97ef-4a1621104ebd", + "metadata": {}, + "outputs": [], + "source": [ + "#Follow the steps below to complete the exercise:\n", + "\n", + "#Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "#Create an empty dictionary called inventory.\n", + "\n", + "#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" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8c203f4d-6233-47f6-b2df-a8b1fd03bdcd", + "metadata": {}, + "outputs": [], "source": [ - "# Lab | Data Structures " + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] \n", + "inventory = {}\n" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 37, + "id": "0ad5a00e-daa8-4f23-a310-6c99bb888c87", "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of products available 5\n", + "Please enter the quantity of products available 6\n", + "Please enter the quantity of products available 7\n", + "Please enter the quantity of products available 8\n", + "Please enter the quantity of products available 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The inventory available for each item is: {'t-shirt': 9, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], "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", + "for item in products: \n", + " inventory [item] = quantity \n", + " quantity = int(input(\"Please enter the quantity of products available\")) \n", + "print (\"The inventory available for each item is:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4860d906-7171-4dcd-9d74-df04678ccfb4", + "metadata": {}, + "outputs": [], + "source": [ + "#Create an empty set called customer_orders.\n", "\n", - "Follow the steps below to complete the exercise:\n", + "#Ask the user to input the name of three products that a customer wants to order (from those in the products list, \n", + "#meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the customer_orders set.\n", "\n", - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "#Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "a60ac556-5102-4c83-8b31-a47f6984ce89", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the product name mug\n", + "Please enter the product name hat\n", + "Please enter the product name book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer has ordered the following items: {'mug', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set () \n", "\n", - "2. Create an empty dictionary called `inventory`.\n", + "product_1 = input(\"Please enter the product name\")\n", + "product_2 =input(\"Please enter the product name\")\n", + "product_3 = input(\"Please enter the product name\")\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", + "customer_orders.add(product_1)\n", + "customer_orders.add(product_2)\n", + "customer_orders.add(product_3)\n", "\n", - "4. Create an empty set called `customer_orders`.\n", + "print (\"The customer has ordered the following items:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d491c34-1ddc-4c87-8c3e-a99060cd9524", + "metadata": {}, + "outputs": [], + "source": [ + "#Calculate the following order statistics:\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", + "#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", + "#Store these statistics in a tuple called order_status." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "ab479737-0d62-45ad-a001-bc516c2f899a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "The total number of products ordered is: 18\n" + ] + } + ], + "source": [ "\n", - "6. Print the products in the `customer_orders` set.\n", + "print (inventory[\"mug\"])\n", + "print (inventory[\"hat\"])\n", + "print (inventory[\"book\"])\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", + "products_ordered = (inventory[\"mug\"]) + (inventory[\"hat\"]) + (inventory[\"book\"])\n", + "print (\"The total number of products ordered is:\", products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "28ed65c5-815b-4e43-8095-8884e11ffa8f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of products available in the inventory is: 35\n" + ] + } + ], + "source": [ "\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", + "available_inventory = sum (inventory.values())\n", + "print (\"The total number of products available in the inventory is:\", available_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "fcbe67c1-44fa-4d94-b708-e4bf1c1c4552", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The percentage of products ordered is 51.42857142857142 %\n" + ] + } + ], + "source": [ "\n", - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "percentage = (products_ordered/available_inventory)*100\n", + "print (\"The percentage of products ordered is\",percentage,\"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "64433172-eafa-4a73-a948-d38a061a2023", + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (\"customer1\", 18, 51.42)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa6d529a-f4b3-4c65-938b-ce8c4f63ccfd", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a4b14e29-d475-492c-b439-fb4cca78c419", + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "#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": 113, + "id": "36d9ade0-9250-4221-9453-ac586912940f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 9, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "dab65466-328d-46e3-aedc-f04e9f176e50", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 4}\n" + ] + } + ], + "source": [ "\n", - "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + "for key, value in inventory.items (): \n", + " inventory[key] = value - 1\n", + "print (inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "id": "7caaa8d7-46b9-4458-9282-60ac2b64d95b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for keys, value in inventory.items():\n", + " print(inventory [keys])" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e57fb6df-c9a2-40a1-b9f1-b9ae15d8c659", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11f6d58b-8824-4011-87e4-33ac79c1d8c5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "294f7cc1-1e0a-47bf-8d11-9df433b9f0b5", + "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,9 +332,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 5 }