Iterators
We use for statement for looping over a list.>>> for i in [1, 2, 3, 4]:
... print i,
...
1
2
3
4
>>> for c in "python":
... print c
...
p
y
t
h
o
n
>>> for k in {"x": 1, "y": 2}:
... print k
...
y
x
>>> for line in open("a.txt"):
... print line,
...
first line
second line
There are many functions which consume these iterables.
>>> ",".join(["a", "b", "c"])
'a,b,c'
>>> ",".join({"x": 1, "y": 2})
'y,x'
>>> list("python")
['p', 'y', 't', 'h', 'o', 'n']
>>> list({"x": 1, "y": 2})
['y', 'x']