diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..a25a57ff 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,184 @@ "\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": 62, + "metadata": {}, + "outputs": [], + "source": [ + "# 1.Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products=['t-shirt','mug','hat','book','keychain']" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "#2.Create an empty dictionary called inventory.\n", + "inventory={}" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 7\n", + "Please enter the quantity of mug: 5\n", + "Please enter the quantity of hat: 9\n", + "Please enter the quantity of book: 4\n", + "Please enter the quantity of keychain: 8\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", + "\n", + "inventory[products[0]]=int(input(f'Please enter the quantity of {products[0]}:'))\n", + "inventory[products[1]]=int(input(f'Please enter the quantity of {products[1]}:'))\n", + "inventory[products[2]]=int(input(f'Please enter the quantity of {products[2]}:'))\n", + "inventory[products[3]]=int(input(f'Please enter the quantity of {products[3]}:'))\n", + "inventory[products[4]]=int(input(f'Please enter the quantity of {products[4]}:'))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called customer_orders.\n", + "\n", + "customer_orders=set()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of the product you wish to order from the list['t-shirt', 'mug', 'hat', 'book', 'keychain']: mug\n", + "Please enter the name of the product you wish to order from the list['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n", + "Please enter the name of the product you wish to order from the list['t-shirt', 'mug', 'hat', 'book', 'keychain']: book\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", + "\n", + "order1=input(f'Please enter the name of the product you wish to order from the list{products}:')\n", + "customer_orders.add(order1)\n", + "order2=input(f'Please enter the name of the product you wish to order from the list{products}:')\n", + "customer_orders.add(order2)\n", + "order3=input(f'Please enter the name of the product you wish to order from the list{products}:')\n", + "customer_orders.add(order3)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# 6. Print the products in the customer_orders set.\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [], + "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", + "\n", + "order_status= (len(customer_orders),(len(customer_orders)/sum(inventory.values()))*100)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total of products ordered is: 3.\n", + "The percentage of products ordered is: 9.090909090909092%.\n" + ] + } + ], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# Order Statistics:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % \n", + "\n", + "print(f'The total of products ordered is: {order_status[0]}.')\n", + "print(f'The percentage of products ordered is: {order_status[1]}%.')" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n", + "\n", + "orders=list(customer_orders)\n", + "inventory[orders[0]]-=1\n", + "inventory[orders[1]]-=1\n", + "inventory[orders[2]]-=1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 7\n", + "mug: 4\n", + "hat: 8\n", + "book: 3\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f'{product}: {quantity}')" + ] } ], "metadata": { @@ -68,7 +246,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4,