Basic Overview:

  • Lists: We loop through a list of colors, printing each color.
  • Dictionaries: We loop through a dictionary of fruits, where the key is the fruit name and the value is the quantity. The loop prints both the fruit name and quantity.
# In programming, loops are used to iterate over collections of data, such as lists or dictionaries.
# This allows us to perform operations on each item without manually writing repetitive code.
# In this example, we will iterate over a list of colors and a dictionary containing fruit details.

# Pseudocode:
# colors ← ["red", "blue", "green", "yellow"]
# FOR EACH color IN colors:
#     DISPLAY color
# END FOR


# Example 1: Loop through a list of colors
colors = ["red", "blue", "green", "yellow"]
for color in colors:
    print(color)  # Display each color

# Example 2: Loop through a fruit dictionary
fruits = {"apple": 3, "banana": 5, "cherry": 7}  # Number of each type of fruit
for fruit, quantity in fruits.items():
    print(fruit, ":", quantity)  # Display each fruit and its quantity

    #Problem: Looping Through a List
    #Create a list of your three favorite foods. Write a loop to display each food item in the list.

    
red
blue
green
yellow
apple : 3
banana : 5
cherry : 7

Homework Problem:

You are tasked with solving two exercises involving loops in Python. The goal is to understand how to iterate through different types of collections (lists and dictionaries) and print their contents.

Requirements:

Exercise 1: Write a for loop to iterate through a list of colors and print each color. Exercise 2: Write a for loop to iterate through a dictionary of fruits where the keys represent the type of fruit, and the values represent the quantity of each fruit. Print the fruit’s name and its corresponding quantity in the format: “fruit_name : quantity”. Input:

For the first exercise, use the list of colors: [“red”, “blue”, “green”, “yellow”]

For the second exercise, use the dictionary of fruits: {“apple”: 3, “banana”: 5, “cherry”: 7}

Expected Output: You should print each color from the list, and each fruit with its quantity from the dictionary.

#Sample answers:

# Example 1: Loop through a list of colors
colors = ["red", "blue", "green", "yellow"]
for color in colors:
    print(color)  # Display each color

# Example 2: Loop through a fruit dictionary
fruits = {"apple": 3, "banana": 5, "cherry": 7}  # Number of each type of fruit
for fruit, quantity in fruits.items():
    print(fruit, ":", quantity)

red
blue
green
yellow
apple : 3
banana : 5
cherry : 7

What’s Happening:

In the first example, the for loop iterates over each element in the colors list. The loop goes through the list one by one, and each color is printed to the console. The loop ends after all the colors (“red,” “blue,” “green,” and “yellow”) are displayed.

In the second example, the for loop iterates over the key-value pairs in the fruits dictionary using the .items() method. The loop prints the fruit name (key) along with its quantity (value). For each fruit in the dictionary—apple, banana, and cherry—the loop prints the name of the fruit followed by its quantity, such as “apple: 3” and so on.

Additional Notes:

  • List Iteration: Using a for loop allows us to access and perform actions on each element in a list. This is useful when you have a collection of similar data, like colors or items in a store.
  • Dictionary Iteration: In a dictionary, a for loop iterates over key-value pairs using .items(). This is particularly useful for tasks where you need to manage related data, such as inventory lists (fruit and quantity).
  • Use Cases: Looping over collections like lists and dictionaries is common in scenarios where you need to analyze data, summarize content, or automate tasks that involve processing multiple items.