Iterators in Python

PolymorphismusGeneratoren

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

Wir haben die Materialien überprüft, dennoch können Fehler vorkommen. Der Inhalt dient ausschließlich Bildungszwecken, daher verwende ihn auf eigene Verantwortung und überprüfe ihn bei Bedarf mit anderen Quellen.

✨ Frag Lara — deine KI-Lernpartnerin

Entsperre personalisierte Lernunterstützung. Lara kann Lektionen erklären, Themen zusammenfassen und deine Lernfragen beantworten — verfügbar ab dem Go-Tarif.


Lara hilft dir, schneller zu lernen — exklusiv für ReadyTools Go-, Plus- und Max-Mitglieder.

Verfolge deinen Fortschritt 🚀

Lerne einfacher, indem du deinen Fortschritt kostenlos verfolgst.