diff --git a/lab_python_data_structures.ipynb b/lab_python_data_structures.ipynb new file mode 100644 index 0000000..4d8d696 --- /dev/null +++ b/lab_python_data_structures.ipynb @@ -0,0 +1,331 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [], + "id": "DCx48fXjMii8" + }, + "source": [ + "# Lab | Data Structures" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s9W3RTPEMii_" + }, + "source": [ + "## Exercise: Managing Customer Orders\n", + "\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", + "\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", + "\n", + "6. Print the products in the `customer_orders` set.\n", + "\n", + "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", + "\n", + "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", + "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\"]" + ], + "metadata": { + "id": "cwDljcDHM0yR" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Create an empty dictionary called inventory.\n", + "inventory = {}" + ], + "metadata": { + "id": "4hZ5wGK5M95I" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "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 item in products:\n", + " quantity = int(input(f\"Enter the quantity of {item} in stock: \"))\n", + " inventory[item] = quantity\n", + "\n", + "print(f\"There are {inventory} in the inventory.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "nQJr7CLRM_TI", + "outputId": "12859c26-7cf3-4cd1-c786-8bd153ec467c" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the quantity of t-shirt in stock: 7\n", + "Enter the quantity of mug in stock: 5\n", + "Enter the quantity of hat in stock: 3\n", + "Enter the quantity of book in stock: 7\n", + "Enter the quantity of keychain in stock: 2\n", + "There are {'t-shirt': 7, 'mug': 5, 'hat': 3, 'book': 7, 'keychain': 2} in the inventory.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Create an empty set called customer_orders.\n", + "customer_orders = set()" + ], + "metadata": { + "id": "4mQf6cRDOdyI" + }, + "execution_count": 24, + "outputs": [] + }, + { + "cell_type": "code", + "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 i in range(3):\n", + " order_3 = (input(\"Which item would you like to order? \"))\n", + "\n", + " while order_3 not in products:\n", + " print(\"This item is not available in our inventory.\")\n", + " order_3 = (input(\"Which item would you like to order? \"))\n", + "\n", + " if order_3 in products:\n", + " customer_orders.add(order_3)\n", + "\n", + " print(f\"You have ordered {customer_orders}.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "eisW9hDdOhH3", + "outputId": "2cd6b02e-f2db-4861-d2c2-5135886d7a76" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Which item would you like to order? bell\n", + "This item is not available in our inventory.\n", + "Which item would you like to order? hat\n", + "You have ordered {'hat'}.\n", + "Which item would you like to order? cat\n", + "This item is not available in our inventory.\n", + "Which item would you like to order? book\n", + "You have ordered {'book', 'hat'}.\n", + "Which item would you like to order? mug\n", + "You have ordered {'book', 'mug', 'hat'}.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Print the products in the customer_orders set.\n", + "print(f\"You have ordered {customer_orders}.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x11YK_UvP-Tj", + "outputId": "0e67a9df-8e37-43db-e845-5a31049fb19e" + }, + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "You have ordered {'book', 'mug', 'hat'}.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "\"\"\"\n", + "#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", + "#Order Statistics:\n", + "total_products_ordered = len(customer_orders)\n", + "total_available_products = sum(inventory.values())\n", + "percentage_ordered = (total_products_ordered / total_available_products) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "#this is a tuple\n", + "\n", + "print(f\"A total of {total_products_ordered} items were ordered.\")\n", + "print(f\"There are a total of {total_available_products} left available in the inventory.\")\n", + "print(f\"Therefore {percentage_ordered}% of the products were ordered.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OWjzTKU5Quko", + "outputId": "f03b85e2-b11c-4e39-dcef-6d4698b24541" + }, + "execution_count": 42, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A total of 3 items were ordered.\n", + "There are a total of 24 left available in the inventory.\n", + "Therefore 12.5% of the products were ordered.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "\"\"\"\n", + "#Print the order statistics using the following format:\n", + "Order Statistics:\n", + "Total Products Ordered: \n", + "Percentage of Products Ordered: %\n", + "\"\"\"\n", + "print(f\"Order Statistics:\\nTotal Products Ordered: {total_products_ordered}\\nPercentage of Products Ordered: {percentage_ordered}%\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dJN203noQ-9o", + "outputId": "c1aabae3-4e22-40f1-9a7e-4e8a36a15fe0" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 12.5%\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "K24IV9FvSSTq", + "outputId": "27720b49-f863-4dc3-f3de-e6b521d6314b" + }, + "execution_count": 50, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Updated Inventory:\n", + "t-shirt: 7\n", + "mug: 5\n", + "hat: 3\n", + "book: 7\n", + "keychain: 2\n" + ] + } + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/lab_python_functions.ipynb b/lab_python_functions.ipynb new file mode 100644 index 0000000..06c1818 --- /dev/null +++ b/lab_python_functions.ipynb @@ -0,0 +1,417 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": { + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" + }, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "markdown", + "id": "0c581062-8967-4d93-b06e-62833222f930", + "metadata": { + "tags": [], + "id": "0c581062-8967-4d93-b06e-62833222f930" + }, + "source": [ + "## Exercise: Managing Customer Orders with Functions\n", + "\n", + "In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n", + "\n", + "2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n", + "\n", + "3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n", + "\n", + "4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n", + "\n", + "5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n", + "\n", + "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n", + "\n", + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "Hints for functions:\n", + "\n", + "- Consider the input parameters required for each function and their return values.\n", + "- Utilize function parameters and return values to transfer data between functions.\n", + "- Test your functions individually to ensure they work correctly.\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "source": [ + "#Define a function named initialize_inventory that takes products as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for item in products:\n", + " quantity = input(f'Number of {item} available in stock: ')\n", + " while not quantity.isdigit():\n", + " quantity = input(f'Please enter a valid number of {item} available in stock: ')\n", + " inventory[item] = int(quantity) # Convert the valid quantity to an integer\n", + " return inventory" + ], + "metadata": { + "id": "b_L1WLizV9pN" + }, + "id": "b_L1WLizV9pN", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Define a function named get_customer_orders that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the customer_orders set.\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input('Enter the name of your product or \"done\" if you are done ordering: ')\n", + " if order == 'done':\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders\n", + " print(customer_orders) # Placing this print before the return will allow orders to be displayed" + ], + "metadata": { + "id": "y9rESHjAWmEg" + }, + "id": "y9rESHjAWmEg", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + " # Ensure the inventory does not drop below zero\n", + " if inventory[item] < 0:\n", + " inventory[item] = 0\n", + " print(f\"One {item} removed from inventory.\")\n", + " else:\n", + " print(f\"There is no {item} in our inventory.\")\n", + "\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ], + "metadata": { + "id": "Q6hb35bmfXW-" + }, + "id": "Q6hb35bmfXW-", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Define a function named calculate_order_statistics that takes customer_orders and products as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " total_available_products = sum(inventory.values())\n", + " percentage_ordered = (total_products_ordered / total_available_products) * 100\n", + "\n", + " print(f\"A total amount of {total_products_ordered} items was ordered, which means that {percentage_ordered:.1f}% of the items were ordered.\")\n" + ], + "metadata": { + "id": "lkoebZ36ZN1Y" + }, + "id": "lkoebZ36ZN1Y", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Define a function named print_order_statistics that takes order_statistics as a parameter. Inside the function, implement the code for printing the order statistics.\n", + "#Order Statistics:\n", + "order_statistics = (total_products_ordered, percentage_ordered)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_ordered:.1f}\")" + ], + "metadata": { + "id": "4yaMjKtaZidq" + }, + "id": "4yaMjKtaZidq", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Define a function named print_updated_inventory that takes inventory as a parameter. Inside the function, implement the code for printing the updated inventory.\n", + "def print_updated_inventory(inventory):\n", + " print(f\"Updated inventory: {inventory}\")" + ], + "metadata": { + "id": "4bAojt76Z12a" + }, + "id": "4bAojt76Z12a", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "o2_iX0V4Z8CD", + "outputId": "d35b1802-af18-4a36-bcc3-62fd53bd5392" + }, + "id": "o2_iX0V4Z8CD", + "execution_count": null, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of t-shirt available in stock: 11\n", + "Number of mug available in stock: 8\n", + "Number of hat available in stock: 5\n", + "Number of book available in stock: 9\n", + "Number of keychain available in stock: 3\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "get_customer_orders()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TvOtkOCAa272", + "outputId": "83623ac5-0490-4d41-a6fd-e3143d01f0ee" + }, + "id": "TvOtkOCAa272", + "execution_count": null, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the name of your product or \"done\" if you are done ordering: umbrella\n", + "Enter the name of your product or \"done\" if you are done ordering: leash\n", + "Enter the name of your product or \"done\" if you are done ordering: mug\n", + "Enter the name of your product or \"done\" if you are done ordering: hat\n", + "Enter the name of your product or \"done\" if you are done ordering: hat\n", + "Enter the name of your product or \"done\" if you are done ordering: book\n", + "Enter the name of your product or \"done\" if you are done ordering: done\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'book', 'hat', 'leash', 'mug', 'umbrella'}" + ] + }, + "metadata": {}, + "execution_count": 14 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#THIS IS EXTRA, AS I MADE A MISTAKE\n", + "customer_orders = {'cup', 'donw', 'hat', 'mug', 'umbrella'}\n", + "customer_orders.remove(\"donw\") # Remove 'donw' from the set\n", + "print(customer_orders)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rRazVJllcQQA", + "outputId": "66ab49d2-44dc-48ce-bb83-86d7fc002caf" + }, + "id": "rRazVJllcQQA", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'umbrella', 'hat', 'cup', 'mug'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "update_inventory(customer_orders, inventory)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uNq6uBXscYfY", + "outputId": "5cb75131-d698-430c-fd03-eba4604dbd6b" + }, + "id": "uNq6uBXscYfY", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "There is no umbrella in our inventory.\n", + "One hat removed from inventory.\n", + "There is no cup in our inventory.\n", + "One mug removed from inventory.\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 11\n", + "mug: 4\n", + "hat: 1\n", + "book: 9\n", + "keychain: 3\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "calculate_order_statistics(customer_orders, products)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xQL0zU9wjDlr", + "outputId": "72027f3d-70cf-42ef-a2cb-f5e76f4c2447" + }, + "id": "xQL0zU9wjDlr", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A total amount of 4 items was ordered, which means that 15.4% of the items were ordered.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print_order_statistics(order_statistics)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "BVg7ScN-kg8l", + "outputId": "048c88b9-bc3f-4a78-b3a8-9140de512599" + }, + "id": "BVg7ScN-kg8l", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Total products ordered: 4\n", + "Percentage of unique products ordered: 15.4\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(update_inventory(customer_orders, inventory))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uMkrMrdNlkD0", + "outputId": "827e5a85-808f-42d3-c619-e5d27e7e7ce4" + }, + "id": "uMkrMrdNlkD0", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "There is no umbrella in our inventory.\n", + "One hat removed from inventory.\n", + "There is no cup in our inventory.\n", + "One mug removed from inventory.\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 11\n", + "mug: 3\n", + "hat: 0\n", + "book: 9\n", + "keychain: 3\n", + "None\n" + ] + } + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file