Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions lab-python-list-comprehension (2).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | List, Dict and Set Comprehension"
]
},
{
"cell_type": "markdown",
"id": "7dd3cbde-675a-4b81-92c3-f728846dbe06",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized with Comprehension"
]
},
{
"cell_type": "markdown",
"id": "5d500160-2fb7-4777-b5e4-09d45ebaf328",
"metadata": {},
"source": [
"In the previous exercise, you developed a program to manage customer orders and inventory. Now, let's take it a step further and incorporate comprehension into your code.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n",
"\n",
" - *Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n",
" \n",
" - For example, in initializing the inventory, we could have:\n",
" \n",
" ```python\n",
" def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"\n",
" ```\n",
"<br>\n",
" \n",
" \n",
"2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n",
"\n",
"3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n",
"\n",
"4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n",
"\n",
"5. Print the total price of the customer order.\n",
"\n",
"Your code should produce output similar to the following:\n",
"\n",
"```python\n",
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: 3\n",
"Enter the quantity of books available: 2\n",
"Enter the quantity of keychains available: 1\n",
"Enter the number of customer orders: 2\n",
"Enter the name of a product that a customer wants to order: hat\n",
"Enter the name of a product that a customer wants to order: keychain\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n",
"Enter the price of keychain: 5\n",
"Enter the price of hat: 10\n",
"Total Price: 15.0\n",
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b6636b3e",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1327d4f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'keychain', 'hat'}\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n",
"keychain: 0\n",
"Order statistics: (2, 40.0)\n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n"
]
}
],
"source": [
"#1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n",
"#Apply it to initialize inventory, updating the inventory and printing the updated inventory. For example, in initializing the inventory, we could have:\n",
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"inventory = initialize_inventory(products)\n",
"\n",
"#Updating the inventory could be:\n",
"\n",
"customer_orders = set()\n",
"while True:\n",
" product_name = input(\"Enter the name of a product that a customer wants to order: \")\n",
" customer_orders.add(product_name)\n",
" if len(customer_orders) == 2:\n",
" break\n",
"print(\"Customer orders:\", customer_orders)\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
" return\n",
"print_updated_inventory(inventory)\n",
"\n",
"#Calculating order statistics could be:\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_unique_ordered\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print(\"Order statistics:\", order_statistics)\n",
"\n",
"#Printing order statistics could be:\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_unique_ordered = order_statistics\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_unique_ordered:.2f}%\")\n",
" return\n",
"print_order_statistics(order_statistics)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}