From b322b19b2fd2bc4bcf46692304a37918215d7633 Mon Sep 17 00:00:00 2001 From: Zina B Date: Sat, 22 Nov 2025 09:15:09 +0100 Subject: [PATCH] Lab Solved --- lab-python-data-structures.ipynb | 222 ++++++++++++++++++++++++++++++- 1 file changed, 219 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..14acc1c6 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,229 @@ "\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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#1 Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#2 Create an empty dictionary called inventory.\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the quantiy of t-shirts you want : 2\n", + "enter the quantiy of mugs you want : 3\n", + "enter the quantiy of hats you want : 4\n", + "enter the quantiy of books you want : 5\n", + "enter the quantiy of keychains you want : 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 2, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 6}\n" + ] + } + ], + "source": [ + "#3 Ask the user to input the quantity of each product available in the inventory. \n", + "# Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values.\n", + " \n", + "inventory[\"tshirt\"] = int(input(\"enter the quantiy of t-shirts you want : \"))\n", + "inventory[\"mug\"] = int(input(\"enter the quantiy of mugs you want : \"))\n", + "inventory[\"hat\"] = int(input(\"enter the quantiy of hats you want : \")) \n", + "inventory[\"book\"] = int(input(\"enter the quantiy of books you want : \")) \n", + "inventory[\"keychain\"] = int(input(\"enter the quantiy of keychains you want : \"))\n", + "\n", + "inventory.items()\n", + "print (inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#4 Create an empty set called customer_orders.\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter a product your want to order: mug\n", + "enter a product your want to order: book\n", + "enter a product your want 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", + "for i in range (3): \n", + " x = input(\"enter a product your want to order: \")\n", + " while x not in products:\n", + " print(\"we don't have that product\")\n", + " x = input(\"enter a product your want to order: \")\n", + " customer_orders.add(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "#6 Print the products in the customer_orders set.\n", + "print (customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "60.0%\n", + "(3, 60.0)\n" + ] + } + ], + "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", + "#Store these statistics in a tuple called order_status\n", + "Total_Products_Ordered = len(customer_orders)\n", + "print (Total_Products_Ordered )\n", + "\n", + "Percentage_of_Products_Ordered = len(customer_orders) / len(products) * 100\n", + "print (f\"{Percentage_of_Products_Ordered}%\")\n", + "\n", + "order_status = (Total_Products_Ordered, Percentage_of_Products_Ordered)\n", + "print (order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "#8 Print the order statistics using the following format:\n", + "#Order Statistics:\n", + "#Total Products Ordered: \n", + "#Percentage of Products Ordered: % \n", + "print(f\"Total Products Ordered: {Total_Products_Ordered}\")\n", + "print(f\"Percentage of Products Ordered: {Percentage_of_Products_Ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#9 Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "\n", + "for key in inventory :\n", + " new_quantity = inventory[key] - 1\n", + " inventory[key] = new_quantity\n", + "\n", + "print (inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#10 Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "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": { @@ -68,7 +284,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,