Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
331 changes: 331 additions & 0 deletions lab_python_data_structures.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/letter-b/lab-python-functions/blob/main/lab_python_data_structures.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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: <total_products_ordered>\n",
" Percentage of Products Ordered: <percentage_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: <total_products_ordered>\n",
"Percentage of Products Ordered: <percentage_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
}
Loading