How Does a For Loop Work ?
A for loop iterates over each item in a collection or sequence one at a time. Python simplifies this process by directly accessing items without needing to use an index explicitly.
Syntax of a For Loop
for item in sequence:
# Code to execute for each item
• item is a variable that takes the value of each element in the sequence during each iteration.
• sequence is the collection you are looping through (e.g., list, string, range).
Examples of For Loop statement in python
Think of sorting mail. You go through each piece of mail in the mailbox:
• For each mail (item in sequence), you check its address and decide where it goes.
Example:1 Iterating over a List (Processing Orders)
Imagine you have a list of orders and you want to process each one.
orders = ["Pizza", "Burger", "Pasta"] for order in orders: print(f"Processing order: {order}") Output: sql Processing order: Pizza Processing order: Burger Processing order: Pasta
Example:2 Using range( ) (Counting Steps)
You want to take 10 steps.
for step in range(1, 11): # range(start, stop) print(f"Taking step {step}") Output: arduino Taking step 1 Taking step 2 Taking step 10
Example:3 Iterating Over a String (Reading Letters in a Name)
You want to spell out each letter of a name.
name = "Alice" for letter in name: print(letter) Output: css A l i c e
Example: 4 Nested For Loops (Matching Shirts and Pants)
Imagine you want to match shirts with pants.
shirts = ["Red Shirt", "Blue Shirt"] pants = ["Black Pants", "White Pants"] for shirt in shirts: for pant in pants: print(f"Outfit: {shirt} and {pant}") Output: mathematica Outfit: Red Shirt and Black Pants Outfit: Red Shirt and White Pants Outfit: Blue Shirt and Black Pants Outfit: Blue Shirt and White Pants
Example: 5. Using break and continue (Stopping at a Specific Condition)
Imagine you're picking apples, but you stop when you find a rotten apple.
apples = ["Good", "Good", "Rotten", "Good"] for apple in apples: if apple == "Rotten": print("Found a rotten apple, stopping!") break print("Picked a good apple.") Output: css Picked a good apple. Picked a good apple. Found a rotten apple, stopping!
Example: 6. Skipping an Item (Ignoring a Missing Task)
You’re completing tasks but skip any marked as "skip".
tasks = ["Task 1", "Skip", "Task 2", "Task 3"] for task in tasks: if task == "Skip": continue # Skip this iteration print(f"Doing {task}") Output: arduino Doing Task 1 Doing Task 2 Doing Task 3
When to Use For Loops
1. Repeating tasks for each element in a collection.
2. Generating sequences of numbers (e.g., counting, creating patterns).
3. Processing strings, lists, or other iterable objects.
Key Tips
• Range (start, stop, step):
o start: Starting number (default is 0).
o stop: The loop runs up to but does not include this number.
o step: Increment or decrement value (default is 1).
for num in range (0, 10, 2): # 0 to 8, increment by 2 print(num) Output: 0 2 4 6 8
• You can use else with a for loop. It executes after the loop completes unless the loop is exited with break.
Next Steps
• Practice writing loops with lists, ranges, and strings.
• Experiment with break, continue, and else for control.
• Try nested loops to solve multi-layered problems.