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
352 changes: 351 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,356 @@
"\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": 98,
"metadata": {},
"outputs": [],
"source": [
"#1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"."
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"#2. Create an empty dictionary called inventory."
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"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."
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter t-shirt quantity: 7\n",
"Enter mug quantity: 3\n",
"Enter hat quantity: 3\n",
"Enter book quantity: 4\n",
"Enter keychain quantity: 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 7, 'mug': 3, 'hat': 3, 'book': 4, 'keychain': 8}\n"
]
}
],
"source": [
"item = products[0]\n",
"quantity = int(input(f\"Enter {item} quantity: \"))\n",
"inventory[item] = quantity\n",
"\n",
"item = products[1]\n",
"quantity = int(input(f\"Enter {item} quantity: \"))\n",
"inventory[item] = quantity\n",
"\n",
"item = products[2]\n",
"quantity = int(input(f\"Enter {item} quantity: \"))\n",
"inventory[item] = quantity\n",
"\n",
"item = products[3]\n",
"quantity = int(input(f\"Enter {item} quantity: \"))\n",
"inventory[item] = quantity\n",
"\n",
"item = products[4]\n",
"quantity = int(input(f\"Enter {item} quantity: \"))\n",
"inventory[item] = quantity\n",
"\n",
"print(\"Inventory: \" + str(inventory))"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set calledcustomer_orders."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"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."
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain? mug\n",
"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain? hat\n",
"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain? keychain\n"
]
}
],
"source": [
"item1 = input (\"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain?\")\n",
"customer_orders.add(item1)\n",
"\n",
"item2 = input (\"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain?\")\n",
"customer_orders.add(item2)\n",
"\n",
"item3 = input (\"What would you like to order out of a t-shirt, a mug, a hat, a book or a keychain?\")\n",
"customer_orders.add(item3)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"#6. Print the products in the customer_orders set."
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer order: {'hat', 'keychain', 'mug'}\n"
]
}
],
"source": [
"print(\"Customer order: \" + str(customer_orders))"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"#7. Calculate the following order statistics: Total Products Ordered: The total number of products in the customer_orders set. Percentage of Products Ordered: The percentage of products ordered compared to the total available products. Store these statistics in a tuple called order_status."
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"12.0\n",
"5\n"
]
}
],
"source": [
"total_products_ordered = len(customer_orders)\n",
"percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n",
"\n",
"print(total_products_ordered)\n",
"print(percentage_products_ordered)\n",
"print(len(products))\n"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12.0"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"percentage_products_ordered"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"order_status = (\n",
" total_products_ordered,\n",
" percentage_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 12.0)"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order_status"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"#8. Print the order statistics using the following format:"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 12.0\n"
]
}
],
"source": [
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", total_products_ordered)\n",
"print(\"Percentage of Products Ordered:\", percentage_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly."
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 7, 'mug': 2, 'hat': 2, 'book': 4, 'keychain': 7}\n"
]
}
],
"source": [
"inventory[item1] -= 1\n",
"inventory[item2] -= 1\n",
"inventory[item3] -= 1\n",
"\n",
"print(\"Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
"#10. Print the updated inventory, displaying the quantity of each product on separate lines."
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T-shirt: 7\n",
"Mug: 2\n",
"Hat: 2\n",
"Book: 4\n",
"Keychain: 7\n"
]
}
],
"source": [
"print(\"T-shirt:\", inventory[\"t-shirt\"])\n",
"print(\"Mug:\", inventory[\"mug\"])\n",
"print(\"Hat:\", inventory[\"hat\"])\n",
"print(\"Book:\", inventory[\"book\"])\n",
"print(\"Keychain:\", inventory[\"keychain\"])"
]
}
],
"metadata": {
Expand All @@ -68,7 +418,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down