🧠 PYTHON ITERATION — BEHIND THE SCENES (BTS)
============================================
🎯 PURPOSE:
------------
Understand how Python loops (like `for`) work behind the scenes using:
- iter()
- next()
- Iterable and Iterator concepts
This helps write better loops, understand errors like StopIteration, and even build your own custom iterators!
────────────────────────────────────────────
1️⃣ ITERATION TOOLS — WHAT WE USE TO LOOP
────────────────────────────────────────────
These are tools used to iterate in Python:
- for loops
- list/set/dict comprehensions
- while loops using manual next()
- generator expressions
All these tools work on top of **iterable** objects.
────────────────────────────────────────────
2️⃣ ITERABLE OBJECTS — WHAT WE LOOP OVER
────────────────────────────────────────────
An object is **iterable** if it can return items one at a time.
Examples of iterable objects:
- list → [1, 2, 3]
- string → "hello"
- tuple → (1, 2, 3)
- set, dict, file objects
Technically, they must define a method:
```python
__iter__()
This method returns an iterator object.
──────────────────────────────────────────── 3️⃣ ITERATORS — WHO DOES THE REAL WORK ──────────────────────────────────────────── An iterator is an object that:
✅ Has a method __iter__() → returns itself
✅ Has a method __next__() → returns the next item
🚫 Raises StopIteration when all items are used
Example:
lst = [1, 2, 3]
itr = iter(lst)
print(next(itr)) # 1
print(next(itr)) # 2
print(next(itr)) # 3
print(next(itr)) # StopIteration Error──────────────────────────────────────────── 4️⃣ HOW FOR LOOPS WORK INTERNALLY ──────────────────────────────────────────── Code you write:
for item in myList:
print(item)Python translates it behind the scenes to:
itr = iter(myList)
while True:
try:
item = next(itr)
print(item)
except StopIteration:
break🔍 So for = iter() + repeated next()
──────────────────────────────────────────── 5️⃣ ITERABLE vs ITERATOR — WHAT’S THE DIFFERENCE? ────────────────────────────────────────────
| Feature | Iterable | Iterator |
|---|---|---|
| Has iter | ✅ | ✅ |
| Has next | ❌ | ✅ |
| Remembers position | ❌ | ✅ |
Check using:
from collections.abc import Iterable, Iterator
print(isinstance([1,2,3], Iterable)) # True
print(isinstance([1,2,3], Iterator)) # False
print(isinstance(iter([1,2,3]), Iterator)) # Trueiter(myList) is myList → False
Because iter() returns a new iterator object.
──────────────────────────────────────────── 6️⃣ REAL-LIFE ANALOGY 🎧 ──────────────────────────────────────────── Iterable = Playlist → You can restart anytime Iterator = Music Player → It plays one by one and remembers which song is playing
──────────────────────────────────────────── 7️⃣ FILE OBJECTS ARE ITERATORS 📂 ──────────────────────────────────────────── You can loop through files directly:
with open("file.txt") as f:
for line in f:
print(line)Why? Because file objects implement both __iter__() and __next__().
────────────────────────────────────────────
8️⃣ CREATING CUSTOM ITERATORS 🛠️
────────────────────────────────────────────
You can build your own iterator class using __iter__() and __next__():
Example:
class CountUpto:
def __init__(self, max):
self.max = max
self.current = 1
def __iter__(self):
return self
def __next__(self):
if self.current > self.max:
raise StopIteration
val = self.current
self.current += 1
return val
# Usage
for num in CountUpto(3):
print(num) # 1, 2, 3──────────────────────────────────────────── 9️⃣ SUMMARY — KEY TAKEAWAYS ✅ ────────────────────────────────────────────
forloops useiter()to get an iterator, andnext()to get each value.StopIterationtells Python when to stop.- Iterables (like list, str, file) do not have memory of position.
- Iterators remember where they left off.
- File objects are their own iterators.
- You can build custom iterators using Python classes.
──────────────────────────────────────────── 🔟 PRACTICE QUESTIONS 💬 ────────────────────────────────────────────
- What does
iter()return? - Why is
iter(myList) is myListFalse? - What happens if we keep calling
next()after the last item? - What exception tells Python to stop a loop?
- What's the difference between an iterable and an iterator?
🎯 Tip: Mastering this helps you understand how loops, generators, comprehensions, and file I/O work at a deeper level!
---
Would you also like a **printable PDF version**, a **diagram**, or a **Roman Urdu explanation** for revision?