diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb new file mode 100644 index 00000000..8ae6c875 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,671 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": "markdown", + "metadata": {}, + "source": [ + "### STEP 1 — Get the lab repository ready (GitHub)\n", + "\n", + "Before writing any Python code, you must prepare the lab repository correctly.\n", + "\n", + "What you need to do now:\n", + "\n", + "1. Open the lab repository link (the one called\n", + "python-data-structures from Ironhack).\n", + "\n", + "2. On GitHub, click Fork (top-right corner).\n", + "\n", + "- This creates your own copy of the lab.\n", + "\n", + "3. After forking, you should see:\n", + "\n", + "- Your GitHub username\n", + "\n", + "- The repository name python-data-structures\n", + "\n", + "- The repository is now under your GitHub account\n", + "\n", + "- You can see a green Code button in your fork" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 2 — Clone the repository to your computer\n", + "\n", + "Now we bring the lab from GitHub to your local machine.\n", + "\n", + "What you need to do:\n", + "\n", + "1. In your forked repository on GitHub:\n", + "\n", + "- Click the green Code button\n", + "\n", + "- Make sure HTTPS is selected\n", + "\n", + "- Click Copy (this copies the repository URL)\n", + "\n", + "2. Open Terminal (Mac/Linux) or Git Bash / Command Prompt (Windows).\n", + "\n", + "3. Navigate to the folder where you keep your labs (for example, Documents or ironhack):\n", + "\n", + "4. Clone the repository: git clone PASTE_THE_URL_HERE (git clone https://github.com/yourusername/python-data-structures.git)\n", + "\n", + "5. Enter the project folder: cd python-data-structures" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 3 — Open the lab notebook (where you will write code)\n", + "\n", + "Now we open the Jupyter Notebook where the lab must be solved.\n", + "\n", + "What you need to do:\n", + "\n", + "1. Make sure you are inside the project folder: pwd\n", + "(You should see something like python-data-structures)\n", + "\n", + "2. Start Jupyter Notebook: jupyter notebook\n", + "\n", + "3. Your browser will open automatically.\n", + "\n", + "4. Click on the notebook file (ends with .ipynb).\n", + "\n", + "What you should see:\n", + "\n", + "- Cells with instructions\n", + "\n", + "- Empty code cells where you will write Python\n", + "\n", + "- A menu bar with Run ▶" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 4 — Create the products list (FIRST line of Python)\n", + "\n", + "This step uses a list, which is just a collection of items.\n", + "\n", + "What to do:\n", + "\n", + "1. Find the first empty code cell in the notebook.\n", + "\n", + "2. Type exactly this code:\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "3. Run the cell:\n", + "Press Shift + Enter\n", + "\n", + "(Optional but recommended)\n", + "Add this below and run again:\n", + "\n", + "print(products)\n", + "\n", + "You should see:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "What you just learned\n", + "- [] creates a list\n", + "\n", + "- Strings go inside quotes \" \"\n", + "\n", + "- Lists can store multiple values\n", + "\n", + "### What we are doing in this step\n", + "\n", + "- We are creating a list\n", + "\n", + "- A list is used to store multiple values in one variable\n", + "\n", + "- In this case: product names for the store\n", + "\n", + "### Explanation of new concepts (very important)\n", + "\n", + "'#' → # This is a comment (Python ignores it, it’s only for humans)\n", + "\n", + "products → This is a variable name (a label that stores data)\n", + "\n", + "= → Assignment operator (means “store this value in the variable”)\n", + "\n", + "[] → Defines a list\n", + "List – synonyms: collection, array, sequence\n", + "\n", + "\"t-shirt\" → A string (text)\n", + "String – synonyms: text, character sequence, text value" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# Create a list called \"products\" to store the names of all available products\n", + "# A list is a data structure that holds multiple items in order\n", + "\n", + "# Square brackets [] are used in Python to define a LIST\n", + "# Each item inside the list is a STRING (text), written inside quotes \" \"\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Print the list to check that it was created correctly\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 5 — Create the empty inventory dictionary - What we are doing\n", + "\n", + "- We need a dictionary to store inventory quantities for each product\n", + "\n", + "- A dictionary stores pairs of keys and values:\n", + " - Key → name of the product\n", + " - Value → quantity available\n", + "\n", + "\n", + "- We start with an empty dictionary and will fill it with user input next\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- {} → Defines a dictionary\n", + " - Dictionary – synonyms: map, hash table, associative array\n", + " - inventory → Variable name\n", + "\n", + "\n", + "- KEY: VALUE → Example: \"t-shirt\": 5\n", + " - Means “5 t-shirts in stock”" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# Create an empty dictionary called \"inventory\"\n", + "# A dictionary stores data in KEY: VALUE pairs\n", + "# Curly braces {} are used to define a dictionary\n", + "\n", + "inventory = {}\n", + "\n", + "# Print the empty dictionary to check\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 6 — Ask the user to input the inventory quantities - What we are doing\n", + "\n", + "- We will ask the user (person running the program) to type how many of each product are in stock\n", + "\n", + "- These quantities will be stored in the inventory dictionary\n", + "\n", + "- We will loop through the products list to make it easier\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- for product in products: → Loop through each item in the list\n", + "\n", + "- input() → Asks the user to type something\n", + "\n", + "- int() → Converts the input from string to integer (number)\n", + "\n", + "- inventory[product] = quantity → Adds the key:value pair to the dictionary\n", + "\n", + "- inventory.items() → Returns all key:value pairs for looping\n", + "\n", + "- f\"{}\" → f-string, allows inserting variables inside text" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 10\n", + "Enter quantity for mug: 5\n", + "Enter quantity for hat: 2\n", + "Enter quantity for book: 1\n", + "Enter quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 5\n", + "hat: 2\n", + "book: 1\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Ask the user to input the quantity available for each product\n", + "# We will store each product as a key in the inventory dictionary\n", + "# And the corresponding quantity as its value\n", + "\n", + "for product in products:\n", + " # input() asks the user to type something\n", + " # int() converts the input text into a number\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " \n", + " # Store the quantity in the dictionary using the product name as key\n", + " inventory[product] = quantity\n", + "\n", + "# Print the updated inventory to check\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 7 — Create the customer_orders set - What we are doing\n", + "\n", + "- We need to store the products a customer wants to order\n", + "\n", + "- A set is perfect because it:\n", + "\n", + " - Stores unique items only (no duplicates)\n", + " - Doesn’t care about order\n", + "\n", + "- We will ask the user to enter 3 products from the products list\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- set() → Creates a set (unique collection)\n", + "\n", + "- customer_orders.add(order) → Adds an item to the set\n", + "\n", + "- for i in range(3): → Repeat 3 times (for 3 products)\n", + "\n", + "- if order in products: → Ensure user enters a valid product\n", + "\n", + "- print(customer_orders) → Shows the products the customer wants" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1 for the customer: mug\n", + "Enter product 2 for the customer: hat\n", + "Enter product 3 for the customer: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders:\n", + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# Create an empty set to store customer orders\n", + "# A set is a collection of unique items, defined with set()\n", + "customer_orders = set()\n", + "\n", + "# Ask the user to input 3 products the customer wants to order\n", + "# Add each product to the customer_orders set\n", + "for i in range(3):\n", + " order = input(f\"Enter product {i+1} for the customer: \")\n", + " # Check if the product is in the available products list\n", + " if order in products:\n", + " customer_orders.add(order) # Add to the set\n", + " else:\n", + " print(f\"{order} is not available. Please choose a valid product.\")\n", + "\n", + "# Print the customer orders to check\n", + "print(\"\\nCustomer Orders:\")\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 8 — Calculate order statistics - What we are doing\n", + "\n", + "- We want two statistics for the customer order:\n", + "\n", + " 1. Total Products Ordered → Number of items in the customer_orders set\n", + " 2. Percentage of Products Ordered → How many products were ordered compared to total available products\n", + "\n", + "- We will store them in a tuple called order_status\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- len() → Returns the number of items in a list, set, or dictionary\n", + "\n", + "- () → Defines a tuple (ordered, immutable collection)\n", + "\n", + "- total_products_ordered / len(products) * 100 → Calculates percentage\n", + "\n", + "- f-strings f\"{}\" → Insert variables into text for printing" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "# Calculate the total number of products ordered\n", + "total_products_ordered = len(customer_orders) # len() gives the number of items in a set/list\n", + "\n", + "# Calculate the percentage of products ordered compared to total available products\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Store the statistics in a tuple called order_status\n", + "# A tuple is like a list but immutable (cannot be changed)\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "# Print the statistics\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 9 — Update the inventory after the order - What we are doing\n", + "\n", + "- We want to subtract 1 from the inventory of each ordered product\n", + "\n", + "- This simulates that the customer bought one of each product\n", + "\n", + "- We will modify the inventory dictionary accordingly\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- inventory[product] -= 1\n", + " - → Shorthand for inventory[product] = inventory[product] - 1\n", + " - → Updates the value in the dictionary\n", + "\n", + "- for product, quantity in inventory.items():\n", + " - → Loop through all key:value pairs in the dictionary\n", + "\n", + "- print(f\"{product}: {quantity}\")\n", + " - → Print in a readable format, one per line" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after the order:\n", + "t-shirt: 10\n", + "mug: 4\n", + "hat: 1\n", + "book: 0\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Update the inventory by subtracting 1 from each ordered product\n", + "for product in customer_orders:\n", + " inventory[product] -= 1 # Subtract 1 from the quantity\n", + "\n", + "# Print the updated inventory, one product per line\n", + "print(\"\\nUpdated Inventory after the order:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 10 — Save and push your lab to GitHub - What we are doing\n", + "\n", + "- We need to save your changes in the local repository\n", + "\n", + "- Then push them to your fork on GitHub\n", + "\n", + "- After that, you can submit the URL as your lab assignment\n", + "\n", + "Step-by-step commands (run in your terminal inside the project folder):\n", + "\n", + "1. Check current status (optional, see changes): git status\n", + "\n", + "\n", + "2. Add all files to staging: git add .\n", + "\n", + "3. Commit your changes with a message: git commit -m \"Solved Python Data Structures lab\"\n", + "\n", + "4. Push to your fork on GitHub: git push origin master\n", + "\n", + "5. Copy the URL of your forked repository\n", + "\n", + " - This is the URL you will submit in the Student Portal.\n", + "\n", + "Success check\n", + "\n", + "- When you go to your GitHub fork, you should see your updated notebook\n", + "\n", + "- All your code and outputs should be there\n", + "\n", + "### STEP 10.1 — If error occur: \n", + "\n", + "This means: \n", + "- Your branch is not called master\n", + "- New repositories usually use main instead of master\n", + "- You are trying to push to the Ironhack original repo\n", + "- You must push to YOUR fork, not theirs\n", + "\n", + "Check your branch name (DO THIS FIRST)\n", + "\n", + "In your terminal, run: git branch\n", + "\n", + "You will see something like: * main or * master\n", + "\n", + "### STEP 10.2 (again, very precise)\n", + "\n", + "Please run this command in the terminal (inside the lab folder): git push origin main\n", + "\n", + "### STEP 10.3 — Check current remote (DO THIS FIRST)\n", + "\n", + "What this error means (simple explanation)\n", + "Permission denied to data-bootcamp-v4/lab-python-data-structures.git\n", + "\n", + "Your local repository is still connected to the Ironhack original repo,\n", + "not to your fork.\n", + "\n", + "You cannot push to Ironhack’s repo → only to your own fork.\n", + "\n", + "So we will change the remote URL.\n", + "We will do this slowly and safely.\n", + "\n", + "Run this command:\n", + "\n", + "git remote -v\n", + "\n", + "You will see something like:\n", + "\n", + "- origin https://github.com/data-bootcamp-v4/lab-python-data-structures.git (fetch)\n", + "- origin https://github.com/data-bootcamp-v4/lab-python-data-structures.git (push)\n", + "\n", + "You are connected to the Ironhack repo, not to your fork.\n", + "Now we fix it with ONE safe command.\n", + "\n", + "### STEP 10.4 — Point the repo to YOUR fork (IMPORTANT)\n", + "1️⃣ First, open your fork in the browser\n", + "\n", + "It should look like:\n", + "\n", + "https://github.com/isailtonsilva/lab-python-data-structures\n", + "\n", + "\n", + "Click Code → HTTPS → Copy\n", + "\n", + "2️⃣ Now run exactly this command, replacing the URL with YOUR fork URL:\n", + "git remote set-url origin https://github.com/isailtonsilva/lab-python-data-structures.git\n", + "\n", + "⚠️ The username must be yours (isailtonsilva).\n", + "\n", + "3️⃣ Verify the change\n", + "\n", + "Run: git remote -v\n", + "\n", + "You should now see:\n", + "- origin https://github.com/isailtonsilva/lab-python-data-structures.git (fetch)\n", + "- origin https://github.com/isailtonsilva/lab-python-data-structures.git (push)\n", + "\n", + "### STEP 10.5 — Final push to GitHub (last command)\n", + "\n", + "Run exactly this: git push origin main\n", + "\n", + "You should see something like:\n", + "- To https://github.com/isailtonsilva/lab-python-data-structures.git\n", + "- * [new branch] main -> main\n", + "\n", + "Then:\n", + "1. Open your GitHub fork in the browser\n", + "2. Refresh the page\n", + "3. Confirm your notebook and code are there" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..8ae6c875 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,601 @@ "\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": [ + "### STEP 1 — Get the lab repository ready (GitHub)\n", + "\n", + "Before writing any Python code, you must prepare the lab repository correctly.\n", + "\n", + "What you need to do now:\n", + "\n", + "1. Open the lab repository link (the one called\n", + "python-data-structures from Ironhack).\n", + "\n", + "2. On GitHub, click Fork (top-right corner).\n", + "\n", + "- This creates your own copy of the lab.\n", + "\n", + "3. After forking, you should see:\n", + "\n", + "- Your GitHub username\n", + "\n", + "- The repository name python-data-structures\n", + "\n", + "- The repository is now under your GitHub account\n", + "\n", + "- You can see a green Code button in your fork" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 2 — Clone the repository to your computer\n", + "\n", + "Now we bring the lab from GitHub to your local machine.\n", + "\n", + "What you need to do:\n", + "\n", + "1. In your forked repository on GitHub:\n", + "\n", + "- Click the green Code button\n", + "\n", + "- Make sure HTTPS is selected\n", + "\n", + "- Click Copy (this copies the repository URL)\n", + "\n", + "2. Open Terminal (Mac/Linux) or Git Bash / Command Prompt (Windows).\n", + "\n", + "3. Navigate to the folder where you keep your labs (for example, Documents or ironhack):\n", + "\n", + "4. Clone the repository: git clone PASTE_THE_URL_HERE (git clone https://github.com/yourusername/python-data-structures.git)\n", + "\n", + "5. Enter the project folder: cd python-data-structures" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 3 — Open the lab notebook (where you will write code)\n", + "\n", + "Now we open the Jupyter Notebook where the lab must be solved.\n", + "\n", + "What you need to do:\n", + "\n", + "1. Make sure you are inside the project folder: pwd\n", + "(You should see something like python-data-structures)\n", + "\n", + "2. Start Jupyter Notebook: jupyter notebook\n", + "\n", + "3. Your browser will open automatically.\n", + "\n", + "4. Click on the notebook file (ends with .ipynb).\n", + "\n", + "What you should see:\n", + "\n", + "- Cells with instructions\n", + "\n", + "- Empty code cells where you will write Python\n", + "\n", + "- A menu bar with Run ▶" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 4 — Create the products list (FIRST line of Python)\n", + "\n", + "This step uses a list, which is just a collection of items.\n", + "\n", + "What to do:\n", + "\n", + "1. Find the first empty code cell in the notebook.\n", + "\n", + "2. Type exactly this code:\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "3. Run the cell:\n", + "Press Shift + Enter\n", + "\n", + "(Optional but recommended)\n", + "Add this below and run again:\n", + "\n", + "print(products)\n", + "\n", + "You should see:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "What you just learned\n", + "- [] creates a list\n", + "\n", + "- Strings go inside quotes \" \"\n", + "\n", + "- Lists can store multiple values\n", + "\n", + "### What we are doing in this step\n", + "\n", + "- We are creating a list\n", + "\n", + "- A list is used to store multiple values in one variable\n", + "\n", + "- In this case: product names for the store\n", + "\n", + "### Explanation of new concepts (very important)\n", + "\n", + "'#' → # This is a comment (Python ignores it, it’s only for humans)\n", + "\n", + "products → This is a variable name (a label that stores data)\n", + "\n", + "= → Assignment operator (means “store this value in the variable”)\n", + "\n", + "[] → Defines a list\n", + "List – synonyms: collection, array, sequence\n", + "\n", + "\"t-shirt\" → A string (text)\n", + "String – synonyms: text, character sequence, text value" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# Create a list called \"products\" to store the names of all available products\n", + "# A list is a data structure that holds multiple items in order\n", + "\n", + "# Square brackets [] are used in Python to define a LIST\n", + "# Each item inside the list is a STRING (text), written inside quotes \" \"\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Print the list to check that it was created correctly\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 5 — Create the empty inventory dictionary - What we are doing\n", + "\n", + "- We need a dictionary to store inventory quantities for each product\n", + "\n", + "- A dictionary stores pairs of keys and values:\n", + " - Key → name of the product\n", + " - Value → quantity available\n", + "\n", + "\n", + "- We start with an empty dictionary and will fill it with user input next\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- {} → Defines a dictionary\n", + " - Dictionary – synonyms: map, hash table, associative array\n", + " - inventory → Variable name\n", + "\n", + "\n", + "- KEY: VALUE → Example: \"t-shirt\": 5\n", + " - Means “5 t-shirts in stock”" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# Create an empty dictionary called \"inventory\"\n", + "# A dictionary stores data in KEY: VALUE pairs\n", + "# Curly braces {} are used to define a dictionary\n", + "\n", + "inventory = {}\n", + "\n", + "# Print the empty dictionary to check\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 6 — Ask the user to input the inventory quantities - What we are doing\n", + "\n", + "- We will ask the user (person running the program) to type how many of each product are in stock\n", + "\n", + "- These quantities will be stored in the inventory dictionary\n", + "\n", + "- We will loop through the products list to make it easier\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- for product in products: → Loop through each item in the list\n", + "\n", + "- input() → Asks the user to type something\n", + "\n", + "- int() → Converts the input from string to integer (number)\n", + "\n", + "- inventory[product] = quantity → Adds the key:value pair to the dictionary\n", + "\n", + "- inventory.items() → Returns all key:value pairs for looping\n", + "\n", + "- f\"{}\" → f-string, allows inserting variables inside text" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 10\n", + "Enter quantity for mug: 5\n", + "Enter quantity for hat: 2\n", + "Enter quantity for book: 1\n", + "Enter quantity for keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 5\n", + "hat: 2\n", + "book: 1\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Ask the user to input the quantity available for each product\n", + "# We will store each product as a key in the inventory dictionary\n", + "# And the corresponding quantity as its value\n", + "\n", + "for product in products:\n", + " # input() asks the user to type something\n", + " # int() converts the input text into a number\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " \n", + " # Store the quantity in the dictionary using the product name as key\n", + " inventory[product] = quantity\n", + "\n", + "# Print the updated inventory to check\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 7 — Create the customer_orders set - What we are doing\n", + "\n", + "- We need to store the products a customer wants to order\n", + "\n", + "- A set is perfect because it:\n", + "\n", + " - Stores unique items only (no duplicates)\n", + " - Doesn’t care about order\n", + "\n", + "- We will ask the user to enter 3 products from the products list\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- set() → Creates a set (unique collection)\n", + "\n", + "- customer_orders.add(order) → Adds an item to the set\n", + "\n", + "- for i in range(3): → Repeat 3 times (for 3 products)\n", + "\n", + "- if order in products: → Ensure user enters a valid product\n", + "\n", + "- print(customer_orders) → Shows the products the customer wants" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1 for the customer: mug\n", + "Enter product 2 for the customer: hat\n", + "Enter product 3 for the customer: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders:\n", + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# Create an empty set to store customer orders\n", + "# A set is a collection of unique items, defined with set()\n", + "customer_orders = set()\n", + "\n", + "# Ask the user to input 3 products the customer wants to order\n", + "# Add each product to the customer_orders set\n", + "for i in range(3):\n", + " order = input(f\"Enter product {i+1} for the customer: \")\n", + " # Check if the product is in the available products list\n", + " if order in products:\n", + " customer_orders.add(order) # Add to the set\n", + " else:\n", + " print(f\"{order} is not available. Please choose a valid product.\")\n", + "\n", + "# Print the customer orders to check\n", + "print(\"\\nCustomer Orders:\")\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 8 — Calculate order statistics - What we are doing\n", + "\n", + "- We want two statistics for the customer order:\n", + "\n", + " 1. Total Products Ordered → Number of items in the customer_orders set\n", + " 2. Percentage of Products Ordered → How many products were ordered compared to total available products\n", + "\n", + "- We will store them in a tuple called order_status\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- len() → Returns the number of items in a list, set, or dictionary\n", + "\n", + "- () → Defines a tuple (ordered, immutable collection)\n", + "\n", + "- total_products_ordered / len(products) * 100 → Calculates percentage\n", + "\n", + "- f-strings f\"{}\" → Insert variables into text for printing" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "# Calculate the total number of products ordered\n", + "total_products_ordered = len(customer_orders) # len() gives the number of items in a set/list\n", + "\n", + "# Calculate the percentage of products ordered compared to total available products\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Store the statistics in a tuple called order_status\n", + "# A tuple is like a list but immutable (cannot be changed)\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "# Print the statistics\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 9 — Update the inventory after the order - What we are doing\n", + "\n", + "- We want to subtract 1 from the inventory of each ordered product\n", + "\n", + "- This simulates that the customer bought one of each product\n", + "\n", + "- We will modify the inventory dictionary accordingly\n", + "\n", + "### Explanation of new concepts\n", + "\n", + "- inventory[product] -= 1\n", + " - → Shorthand for inventory[product] = inventory[product] - 1\n", + " - → Updates the value in the dictionary\n", + "\n", + "- for product, quantity in inventory.items():\n", + " - → Loop through all key:value pairs in the dictionary\n", + "\n", + "- print(f\"{product}: {quantity}\")\n", + " - → Print in a readable format, one per line" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after the order:\n", + "t-shirt: 10\n", + "mug: 4\n", + "hat: 1\n", + "book: 0\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Update the inventory by subtracting 1 from each ordered product\n", + "for product in customer_orders:\n", + " inventory[product] -= 1 # Subtract 1 from the quantity\n", + "\n", + "# Print the updated inventory, one product per line\n", + "print(\"\\nUpdated Inventory after the order:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### STEP 10 — Save and push your lab to GitHub - What we are doing\n", + "\n", + "- We need to save your changes in the local repository\n", + "\n", + "- Then push them to your fork on GitHub\n", + "\n", + "- After that, you can submit the URL as your lab assignment\n", + "\n", + "Step-by-step commands (run in your terminal inside the project folder):\n", + "\n", + "1. Check current status (optional, see changes): git status\n", + "\n", + "\n", + "2. Add all files to staging: git add .\n", + "\n", + "3. Commit your changes with a message: git commit -m \"Solved Python Data Structures lab\"\n", + "\n", + "4. Push to your fork on GitHub: git push origin master\n", + "\n", + "5. Copy the URL of your forked repository\n", + "\n", + " - This is the URL you will submit in the Student Portal.\n", + "\n", + "Success check\n", + "\n", + "- When you go to your GitHub fork, you should see your updated notebook\n", + "\n", + "- All your code and outputs should be there\n", + "\n", + "### STEP 10.1 — If error occur: \n", + "\n", + "This means: \n", + "- Your branch is not called master\n", + "- New repositories usually use main instead of master\n", + "- You are trying to push to the Ironhack original repo\n", + "- You must push to YOUR fork, not theirs\n", + "\n", + "Check your branch name (DO THIS FIRST)\n", + "\n", + "In your terminal, run: git branch\n", + "\n", + "You will see something like: * main or * master\n", + "\n", + "### STEP 10.2 (again, very precise)\n", + "\n", + "Please run this command in the terminal (inside the lab folder): git push origin main\n", + "\n", + "### STEP 10.3 — Check current remote (DO THIS FIRST)\n", + "\n", + "What this error means (simple explanation)\n", + "Permission denied to data-bootcamp-v4/lab-python-data-structures.git\n", + "\n", + "Your local repository is still connected to the Ironhack original repo,\n", + "not to your fork.\n", + "\n", + "You cannot push to Ironhack’s repo → only to your own fork.\n", + "\n", + "So we will change the remote URL.\n", + "We will do this slowly and safely.\n", + "\n", + "Run this command:\n", + "\n", + "git remote -v\n", + "\n", + "You will see something like:\n", + "\n", + "- origin https://github.com/data-bootcamp-v4/lab-python-data-structures.git (fetch)\n", + "- origin https://github.com/data-bootcamp-v4/lab-python-data-structures.git (push)\n", + "\n", + "You are connected to the Ironhack repo, not to your fork.\n", + "Now we fix it with ONE safe command.\n", + "\n", + "### STEP 10.4 — Point the repo to YOUR fork (IMPORTANT)\n", + "1️⃣ First, open your fork in the browser\n", + "\n", + "It should look like:\n", + "\n", + "https://github.com/isailtonsilva/lab-python-data-structures\n", + "\n", + "\n", + "Click Code → HTTPS → Copy\n", + "\n", + "2️⃣ Now run exactly this command, replacing the URL with YOUR fork URL:\n", + "git remote set-url origin https://github.com/isailtonsilva/lab-python-data-structures.git\n", + "\n", + "⚠️ The username must be yours (isailtonsilva).\n", + "\n", + "3️⃣ Verify the change\n", + "\n", + "Run: git remote -v\n", + "\n", + "You should now see:\n", + "- origin https://github.com/isailtonsilva/lab-python-data-structures.git (fetch)\n", + "- origin https://github.com/isailtonsilva/lab-python-data-structures.git (push)\n", + "\n", + "### STEP 10.5 — Final push to GitHub (last command)\n", + "\n", + "Run exactly this: git push origin main\n", + "\n", + "You should see something like:\n", + "- To https://github.com/isailtonsilva/lab-python-data-structures.git\n", + "- * [new branch] main -> main\n", + "\n", + "Then:\n", + "1. Open your GitHub fork in the browser\n", + "2. Refresh the page\n", + "3. Confirm your notebook and code are there" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +663,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,