Skip to content
Open

Title #686

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
314 changes: 314 additions & 0 deletions LAB_python_data_structures.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
{
"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-data-structures/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": "zntUo5DM4mJd"
},
"source": [
"# Lab | Data Structures"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xgWUTvVi4mJf"
},
"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": [
"# 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\"]"
],
"metadata": {
"id": "4mOCUTGwP5AC"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# 2. Create an empty dictionary called inventory.\n",
"inventory = {}"
],
"metadata": {
"id": "Ik-xAa0DQMLU"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"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",
"for product in products:\n",
" quantity = int(input(f\"How many {product}s do you have? \"))\n",
" inventory[product] = quantity\n",
"\n",
"print(inventory)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Gnv-hv1FQWRz",
"outputId": "65215967-2bc8-4f4a-f7cd-10d07875b904"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"How many t-shirts do you have? 4\n",
"How many mugs do you have? 4\n",
"How many hats do you have? 7\n",
"How many books do you have? 1\n",
"How many keychains do you have? 3\n",
"{'t-shirt': 4, 'mug': 4, 'hat': 7, 'book': 1, 'keychain': 3}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# 4. Create an empty set called customer_orders.\n",
"customer_orders = set()"
],
"metadata": {
"id": "7fOssNWEQnl7"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"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",
"for order_id in range(3):\n",
" product = input(f\"What would you like to order from {products}? \")\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" else:\n",
" print(f\"Sorry, {product} is not in the products list.\")\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xBYhs9eWQ4ew",
"outputId": "91c2b8a0-7708-4cde-d6b1-25e42bcfd028"
},
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What would you like to order from ['t-shirt', 'mug', 'hat', 'book', 'keychain']? mug\n",
"What would you like to order from ['t-shirt', 'mug', 'hat', 'book', 'keychain']? hat\n",
"What would you like to order from ['t-shirt', 'mug', 'hat', 'book', 'keychain']? book\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# 6. Print the products in the customer_orders set.\n",
"print(f\"Your orders are: {customer_orders}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aQR8YV7QRMJO",
"outputId": "b9bd85b4-06e0-451e-852a-a6f4291fa1e6"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Your orders are: {'mug', 'hat', 'book'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# 7.\n",
"\"\"\"\n",
"Calculate the following order statistics:\n",
"\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",
"total_products_ordered = len(customer_orders)\n",
"percentage_products_ordered = (total_products_ordered / len(products)) * 100.\n",
"order_status = (total_products_ordered, percentage_products_ordered)"
],
"metadata": {
"id": "XWXIDZaiRakK"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# 8.\n",
"\"\"\"\n",
"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",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_products_ordered:.2f}%\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "r7ekqTZCS4AF",
"outputId": "dc0de318-ef47-4c4f-8ec2-86f2352d83ff"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"\n",
"inventory = {product: (quantity - 1 if product in customer_orders else quantity) for product, quantity in inventory.items() }\n"
],
"metadata": {
"id": "lFfH5G4RIN3q"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"print(\"Updated Inventory:\")\n",
"print(inventory)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hi4z9jwEUpqx",
"outputId": "10983a5d-9173-47ff-f05d-8b9417500b25"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Updated Inventory:\n",
"{'t-shirt': 4, 'mug': 3, 'hat': 6, 'book': 0, 'keychain': 3}\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
}