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
232 changes: 231 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,236 @@
"\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": 2,
"metadata": {},
"outputs": [],
"source": [
"# 1. Define a list called `products` that contains the following items: \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#2. Create an empty dictionary called `inventory`\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 5\n",
"Enter quantity for mug: 3\n",
"Enter quantity for hat: 5\n",
"Enter quantity for book: 7\n",
"Enter quantity for keychain: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 3, 'hat': 5, 'book': 7, 'keychain': 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",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set called `customer_orders`."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'mug'}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'mug', 'hat'}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: sandals\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"sandals is not available.\n",
"Customer orders: {'mug', 'hat'}\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",
"for i in range(3):\n",
" order = input(\"Enter a product to order: \")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"{order} is not available.\")\n",
"\n",
" print (\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products in customer_orders:\n",
"- mug\n",
"- hat\n"
]
}
],
"source": [
"#6. Print the products in the `customer_orders` set.\n",
"print (\"Products in customer_orders:\")\n",
"for item in customer_orders:\n",
" print(\"-\", item)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order status tuple: (2, 40.0)\n"
]
}
],
"source": [
"#7. Calculate the following order statistics:\n",
"#Total Products Ordered (unique items in the set)\n",
"total_products_ordered = len(customer_orders)\n",
"#Percentage of Products Ordered compared to the total available products.\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"#Store results in a tuple\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"print(\"Order status tuple:\", order_status)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n"
]
}
],
"source": [
"#8. Print the order statistics using the following format:\n",
"print (\"Order Statistics:\")\n",
"print (\"Total Products Ordered:\", order_status[0])\n",
"print (\"Percentage of Products Ordered: {:.2f}%\".format(order_status[1]))\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: {'t-shirt': 5, 'mug': 0, 'hat': 2, 'book': 7, 'keychain': 5}\n"
]
}
],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"Updated inventory:\", inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +298,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down