diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..9c62e12a 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,287 @@ "\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": [ + "#1. Define a list called products" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "2. #Create an empty dictionary called inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "inventory={}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#3. Ask the user to input the quantity of each product available in the inventory. " + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of the product t-shirt 3\n", + "Enter the quantity of the product mug 3\n", + "Enter the quantity of the product hat 3\n", + "Enter the quantity of the product book 3\n", + "Enter the quantity of the product keychain 3\n" + ] + } + ], + "source": [ + "for i in products:\n", + " inventory[i]=int(input(f\"Enter the quantity of the product {i}\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty set called customer_orders." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders=set()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Ask the user to input the name of three products that a customer wants to order \n", + "#(from those in the products list)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Item 1:Type the exact name of the product you would like mug\n", + "Item 2:Type the exact name of the product you would like hat\n", + "Item 3:Type the exact name of the product you would like \n" + ] + } + ], + "source": [ + "itemN=1\n", + "while itemN <=3:\n", + " item=input(f\"Item {itemN}:Type the exact name of the product you would like\")\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " itemN +=1\n", + " elif item==\"\":\n", + " itemN+=1\n", + " elif type(item)==str:\n", + " print(\"Enter the product name again, check in case of any typos\")\n", + " \n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug'}\n" + ] + } + ], + "source": [ + "#6. Print the products in the customer_orders set.\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#7. Calculate: Total Products Ordered & Percentage of Products Ordered.\n", + "#Store these statistics in a tuple called `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 28.571428571428573)\n" + ] + } + ], + "source": [ + "#Total products ordered\n", + "total_order=len(customer_orders)\n", + "#Percentage of Products Ordered: % \n", + "percent_prod= (total_order*100)/total_inventory\n", + "order_status=(total_order, percent_prod)\n", + "\n", + "print(order_status)\n", + "#total_inventory= sum(inventory.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 28.571428571428573%\n" + ] + } + ], + "source": [ + "#8. Order Statistics:\n", + "print(f\"Total Products Ordered: {total_order}\")\n", + "print(f\"Percentage of Products Ordered: {percent_prod}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#9. Update the inventory by subtracting 1 from the quantity of each product. \n", + "#Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "for order in customer_orders:\n", + " inventory[order]-=1\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 3, 'mug': 1, 'hat': 1, 'book': 3, 'keychain': 3}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of t-shirt that are left in the inventory are 3\n", + "The number of mug that are left in the inventory are 1\n", + "The number of hat that are left in the inventory are 1\n", + "The number of book that are left in the inventory are 3\n", + "The number of keychain that are left in the inventory are 3\n" + ] + } + ], + "source": [ + "for x,y in inventory.items():\n", + " print(f\"The number of {x} that are left in the inventory are {y}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "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 +342,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,