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.


examples/functional/iterables.py
numbers = [101, 2, 3, 42]
for num in numbers:
    print(num)
print(numbers)

print()

name = "FooBar"
for cr in name:
    print(cr)
print(name)

print()

rng = range(3, 9, 2)
for num in rng:
    print(num)
print(rng)

101
2
3
42
[101, 2, 3, 42]

F
o
o
B
a
r
FooBar

3
5
7
range(3, 9, 2)