Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.ipynb_checkpoints
309 changes: 308 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,313 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\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."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of t-shirt: 2\n",
"Enter the number of mug: 3\n",
"Enter the number of hat: 4\n",
"Enter the number of book: 56\n",
"Enter the number of keychain: 8\n"
]
}
],
"source": [
"for item in products:\n",
" quantity = int(input(f\"Enter the number of {item}: \"))\n",
" inventory[item] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 2, 'mug': 3, 'hat': 4, 'book': 56, 'keychain': 8}"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Name 1 product: from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] list: book\n",
"Name 1 product: from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] list: keychain\n",
"Name 1 product: from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] list: mug\n"
]
}
],
"source": [
"for item in range(3):\n",
" order = input(f\"Name 1 product: from {products} list: \")\n",
" customer_orders.add(order)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'keychain', 'mug'}"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
" ```"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"total_orders = len(customer_orders)\n",
"\n",
"percentage = (total_orders / len(products) * 100)\n",
"\n",
"order_status = (total_orders, percentage)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order_status"
]
},
{
"cell_type": "code",
"execution_count": 69,
"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(f\"\"\"Order Statistics:\n",
"Total Products Ordered: {order_status[0]}\n",
"Percentage of Products Ordered: {order_status[1]}%\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 2, 'mug': 2, 'hat': 4, 'book': 55, 'keychain': 7}"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'keychain', 'mug'}"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"for item in customer_orders:\n",
" inventory[item] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 2, 'mug': 1, 'hat': 4, 'book': 54, 'keychain': 6}"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 2\n",
"mug: 1\n",
"hat: 4\n",
"book: 54\n",
"keychain: 6\n"
]
}
],
"source": [
"for keys, values in inventory.items():\n",
" print(f\"{keys}: {values}\")"
]
}
],
"metadata": {
Expand All @@ -68,7 +375,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down