Iterators in Python
An iterator is an object that allows traversal through a sequence of values – one at a time. It's the mechanism behind for loops and many built-in functions.
Python's iteration protocol uses two special methods: __iter__() and __next__(). Understanding iterators helps you create custom iterable objects.
How For Loops Work
for item in iterable: is equivalent to calling iter() and next() repeatedly.
Creating a Custom Iterator
Define __iter__() (returns self) and __next__() (returns next value or raises StopIteration).
Using iter() and next()
Quick Quiz
Hemos revisado y comprobado los materiales, pero aún pueden existir errores. El contenido se ofrece únicamente con fines educativos, así que úsalo bajo tu propia responsabilidad y verifica con otras fuentes si es necesario.
✨ Pregunta a Lara — tu compañera de estudio con IA
Desbloquea soporte de aprendizaje personalizado. Lara puede explicar lecciones, resumir temas y responder tus preguntas — disponible desde el plan Go y superiores.
Lara te ayuda a aprender más rápido — exclusivo para los miembros ReadyTools Go, Plus y Max.


