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
191 changes: 189 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,198 @@
"\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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# step 1: Defining the list # list\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# step 2: creating an empty dictionary # dictionary\n",
"\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"# step 3: Asking the user to enter the quantity of each product available in the inventory # for loop\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Please enter the quantity of {products}: \"))\n",
" inventory[product] = quantity # In the dictionary inventory, create (or update) an entry where the key is product and the value is quantity\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# step 4: creating an empty set # set()\n",
"\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat', 't-shirt', 'book,', 'keychain', 'mug'}\n"
]
}
],
"source": [
"# step 5: Asking user to enter the name of 3 products that they want to order\n",
"\n",
"for i in range(3):\n",
" order = input(f\"Please enter product {i+1} that you want to order. Attention! Please choose from {products} list: \")\n",
" customer_orders.add(order)\n",
"\n",
"# Step 6: Printing products in the set \n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"#Step 7: Calculate total products ordered\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"print(total_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120.0\n"
]
}
],
"source": [
"percentage_of_ordered_products = (total_products_ordered / len(products)) * 100\n",
"print(percentage_of_ordered_products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 6\n",
"Percentage of Products Ordered: 120.0%\n"
]
}
],
"source": [
"# Step 8: Prining the statistics\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_of_ordered_products}%\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Step 9: Updating Inventory\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory is: \n",
"t-shirt: -2\n",
"mug: -1\n",
"hat: 0\n",
"book: 1\n",
"keychain: 2\n"
]
}
],
"source": [
"# Step 10: Print the updated Inventory\n",
"print(\"Updated inventory is: \")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +255,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down