diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..11f9542a 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -19,36 +19,297 @@ "\n", "Follow the steps below to complete the exercise:\n", "\n", - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", - "\n", - "2. Create an empty dictionary called `inventory`.\n", - "\n", - "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", - "4. Create an empty set called `customer_orders`.\n", - "\n", - "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", + "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "#type (product)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory = {}\n", + "#type(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 9, 'hat': 2, 'book': 9, 'keychain': 2}\n" + ] + } + ], + "source": [ + "for item in product: \n", + " quantity = int(input(f\"how many {item} I have ?\"))\n", + " inventory [item] = quantity\n", "\n", - "6. Print the products in the `customer_orders` set.\n", + "print (inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "custumer_order = set()\n", + "#type (custumer_order)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "iguana is not a product available\n" + ] + } + ], + "source": [ + "for i in range(3):\n", + " item = input(\"What product you want to order (t-shirt, mug, hat, book, keychain)? \")\n", + " if item in product:\n", + " custumer_order.add(item)\n", + " else:\n", + " print(f'{item}' \" is not a product available\")\n", "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6. Print the products in the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'keychain', 'book'}\n" + ] + } + ], + "source": [ + "print(custumer_order)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + " Store these statistics in a tuple called `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total products ordered 2\n", + "total products inventory 5\n", + "percentage of products ordered 40.0 %\n", + "order status (2, 40.0)\n" + ] + } + ], + "source": [ + "total_products_ordered = len(custumer_order)\n", + "print (\"total products ordered \", total_products_ordered)\n", + "\n", + "total_products_inventory = len (product)\n", + "print (\"total products inventory\" , total_products_inventory)\n", "\n", + "percentage_of_products_ordered = (total_products_ordered/total_products_inventory)*100\n", + "print (\"percentage of products ordered\" , percentage_of_products_ordered, \"%\")\n", + "\n", + "order_status = (total_products_ordered,percentage_of_products_ordered)\n", + "#print(type (order_status))\n", + "print (\"order status\" , order_status)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", - "\n", - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", - "\n", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\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": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: \n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0 %\n" + ] + } + ], + "source": [ + "print (\"Order Statistics: \")\n", + "print (\"Total Products Ordered:\" , total_products_ordered )\n", + "print (\"Percentage of Products Ordered:\" , percentage_of_products_ordered , \"%\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "keychain uptaded now there's {1} left in inventory\n", + "book uptaded now there's {8} left in inventory\n" + ] + } + ], + "source": [ + "for item in custumer_order:\n", + " if item in inventory:\n", + " if inventory[item]>0:\n", + " inventory[item] -= 1\n", + " print (f'{item}',\"uptaded now there's\",{inventory[item]},\"left in inventory\")\n", + " else:\n", + " print (f'{item}', \"is out of stock in inventory\")\n", + " else:\n", + " print (f'{item}', \"does not exist in inventory\") " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "updated inventory:\n", + "{'t-shirt': 6, 'mug': 9, 'hat': 2, 'book': 8, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print (\"updated inventory:\")\n", + "print (inventory)" ] } ], @@ -68,7 +329,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,