diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..eff3e527 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,233 @@ "\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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "#Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "#Create an empty dictionary called inventory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 8\n", + "Enter the quantity for mug: 10\n", + "Enter the quantity for hat: 12\n", + "Enter the quantity for book: 11\n", + "Enter the quantity for keychain: 21\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 8, 'mug': 10, 'hat': 12, 'book': 11, 'keychain': 21}\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", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(\"Inventory: \", inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "#Create an empty set called customer_orders\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product to order : book\n", + "Choose a product to order : mug\n", + "Choose a product to order : hat\n" + ] + } + ], + "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", + "for item in range(3):\n", + " product = input(\"Choose a product to order : \")\n", + " customer_orders.add(product)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "#Print the products in the customer_orders set.\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Calculate the following order statistics:\n", + "#Total Products Ordered: The total number of products in the customer_orders set.\n", + "total_products_ordered = len(customer_orders)\n", + "total_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0 %\n" + ] + } + ], + "source": [ + "#Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + "percentage_of_products_ordered = (total_products_ordered / len(products))*100\n", + "print(percentage_of_products_ordered,\"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3, 60.0)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Store these statistics in a tuple called order_status.\n", + "order_status = (total_products_ordered, percentage_of_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "#Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "for product in inventory:\n", + " inventory[product] -=1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 7\n", + "mug 9\n", + "hat 11\n", + "book 10\n", + "keychain 20\n" + ] + } + ], + "source": [ + "#Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "for product, quantity in inventory.items():\n", + " print(product,quantity)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +295,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,