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
256 changes: 253 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,263 @@
"\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": 29,
"metadata": {},
"outputs": [],
"source": [
"#Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"#Create an empty dictionary called inventory\n",
"\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the quantity of t-shirts?: 5\n",
"What is the quantity of mugs?: 5\n",
"What is the quantity of hats?: 5\n",
"What is the quantity of books?: 5\n",
"What is the quantity of keychains?: 5\n"
]
}
],
"source": [
"# Ask the user to input the quantity of each product available in the inventory. Use the product names from the products list as keys\n",
"# in the inventory dictionary and assign the respective quantities as values.\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"What is the quantity of {product}s?: \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# Create empty set\n",
"\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product: hat\n",
"Enter a product: muh\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not in list.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product: mug\n",
"Enter a product: keychain\n"
]
}
],
"source": [
"# Ask the user to input the name of three products that a customer wants to order (from those in products list).\n",
"# Add each product name to the customer_orders set.\n",
"\n",
"counter = 3\n",
"\n",
"while counter > 0:\n",
" \n",
" three_products = input(\"Enter a product: \")\n",
"\n",
" if three_products in products:\n",
" customer_orders.add(three_products)\n",
" counter -=1\n",
" else:\n",
" print(\"Product not in list.\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'keychain', 'mug'}\n"
]
}
],
"source": [
"# Print the products in the customer_orders set.\n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# Calculate the following order statistics:\n",
"# Total Products Ordered: The total number of products in the customer_orders set.\n",
"\n",
"total_products_ordered = sum(inventory[product] for product in customer_orders)\n",
"print(total_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# Percentage of products ordered: The percentage of products ordered compared to the total available products.\n",
"\n",
"total_available_products = sum(inventory.values())\n",
"percentage_products = (total_products_ordered / total_available_products) * 100"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(15, 60.0)\n"
]
}
],
"source": [
"# Store these statistics in a tuple called order_status.\n",
"\n",
"order_status = (total_products_ordered, percentage_products)\n",
"\n",
"print(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order statistics:\n",
"Total products ordered: 15\n",
"Percentage of products ordered: 60.0\n"
]
}
],
"source": [
"# Print the order statistics using the following format:\n",
"# Order statistics:\n",
"# Total products ordered: <total_products_ordered>\n",
"# Percentage of products ordered: <percentage_ordered>\n",
"\n",
"print(\"Order statistics:\")\n",
"print(\"Total products ordered: \", total_products_ordered)\n",
"print(\"Percentage of products ordered: \", percentage_products)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 4}\n"
]
}
],
"source": [
"# Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"\n",
"for product in inventory:\n",
" inventory[product] -= 1\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt : 4\n",
"mug : 4\n",
"hat : 4\n",
"book : 4\n",
"keychain : 4\n"
]
}
],
"source": [
"# Print the updated inventory , displaying the quantity of each product on separate lines.\n",
"\n",
"for product, quantity in inventory.items():\n",
" print(product,\":\", quantity)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +318,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down