Generators for Engineers
A gentle introduction to infinite sequences.
Comments are moderated. It may take a few minutes before your comment appears.
Markdown is supported in your comments.
If you've heard of the
itertools module, feel free to skip this section. If you do not already love
itertools, read this.
Itertools provides high level operations for infinite lists. There are also a bunch of awesome mathy parts, such as
itertools.permutations(). We'll be using
count() is pretty simple. It acts like
xrange(0, infinity). You can specify any starting point, but it will always keep on counting up.
islice() is a bit more tricky. It has similar semantics to
slice() is the underlying function to Python's
list[start:stop:interval] notation. All these snippets do the same thing, produce even numbers between 4 and 16.