Python For Loops & Sequence Traversal
A for loop is the civilized way to visit every single item in a collection. Python picks up item 1, gives it to your code, then picks up item 2, and stops automatically when the collection is finished. No index counters, no infinite loops, just pure productivity.
"Imagine dealing playing cards to players around a table. You take the top card, hand it to the first player, take the next card, hand it to the second player, and stop when everyone has their card."
Deep Dive: How It Works
In Python, for loops are "for-each" loops. Instead of managing a numeric index (like in old C or Java), you iterate directly over the elements themselves.
The syntax "for item in collection:" automatically assigns each element to the temporary variable "item" for that iteration.
For loops work seamlessly on any iterable data type in Python: lists, tuples, sets, dictionaries, and even strings (iterating character by character).
Syntax Blueprint
for item in collection:
# 4 spaces of indentation
process(item)Python extracts each element from collection into the variable item sequentially.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Modifying a list while iterating over itWhy it happens: Deleting items from a list while looping over it skips elements because indices shift.
How to fix: Iterate over a copy of the list (e.g. for item in items.copy():) or use a list comprehension.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Loop over programming languages
Given langs = ["Python", "JavaScript", "Rust"], write a for loop to print each language.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.