From fc2ef4ed1e57c24cd7d527095d45cbcef79cff90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Gomes?= Date: Sat, 11 Oct 2025 15:50:08 +0200 Subject: [PATCH] Done with the lab Comprehension --- lab-python-list-comprehension.ipynb | 70 ++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..5260b5f 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,77 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3cf16bcc", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#{key: value for item in iterable} \n", + "#You are constructing the entire dictionary in one expression, not performing assignments.\n", + "#So instead of “assign this value to this key,” you’re defining pairs of key and value.\n", + "def initialize_inventory (products):\n", + " inventory = {product: int(input(f\"Insert Quantity of {product}s\")) for product in products}\n", + " \n", + " return inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "#{value for item in iterable}\n", + "def get_customer_orders():\n", + " num_orders = int(input(f\"How many unique products from the products list do you want?\"))\n", + "\n", + " customer_orders = {input(f\"Select a product from the products list: \").lower() for amount in range(num_orders)}\n", + " \n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {item: inventory[item] - 1 for item in customer_orders if inventory[item] > 1}\n", + "\n", + " return inventory\n", + " \n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "def calculate_total_price_customer_orders(inventory):\n", + " total_price_customer_orders = {item: float(input(f\"Insert the price of the {item}: \")) for item in inventory}\n", + " total_price_customer_orders = sum(total_price_customer_orders.values())\n", + " print(f\"The total price is: {total_price_customer_orders}\")\n", + " return total_price_customer_orders\n", + "\n", + "total_price = calculate_total_price_customer_orders(inventory)\n", + "\n", + "def calculate_order_statistics(inventory, products):\n", + " total_products_ordered = len(inventory)\n", + " total_available_products = len(products)\n", + " percentage_ordered = (total_products_ordered / total_available_products) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "order_statistics = calculate_order_statistics(inventory, products)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Total unique products ordered:\", total_products_ordered)\n", + " print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "def print_updated_inventory(inventory):\n", + " {print(f\"{product}s: {quantity}\") for product, quantity in inventory.items()}\n", + "\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -93,7 +159,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,