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
167 changes: 165 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,174 @@
"\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": 42,
"metadata": {},
"outputs": [],
"source": [
"# 1. Define list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"# 2. Empty dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 894, 'mug': 428, 'hat': 22, 'book': 478, 'keychain': 22}\n"
]
}
],
"source": [
"# 3. Input quantity of each product available in the inventory\n",
"for i in products:\n",
" quantity = int(input(f\"Quantity of {i}: \"))\n",
" inventory[i] = quantity # In the inventory dict, at key, store this quantity!!!\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"# 4. Create empty set\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'mug', 't-shirt'}\n"
]
}
],
"source": [
"# 5. Input name of 3 products that customer wants to order from products list\n",
"for i in range(0, 3):\n",
" product_choice = input(\n",
" f\"What product do you want to order from these products: {products}?\"\n",
" )\n",
" # Add each product name to customer_orders set\n",
" customer_orders.add(product_choice)\n",
"\n",
"\n",
"# 6. Print products in customer_orders set\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total number of products in the set: 3\n",
"The total percentage ordered is: 60.0\n",
"(3, 60.0)\n"
]
}
],
"source": [
"# 7. Tot num of products in customer_orders set\n",
"total_products_ordered = len(customer_orders)\n",
"print(f\"The total number of products in the set: {total_products_ordered}\")\n",
"\n",
"# 8. Percentage of products ordred compared to tot available products\n",
"percentage_ordered = (total_products_ordered) / (len(products)) * 100\n",
"print(f\"The total percentage ordered is: {percentage_ordered}\")\n",
"\n",
"# Create tuple to add data together\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"print(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of products Ordered: 60.0%\n"
]
}
],
"source": [
"# 8. Print order statistics\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of products Ordered: {order_status[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"# 9. Update inventory subtracting 1 from quantity of each product\n",
"for i in customer_orders:\n",
" inventory[i] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 890\n",
"mug: 424\n",
"hat: 18\n",
"book: 478\n",
"keychain: 22\n"
]
}
],
"source": [
"# 10. Print updated inventory\n",
"for i in inventory:\n",
" print(f\"{i}: {inventory[i]}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +231,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down