📚

-Explanation-

# List of product categories
categories = ["Fruit", "Dry Fruit", "Berry"]

# List of products with their names, categories, and prices
products = [
    {"name": "Apple", "category": "Fruit", "price": 0.5},
    {"name": "Banana", "category": "Fruit", "price": 0.3},
    {"name": "Cherry", "category": "Fruit", "price": 0.2},
    {"name": "Date", "category": "Dry Fruit", "price": 1.5},
    {"name": "Elderberry", "category": "Berry", "price": 1.0},
    {"name": "Figs", "category": "Dry Fruit", "price": 2.0}
]
for category in categories:
    print(f"\nCategory: {category}")
    for product in products:
        if product["category"] == category:
            print(f"  {product['name']} - ${product['price']}")

The code involves both the outer loop and inner loop, and this will be taken individually.

Outer Loop

for category in categories:
    print(f"\nCategory: {category}")
    
    
# output
    
Category: Fruit

Category: Dry Fruit

Category: Berry
   

• Purpose: The outer loop iterates over each category in the categories list.

• Iteration: For each category (e.g., “Fruit”, “Dry Fruit”, “Berry”), it prints the category name.

• Output: This will print the name of each category followed by a newline (\n).

Inner Loop

    for product in products:
        if product["category"] == category:
            print(f"  {product['name']} - ${product['price']}")
  1. Inner Loop: For each category in the outer loop, the inner loop iterates over each product in the products list.
  2. If Statement: Inside the inner loop, it checks if the category of the current product matches the current category from the outer loop.
    • Condition: if product["category"] == category checks if the product’s category matches the current category being iterated over in the outer loop.
  3. Print Statement: If the condition is true, it prints the product’s name and price, indented by two spaces for better readability.

Overall Flow

1. Outer Loop Iteration 1 (Category: “Fruit”):

• Prints Category: Fruit.

• Inner loop iterates over each product:

  • Product 1 (Apple): Category matches “Fruit”. Prints Apple - $0.5.
  • Product 2 (Banana): Category matches “Fruit”. Prints Banana - $0.3.
  • Product 3 (Cherry): Category matches “Fruit”. Prints Cherry - $0.2.
  • Product 4 (Date): Category does not match “Fruit”. Does nothing.
  • Product 5 (Elderberry): Category does not match “Fruit”. Does nothing.
  • Product 6 (Figs): Category does not match “Fruit”. Does nothing.

2. Outer Loop Iteration 2 (Category: “Dry Fruit”):

• Prints Category: Dry Fruit.

• Inner loop iterates over each product:

  • Product 1 (Apple): Category does not match “Dry Fruit”. Does nothing.
  • Product 2 (Banana): Category does not match “Dry Fruit”. Does nothing.
  • Product 3 (Cherry): Category does not match “Dry Fruit”. Does nothing.
  • Product 4 (Date): Category matches “Dry Fruit”. Prints Date - $1.5.
  • Product 5 (Elderberry): Category does not match “Dry Fruit”. Does nothing.
  • Product 6 (Figs): Category matches “Dry Fruit”. Prints Figs - $2.0.

3. Outer Loop Iteration 3 (Category: “Berry”):

• Prints Category: Berry.

• Inner loop iterates over each product:

  • Product 1 (Apple): Category does not match “Berry”. Does nothing.
  • Product 2 (Banana): Category does not match “Berry”. Does nothing.
  • Product 3 (Cherry): Category does not match “Berry”. Does nothing.
  • Product 4 (Date): Category does not match “Berry”. Does nothing.
  • Product 5 (Elderberry): Category matches “Berry”. Prints Elderberry - $1.0.
  • Product 6 (Figs): Category does not match “Berry”. Does nothing.

Complete Output

Category: Fruit
  Apple - $0.5
  Banana - $0.3
  Cherry - $0.2

Category: Dry Fruit
  Date - $1.5
  Figs - $2.0

Category: Berry
  Elderberry - $1.0

Summary

• Outer Loop: Iterates over each category in the categories list and prints the category name.

• Inner Loop: Iterates over each product in the products list and checks if the product’s category matches the current category.

• If Statement: Ensures that only products matching the current category are printed, along with their names and prices.

• Output: Organized by category, lists the products and their prices under each relevant category.