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
146 changes: 143 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Exercise: Managing Customer Orders\n",
"\n",
Expand All @@ -21,8 +23,10 @@
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"\n",
"2. Create an empty dictionary called `inventory`.\n",
"\n",
"\n",
"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",
"\n",
"4. Create an empty set called `customer_orders`.\n",
Expand Down Expand Up @@ -50,11 +54,147 @@
"\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": 6,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the number of {product} available in the inventory\"))\n",
" inventory[product] = quantity #name_of_the_dictionary(key) = output variable (or) value for the key\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 76, 'mug': 22, 'hat': 81, 'book': 60, 'keychain': 3}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"order = input(f\"Please enter the name of any three products from {products} you want to order:\")\n",
"final_order = [item.strip() for item in order.split(\",\")] \n",
"#without stripping and splitting, the items either appear duplicated or with spaces\n",
"#so strip function is carried out for each item that is split\n",
"customer_orders = set(final_order)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 't-shirt', 'book'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"total_products_ordered = len(customer_orders)\n",
"percentage_of_the_products_ordered = (total_products_ordered / (len(products)))*100\n",
"order_status = (total_products_ordered, percentage_of_the_products_ordered) "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered:\", int(total_products_ordered))\n",
"print(f\"Percentage of Products Ordered:\", str(percentage_of_the_products_ordered) + '%')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"inventory = {'t-shirt': 76, 'mug': 22, 'hat': 81, 'book': 60, 'keychain': 3}\n",
"\n",
"for product, quantity in inventory.items():\n",
" inventory[product] = quantity -1 #dictionary_name[key] = value -1 (this will assign the value to the respective keys)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 75\n",
"mug: 21\n",
"hat: 80\n",
"book: 59\n",
"keychain: 2\n"
]
}
],
"source": [
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +208,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down