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
155 changes: 155 additions & 0 deletions lab-python-data-structures (1).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"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: <total_products_ordered>\n",
" Percentage of Products Ordered: <percentage_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": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add amount for hat: 3\n",
"Add amount for t-shirt: 4\n",
"Add amount for mug: 6\n",
"Add amount for book: 8\n",
"Add amount for keychain: 4\n",
"Enter a selected item: book\n",
"Enter a selected item: mug\n",
"Enter a selected item: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"customer_orders: {'book', 'mug', 'hat'}\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n",
"Updated Inventory:\n",
"hat: 2\n",
"t-shirt: 4\n",
"mug: 5\n",
"book: 7\n",
"keychain: 4\n"
]
}
],
"source": [
"# Create a list called 'products' to store product names as strings inside of [].\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\",]\n",
"# Create an empty dictionary{} called 'inventory' to hold the product names(key) and the quantity(value).\n",
"inventory = {}\n",
"# Using int() before input() to ask the user to add an amount to the items and convert the answer into an integer. \n",
"inventory[\"hat\"] = int(input(\"Add an amount for hat: \"))\n",
"inventory[\"t-shirt\"] = int(input(\"Add an amount for t-shirt: \"))\n",
"inventory[\"mug\"] = int(input(\"Add an amount for mug: \"))\n",
"inventory[\"book\"] = int(input(\"Add an amount for book: \"))\n",
"inventory[\"keychain\"] = int(input(\"Add an amount for keychain: \"))\n",
"# Create an empty set() called customer_orders.\n",
"customer_orders = set()\n",
"# Use .add() to ask the user to input() 3 items to the set.\n",
"customer_orders.add(input(\"Enter a selected item: \"))\n",
"customer_orders.add(input(\"Enter a selected item: \"))\n",
"customer_orders.add(input(\"Enter a selected item: \"))\n",
"# Use print() to display results.\n",
"print(\"customer_orders: \", customer_orders)\n",
"# Calculate total number using len() and percentage using * 100 of products ordered to get the percentage.\n",
"total_ordered = len(customer_orders)\n",
"percentage_ordered = (total_ordered / len(products)) * 100 \n",
"# Store answers in a tuple() called order_status.\n",
"order_status = (total_ordered, percentage_ordered)\n",
"# Use print to display results.\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", order_status[0])\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n",
"# Use -= to subtract from the inventory by 1 from each item in customer_orders based on its boolean value(True/False).\n",
"inventory[\"hat\"] -= (\"hat\" in customer_orders) \n",
"inventory[\"t-shirt\"] -= (\"t-shirt\" in customer_orders)\n",
"inventory[\"mug\"] -= (\"mug\" in customer_orders)\n",
"inventory[\"book\"] -= (\"book\" in customer_orders)\n",
"inventory[\"keychain\"] -= (\"keychain\" in customer_orders)\n",
"# Use print to display the updated inventory on separate lines.\n",
"print(\"Updated Inventory:\")\n",
"print(\"hat:\", inventory[\"hat\"])\n",
"print(\"t-shirt:\", inventory[\"t-shirt\"])\n",
"print(\"mug:\", inventory[\"mug\"])\n",
"print(\"book:\", inventory[\"book\"])\n",
"print(\"keychain:\", inventory[\"keychain\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "anaconda-2025.12-py312",
"language": "python",
"name": "conda-env-anaconda-2025.12-py312-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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}