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
325 changes: 322 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,332 @@
"\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": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"type (products)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#2. Create an empty dictionary called `inventory`.\n",
"inventory = {}\n",
"type (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of t-shirts: 4\n",
"Enter the number of mugs: 5\n",
"Enter the number of hats: 6\n",
"Enter the number of books: 7\n",
"Enter the number of keychains: 8\n"
]
}
],
"source": [
"#3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the\n",
"#`products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"\n",
"tshirt_quantity= int(input(\"Enter the number of t-shirts: \"))\n",
"mug_quantity= int(input(\"Enter the number of mugs: \"))\n",
"hat_quantity= int(input(\"Enter the number of hats: \"))\n",
"book_quantity= int(input(\"Enter the number of books: \"))\n",
"keychain_quantity= int(input(\"Enter the number of keychains: \"))\n",
"\n",
"inventory = {\"t-shirt\":tshirt_quantity, \"mug\":mug_quantity, \"hat\":hat_quantity, \"book\":book_quantity, \"keychain\":keychain_quantity}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set called `customer_orders`.\n",
"\n",
"customer_orders= {}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What's the first product that you'd like to order: mug\n",
"What's the second product that you'd like to order: book\n",
"What's the third product that you'd like to order: hat\n"
]
}
],
"source": [
"#5. Ask the user to input the name of three products that a customer wants to order \n",
"#(from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". \n",
"#Add each product name to the `customer_orders` set.\n",
"\n",
"order1=input(\"What's the first product that you'd like to order: \")\n",
"order2=input(\"What's the second product that you'd like to order: \")\n",
"order3=input(\"What's the third product that you'd like to order: \")\n",
"\n",
"customer_orders= {order1,order2,order3}\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(customer_orders)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"#6. Print the products in the `customer_orders` set.\n",
"\n",
"print (customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"#7. Calculate the following order statistics: \n",
"#Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"\n",
"number_of_orders=len(customer_orders)\n",
"print(number_of_orders)\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60.0 %\n"
]
}
],
"source": [
"# Calculate the following order statistics: \n",
"# Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"\n",
"percentage_of_orders= number_of_orders/5*100\n",
"print((percentage_of_orders) , '%')\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Store these statistics in a tuple called `order_status`.\n",
"\n",
"order_status=(number_of_orders,percentage_of_orders)\n",
"type(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: Total Products Ordered:3, Percentage of Products Ordered:60.0 %\n"
]
}
],
"source": [
"#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",
"text= f\"Order Statistics: Total Products Ordered:{number_of_orders}, Percentage of Products Ordered:{percentage_of_orders} %\"\n",
"print(text)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. \n",
"#Modify the `inventory` dictionary accordingly.\n",
"\n",
"inventory = {\"t-shirt\":tshirt_quantity-1, \"mug\":mug_quantity-1, \"hat\":hat_quantity-1, \"book\":book_quantity-1, \"keychain\":keychain_quantity-1}\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 3, 'mug': 4, 'hat': 5, 'book': 6, 'keychain': 7}\n"
]
}
],
"source": [
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"Update Inventory:\")\n",
"print(\"T-shirts:\", inventory[\"t-shirt\"])\n",
"print(\"Mugs:\",inventory[\"mug\"])\n",
"print(\"Hats:\",inventory[\"hat\"])\n",
"print(\"Books:\",inventory[\"book\"])\n",
"print(\"Keychains:\",inventory[\"keychain\"])\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:Anaconda]",
"language": "python",
"name": "python3"
"name": "conda-env-Anaconda-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +387,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down