Iterators (Iterables)
You already know that there are all kinds of objects in Python that you can iterate over using the for-in construct. For example, you can iterate over the characters of a string, or the elements of a list, or whatever range() returns. You can also iterate over the lines of a file and you have probably seen the for in construct in other cases as well. The objects that can be iterated over are collectively called iterables. You can do all kind of interesting things on such iterables. We’ll see a few now.
There are several data types that we can iterate over using the for … in … construct. For example
- strings
- lists
- tuples
- filehandles
- range