You can create your own iterator by using the keyword yield. For example, a generator for odd numbers smaller than can be defined by:
def odd_numbers(n):
"generator for odd numbers less than n"
for k in range(n):
if k % 2 == 1:
yield k
Then you can use it as follows:
g = odd_numbers(10) for k in g: ... # do something with k
Or even like this:
for k in odd_numbers(10): ... # do something with k