From d2cf8ed7b902464337132905c897c45f4dabe905 Mon Sep 17 00:00:00 2001 From: VeliaAlam Date: Fri, 25 Jul 2025 17:44:17 +0200 Subject: [PATCH] lab done --- .DS_Store | Bin 0 -> 6148 bytes lab-python-data-structures.ipynb | 233 ++++++++++++++++++++++++++++++- 2 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..222f5c5ea9a193f742afe7419d77c667f99496c7 GIT binary patch literal 6148 zcmeHKJ5Iw;5Znb9mS|E!_fCM68<@zPAQwO(Qb0=N6r?v5m&h5o2G`&OoPpU7$d(mK z3YeAljoEN?!svy4zg^W}ZF+fOP_2aH?E zwQS^3p2qy=<1n}D+IF?=R*2PlJiLBfz1@uS>mT#`hrzSyJE$}&Kn17(6`%tDQUUaA zv-%>CD;1yuRNz|y`#u!7VUySg`lkbnj{rdXjd#Pf&l1350brBZ2O4-ZW$R7dIg+>K_ GLxE3#DJhl! literal 0 HcmV?d00001 diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..276f9f1d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -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": { @@ -68,7 +295,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,