Skip to content

lab done #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
233 changes: 230 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,240 @@
"\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": 33,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt 5\n",
"Please enter the quantity of mug 6\n",
"Please enter the quantity of hat 7\n",
"Please enter the quantity of book 8\n",
"Please enter the quantity of keychain 9\n"
]
}
],
"source": [
"#1.Define a list \n",
"product_1 = \"t-shirt\"\n",
"product_2 = \"mug\"\n",
"product_3 = \"hat\"\n",
"product_4 = \"book\"\n",
"product_5 = \"keychain\"\n",
"\n",
"products = [product_1, product_2, product_3, product_4, product_5]\n",
"products\n",
"\n",
"#2.Create an empty dictionary called inventory\n",
"inventory = {}\n",
"\n",
"#3.1.Ask the user to input the quantity of each product available in the inventory.\n",
"question_1 = int(input(f\"Please enter the quantity of {product_1}\"))\n",
"question_2 = int(input(f\"Please enter the quantity of {product_2}\"))\n",
"question_3 = int(input(f\"Please enter the quantity of {product_3}\"))\n",
"question_4 = int(input(f\"Please enter the quantity of {product_4}\"))\n",
"question_5 = int(input(f\"Please enter the quantity of {product_5}\"))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 8, 'keychain': 9}\n"
]
}
],
"source": [
"#3.2.Use the product names from the products list as keys in the inventory dictionary \n",
"#and assign the respective quantities as values.\n",
"\n",
"inventory = {\n",
" product_1: question_1,\n",
" product_2: question_2,\n",
" product_3: question_3,\n",
" product_4: question_4,\n",
" product_5: question_5,\n",
"}\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"#4.Create an empty set called customer_orders\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the first product: mug\n",
"Enter the name of the second product: book\n",
"Enter the name of the third product: hat\n"
]
}
],
"source": [
"#5.1.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\" \n",
"#or \"keychain\". \n",
"\n",
"order1 = input(\"Enter the name of the first product: \").strip().lower()\n",
"order2 = input(\"Enter the name of the second product: \").strip().lower()\n",
"order3 = input(\"Enter the name of the third product: \").strip().lower()\n",
"\n",
"# Add only valid products to the set\n",
"for order in [order1, order2, order3]:\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"'{order}' is not a valid product.\")\n",
"\n",
"#Add each product name to the customer_orders set.\n",
"customer_orders = set()\n",
"customer_orders.add(order1)\n",
"customer_orders.add(order2)\n",
"customer_orders.add(order3)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'hat', 'mug', 'book'}\n"
]
}
],
"source": [
"#5.2.Add each product name to the customer_orders set.\n",
"print(\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"#Total Products Ordered: The total number of products in the customer_orders set.\n",
"print(len(customer_orders))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"#7.Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered/len(products))*100\n",
"\n",
"order_stats = (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: \n",
"Total Products Ordered: 3\n",
"Percentage of Unique products ordered: 60.0\n"
]
}
],
"source": [
"#8.Print the order statistics using the following format:\n",
"\n",
"print(\"Order Statistics: \")\n",
"print(\"Total Products Ordered: \", order_stats[0])\n",
"print(\"Percentage of Unique products ordered: \", order_stats[1])"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"#9.Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"\n",
"product_1 = list(customer_orders)[0]\n",
"inventory[product_1] -=1\n",
"\n",
"product_2 = list(customer_orders)[1]\n",
"inventory[product_2] -=1\n",
"\n",
"product_3 = list(customer_orders)[2]\n",
"inventory[product_3] -=1\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 9}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#10.Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"inventory"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +295,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down