From 40a7878f34cf5583ed19263513cf71e613a0290b Mon Sep 17 00:00:00 2001 From: Francisco Date: Mon, 15 Jun 2026 17:39:14 +0200 Subject: [PATCH] solving Lab day 1 --- ...ab-python-data-structures-checkpoint.ipynb | 470 ++++++++++++++++++ lab-python-data-structures.ipynb | 400 ++++++++++++++- 2 files changed, 867 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..401ed444 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,470 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 1 # list\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "products" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 2 # Dictionary\n", + "inventory = {}\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of available T-shirts: 1\n", + "Enter the quantity of available mugs: 2\n", + "Enter the quantity of available hats: 3\n", + "Enter the quantity of available books: 4\n", + "Enter the quantity of available keychains: 5\n" + ] + } + ], + "source": [ + "# 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", + "q_tshirt = int(input(\"Enter the quantity of available T-shirts: \"))\n", + "q_mug = int(input(\"Enter the quantity of available mugs: \"))\n", + "q_hat = int(input(\"Enter the quantity of available hats: \"))\n", + "q_book = int(input(\"Enter the quantity of available books: \"))\n", + "q_keychain = int(input(\"Enter the quantity of available keychains: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q_hat" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {products[0]:q_tshirt, products[1]:q_mug, products[2]:q_hat, products[3]:q_book, products[4]:q_keychain }" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + }, + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(inventory)\n", + "type(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4 # Create an empty set called customer_orders\n", + "customer_orders = set()\n", + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the first product of the order: mug\n", + "Enter the name of the first product of the order: hat\n", + "Enter the name of the first product of the order: book\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\". Add each product name to the `customer_orders` set.\n", + "ordered_product_1 = input(\"Enter the name of the first product of the order: \")\n", + "ordered_product_2 = input(\"Enter the name of the second product of the order: \")\n", + "ordered_product_3 = input(\"Enter the name of the third product of the order: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders.add(ordered_product_1)\n", + "customer_orders.add(ordered_product_2)\n", + "customer_orders.add(ordered_product_3)\n", + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# 6 #\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + }, + { + "data": { + "text/plain": [ + "60.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 7 #\n", + "\"\"\" 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", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "pct_products_ordered = 100*total_products_ordered/len(products)\n", + "pct_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3, 60.0)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_status = (total_products_ordered, pct_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "# 8 # Print the order statistics using the following format:\n", + "'''\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " '''\n", + "# print(\"Your age after 10 years:\",age_after_10y)\n", + "# message = f\"Your age after 10 years is {age_after_10y}\"\n", + "\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {pct_products_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "# 9 # Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "inventory['t-shirt'] = q_tshirt - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "inventory['mug'] = q_mug - 1\n", + "inventory['hat'] = q_hat - 1\n", + "inventory['book'] = q_book - 1\n", + "inventory['keychain'] = q_keychain - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 4}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in inventory: dict_keys(['t-shirt', 'mug', 'hat', 'book', 'keychain'])\n", + "Quantities in inventory: dict_values([0, 1, 2, 3, 4])\n" + ] + } + ], + "source": [ + "# 10. # Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(f\"Products in inventory: {inventory.keys()}\")\n", + "print(f\"Quantities in inventory: {inventory.values()}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..401ed444 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -9,6 +9,13 @@ "# Lab | Data Structures " ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -50,13 +57,400 @@ "\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": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 1 # list\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "products" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 2 # Dictionary\n", + "inventory = {}\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of available T-shirts: 1\n", + "Enter the quantity of available mugs: 2\n", + "Enter the quantity of available hats: 3\n", + "Enter the quantity of available books: 4\n", + "Enter the quantity of available keychains: 5\n" + ] + } + ], + "source": [ + "# 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", + "q_tshirt = int(input(\"Enter the quantity of available T-shirts: \"))\n", + "q_mug = int(input(\"Enter the quantity of available mugs: \"))\n", + "q_hat = int(input(\"Enter the quantity of available hats: \"))\n", + "q_book = int(input(\"Enter the quantity of available books: \"))\n", + "q_keychain = int(input(\"Enter the quantity of available keychains: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q_hat" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {products[0]:q_tshirt, products[1]:q_mug, products[2]:q_hat, products[3]:q_book, products[4]:q_keychain }" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + }, + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(inventory)\n", + "type(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4 # Create an empty set called customer_orders\n", + "customer_orders = set()\n", + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the first product of the order: mug\n", + "Enter the name of the first product of the order: hat\n", + "Enter the name of the first product of the order: book\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\". Add each product name to the `customer_orders` set.\n", + "ordered_product_1 = input(\"Enter the name of the first product of the order: \")\n", + "ordered_product_2 = input(\"Enter the name of the second product of the order: \")\n", + "ordered_product_3 = input(\"Enter the name of the third product of the order: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders.add(ordered_product_1)\n", + "customer_orders.add(ordered_product_2)\n", + "customer_orders.add(ordered_product_3)\n", + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# 6 #\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + }, + { + "data": { + "text/plain": [ + "60.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 7 #\n", + "\"\"\" 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", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "pct_products_ordered = 100*total_products_ordered/len(products)\n", + "pct_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3, 60.0)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_status = (total_products_ordered, pct_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "# 8 # Print the order statistics using the following format:\n", + "'''\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " '''\n", + "# print(\"Your age after 10 years:\",age_after_10y)\n", + "# message = f\"Your age after 10 years is {age_after_10y}\"\n", + "\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {pct_products_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "# 9 # Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "inventory['t-shirt'] = q_tshirt - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "inventory['mug'] = q_mug - 1\n", + "inventory['hat'] = q_hat - 1\n", + "inventory['book'] = q_book - 1\n", + "inventory['keychain'] = q_keychain - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 4}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in inventory: dict_keys(['t-shirt', 'mug', 'hat', 'book', 'keychain'])\n", + "Quantities in inventory: dict_values([0, 1, 2, 3, 4])\n" + ] + } + ], + "source": [ + "# 10. # Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(f\"Products in inventory: {inventory.keys()}\")\n", + "print(f\"Quantities in inventory: {inventory.values()}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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,7 +462,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,