0
0
Pythonprogramming~15 mins

For–else execution behavior in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
For-else Execution Behavior
📖 Scenario: Imagine you are searching for a specific item in a list of products in a store. You want to know if the item is available or not.
🎯 Goal: You will write a Python program that uses a for loop with an else clause to check if a product is in the list. If the product is found, the program will print a message. If not, it will print a different message.
📋 What You'll Learn
Create a list of products with exact names
Create a variable for the product to search
Use a for loop with else to find the product
Print the correct message depending on whether the product is found
💡 Why This Matters
🌍 Real World
Searching for items in lists is common in shopping apps, inventory systems, and data filtering.
💼 Career
Understanding for-else helps write clear and efficient search code, useful in software development and data analysis.
Progress0 / 4 steps
1
Create the product list
Create a list called products with these exact items: 'apple', 'banana', 'orange', 'grape', 'mango'.
Python
Need a hint?

Use square brackets [] to create a list and separate items with commas.

2
Set the product to search
Create a variable called search_item and set it to the string 'orange'.
Python
Need a hint?

Use an equals sign = to assign the string 'orange' to search_item.

3
Use for-else to search the product
Write a for loop using item to iterate over products. Inside the loop, use an if statement to check if item == search_item. If yes, print Found orange! and use break. After the loop, add an else clause that prints orange not found..
Python
Need a hint?

The else after a for runs only if the loop did not break.

4
Print the final output
Run the program and print the output. The output should be exactly Found orange!.
Python
Need a hint?

Make sure you run the whole program to see the printed message.