diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..0c41bb55 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,236 @@ "\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": 113, + "metadata": {}, + "outputs": [], + "source": [ + "# 1.Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products_list = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [], + "source": [ + "# 2.Create an empty dictionary called inventory.\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of the t-shirt 11\n", + "Enter the quantity of the mug 22\n", + "Enter the quantity of the hat 33\n", + "Enter the quantity of the book 44\n", + "Enter the quantity of the keychain 55\n" + ] + } + ], + "source": [ + "# 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.\n", + "for product in products_list: \n", + " quantity_of_the_product = int(input(f\"Enter the quantity of the {product}\"))\n", + " inventory[product] = quantity_of_the_product\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [], + "source": [ + "# 4.Create an empty set called customer_orders.\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order from product_list: mug\n", + "Enter a product to order from product_list: hat\n", + "Enter a product to order from product_list: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 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", + "for i in range(3):\n", + " product = input(\"Enter a product to order from product_list:\")\n", + " if product in products_list:\n", + " customer_orders.add(product)\n", + "\n", + "print(customer_orders)\n", + "\n", + " \n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6.Print the products in the customer_orders set.\n", + "\n", + "print(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "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", + " \n", + " # Store these statistics in a tuple called `order_status`.\n", + "\n", + "products_orders = len(customer_orders)\n", + "print(products_orders)\n", + "\n", + "percentage_products_ordered= (products_orders/ len(products_list))*100\n", + "print (percentage_products_ordered)\n", + "\n", + "order_status = (products_orders,percentage_products_ordered)\n", + "print (order_status)\n", + " \n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total products ordered: 3\n", + "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: \n", + "#Percentage of Products Ordered: % \n", + "\n", + "print(\"Order Statistics\")\n", + "print(\"Total products ordered:\", order_status[0])\n", + "print(\"Percentage of Products Ordered:\", percentage_products_ordered)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 21, 'hat': 32, 'book': 43, 'keychain': 54}\n" + ] + } + ], + "source": [ + "# 9.Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "\n", + "for product in products_list:\n", + " if product in inventory:\n", + " inventory[product]-=1\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new_inventory:\n", + "t-shirt 10\n", + "mug 21\n", + "hat 32\n", + "book 43\n", + "keychain 54\n" + ] + } + ], + "source": [ + "# 10.Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(\"new_inventory:\")\n", + "for product in inventory:\n", + " print(product, inventory[product])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,