Skip to content
Open
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
309 changes: 309 additions & 0 deletions lab_python_data_structures-Emilie-Frillay.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"tags": [],
"id": "ghKHKfF2XYgU"
},
"source": [
"# Lab | Data Structures"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w-GvgA58XYgV"
},
"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": "markdown",
"source": [
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"."
],
"metadata": {
"id": "MEM4eMGcXkLF"
}
},
{
"cell_type": "code",
"source": [
"#**Lists**: Ordered, mutable collections that store elements of different types and allow indexing, appending, removing, and modifying. [ ]\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BGUlFr4-Xlbd",
"outputId": "cd76981a-1878-4859-9678-aa8e6a89ff0a"
},
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"2. Create an empty dictionary called `inventory`."
],
"metadata": {
"id": "QUi-E_bQYHUv"
}
},
{
"cell_type": "code",
"source": [
"#**Dictionaries**: Ordered (from Python 3.6), mutable key-value pairs for efficient lookup and organization of data. { }\n",
"\n",
"inventory = {} # create a new dict ----> assign curly brackets or inventory = dict()"
],
"metadata": {
"id": "riWaaNroYIW_"
},
"execution_count": 28,
"outputs": []
},
{
"cell_type": "markdown",
"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."
],
"metadata": {
"id": "omvkJB0gYgJz"
}
},
{
"cell_type": "code",
"source": [
"for product in products:# for loop > take each product of the list\n",
"\n",
" quantity = int(\n",
" input(\n",
" f\"Enter the quantity of {product}: \"\n",
" )\n",
" )\n",
"\n",
" inventory[product] = quantity\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nfaPb0s4Yiac",
"outputId": "6519297e-0cd1-4e5b-b0a8-111baf36297b"
},
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt: 2\n",
"Enter the quantity of mug: 6\n",
"Enter the quantity of hat: 3\n",
"Enter the quantity of book: 4\n",
"Enter the quantity of keychain: 30\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"4. Create an empty set called `customer_orders`."
],
"metadata": {
"id": "dD1laDUihdvZ"
}
},
{
"cell_type": "code",
"source": [
"#**Sets**: Unordered, mutable collections of unique elements with set operations like union and intersection. { }\n",
"customer_orders = set()# To create an empty list -----> assign brackets"
],
"metadata": {
"id": "5Aw4sUhRheq5"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "markdown",
"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",
"> Ajouter une citation\n",
"\n"
],
"metadata": {
"id": "T5KHnz-z-2dj"
}
},
{
"cell_type": "code",
"source": [
"for _ in range(3): # for loop\n",
" product_name = input(\"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): \")\n",
" customer_orders.add(product_name)#modifying a set ----> # Adding elements to a set e.g fruit.add('grape')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hWLGeI22-5rM",
"outputId": "62289e44-af4e-4f8b-94f7-b216a2a4109b"
},
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): t-shirt\n",
"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): mug\n",
"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): hat\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"6. Print the products in the `customer_orders` set."
],
"metadata": {
"id": "Ut_mxStJTsH2"
}
},
{
"cell_type": "code",
"source": [
" print(customer_orders) # Output: {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sBbE7vTpUlkT",
"outputId": "386b920d-7af0-4b90-8486-ccac872d78e5"
},
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'hat', 't-shirt', 'mug'}\n"
]
}
]
},
{
"cell_type": "markdown",
"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`."
],
"metadata": {
"id": "u9BhZzxFUti3"
}
},
{
"cell_type": "code",
"source": [
"Total_Products_Ordered = len(customer_orders)\n",
"\n",
"Percentage_of_Products_Ordered = (Total_Products_Ordered / len(products)) * 100\n",
"\n",
"#**Tuples**: Ordered, immutable collections commonly used for fixed values. ( )\n",
"\n",
"order_status = (Total_Products_Ordered+Percentage_of_Products_Ordered)\n",
"\n",
"order_status2 = (100-order_status)\n",
"\n",
"print(f\"your remaining stock is {order_status2} %\")"
],
"metadata": {
"id": "0hAAT5-pbZNl"
},
"execution_count": null,
"outputs": []
}
],
"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": [],
"history_visible": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}