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
273 changes: 231 additions & 42 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
@@ -1,55 +1,244 @@
{
"cells": [
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 1,
"id": "162b2d52-43da-4965-9d21-0f0ae57df848",
"metadata": {},
"outputs": [],
"source": [
"#1. Defining the products list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5a46c13f-2930-40ab-bfc3-812f0aad017c",
"metadata": {},
"outputs": [],
"source": [
"#2. Creating an empty dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5a7bd667-5c2b-4c9b-9978-85a38695cdfe",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt 15\n",
"Enter the quantity of mug 28\n",
"Enter the quantity of hat 34\n",
"Enter the quantity of book 9\n",
"Enter the quantity of keychain 12\n"
]
}
],
"source": [
"#3. Filling in the inventory database\n",
"for item in products:\n",
" quantity = int(input(f\"Enter the quantity of {item}\"))\n",
" inventory[item] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8a1466d2-9a66-476d-8326-bc1d2c771952",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 15, 'mug': 28, 'hat': 34, 'book': 9, 'keychain': 12}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "35d67f0a-487e-4cc5-a317-02f390b9646f",
"metadata": {},
"outputs": [],
"source": [
"#4.Creating the empty set customer_order\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6ea98601-e1b0-46c4-b436-004420fcdb87",
"metadata": {
"tags": []
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a product book\n",
"Choose a product hat\n",
"Choose a product mug\n"
]
}
],
"source": [
"# Lab | Data Structures "
"#5. Setting the custom order\n",
"print(f\"Available products: {products}\")\n",
"for i in range(3):\n",
" order = input(\"Choose a product \")\n",
" customer_orders.add(order)"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 7,
"id": "df809e59-b776-4c03-8175-9b082b83feb7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'book', 'hat'}\n"
]
}
],
"source": [
"## Exercise: Managing Customer Orders\n",
"\n",
"As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"2. Create an empty dictionary called `inventory`.\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",
"\n",
"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",
"\n",
"6. Print the products in the `customer_orders` set.\n",
"\n",
"7. Calculate the following order statistics:\n",
" - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
" - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" \n",
" Store these statistics in a tuple called `order_status`.\n",
"\n",
"8. Print the order statistics using the following format:\n",
" ```\n",
" Order Statistics:\n",
" Total Products Ordered: <total_products_ordered>\n",
" Percentage of Products Ordered: <percentage_ordered>% \n",
" ```\n",
"\n",
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
"#6 Getting the order\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2a699da7-e193-4978-b393-4e277cdc19ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"#7. Calculating the total of the kind of products ordered\n",
"total_products_ordered = len(customer_orders)\n",
"print(total_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6b81f427-d93c-4ae9-b26d-a4d5d0931288",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60.0\n"
]
}
],
"source": [
"#8. Percentage of products ordered (#part and the whole)\n",
"percentage_products_ordered = total_products_ordered / len(products) *100\n",
"print(percentage_products_ordered)\n",
"#The customer has ordered 60% of the store's lineup"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "501e092f-d514-49a6-ae19-edc8036c86f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 60.0)\n"
]
}
],
"source": [
"order_status = (total_products_ordered, percentage_products_ordered)\n",
"print(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d4bb09e1-7c62-4d84-b20d-05868d89ea3b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 15, 'mug': 27, 'hat': 33, 'book': 8, 'keychain': 12}\n"
]
}
],
"source": [
"#9.Updating the inventory\n",
"for item in customer_orders:\n",
" inventory[item] = inventory[item] - 1\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "d6cd2a9a-02a0-46a1-bc38-f055409cf9cb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt, 15\n",
"mug, 27\n",
"hat, 33\n",
"book, 8\n",
"keychain, 12\n"
]
}
],
"source": [
"for item, quantity in inventory.items():\n",
" print(f\"{item}, {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2354965-f883-450e-b3d5-271059496bda",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,9 +257,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 5
}