Python Tutorial 0/52 lessons ~6 min read Lesson 10
Loops (for / while)
Python's for loop iterates over any iterable (list, dict, string, file, generator).
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Python's for loop iterates over any iterable (list, dict, string, file, generator). while loops repeat until a condition is false.
Understanding the topic
Core concepts to understand:
for x in iterable:— most common.range(n)for index loops.enumerate()for index + value.break,continue,elseon loops.
Syntax reference
Visual flow / code:
python
# Iteratefor fruit in ["apple", "banana"]:print(fruit)# Index + valuefor i, name in enumerate(["a", "b"], start=1):print(i, name)# Parallel iterationfor k, v in zip(["a", "b"], [1, 2]):print(k, v)# whilen = 5while n > 0:n -= 1
Execution workflow
1Loops (for / while) Workflow
1 / 4Step 1
for x in iterable: — most common.
Apply this step while implementing loops (for / while) in real code.
Real-world use
Pythonic loops avoid C-style index loops. Use enumerate, zip, and comprehensions instead of for i in range(len(x)).
Best practices
- Iterate directly, not by index.
- Use
enumeratewhen you need the index. - Use
zipfor parallel iteration.
Common mistakes
- Modifying a list while iterating it — make a copy first.
Hands-on exercise
Interview preparation — practice these questions:
- for-else clause — what does it do?
- enumerate vs range(len)?
- When use
while?
Summary
In summary: Iterate directly. enumerate + zip are everyday tools.
Ready to mark this lesson complete?Track your journey across the entire course.