diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..ffac1d28 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,167 @@ "\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": null, + "metadata": {}, + "outputs": [], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + " # Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#Create an empty dictionary called `inventory`.\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 7, 'hat': 9, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "#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", + "inventory[\"t-shirt\"] = int(input(\"Enter the quantity of product t-shirt: \"))\n", + "inventory[\"mug\"] = int(input(\"Enter the quantity of product mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Enter the quantity of product hat: \"))\n", + "inventory[\"book\"] = int(input(\"Enter the quantity of product book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Enter the quantity of product keychain: \"))\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "#Create an empty set called `customer_orders`.\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "customer_orders.add(input(\"Enter product 1: \"))\n", + "customer_orders.add(input(\"Enter product 2: \"))\n", + "customer_orders.add(input(\"Enter product 3: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "#Print the products in the `customer_orders` set.\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "total_products_ordered=len(customer_orders)\n", + "percentage_ordered=total_products_ordered/len(products)*100\n", + "order_status=(total_products_ordered, percentage_ordered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: Total Products Ordered: 3 Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "#Print the order statistics using the following format:\n", + " #Order Statistics:\n", + " #Total Products Ordered: \n", + " #Percentage of Products Ordered: % \n", + "print(\"Order Statistics:\", \"Total Products Ordered: \", total_products_ordered, \"Percentage of Products Ordered: \", percentage_ordered, \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "#Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 6\n", + "hat: 8\n", + "book: 3\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "#Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +224,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.7" } }, "nbformat": 4,