out[ ]any iterator is any object that will yield objects to the python interpreter when used in context like for loop most methods expecting list or list-like object will also accept any iterable object this includes built-in methods such as minmaxand sumand type constructors like list and tuplein [ ]list(dict_iteratorout[ ][' '' '' ' generator is simple way to construct new iterable object whereas normal functions execute and return single valuegenerators return sequence of values lazilypausing after each one until the next one is requested to create generatoruse the yield keyword instead of return in functiondef squares( = )for in xrange( )print 'generating squares from to % ( * yield * when you actually call the generatorno code is immediately executedin [ ]gen squares(in [ ]gen out[ ]it is not until you request elements from the generator that it begins executing its codein [ ]for in genprint xgenerating squares from to as less trivial examplesuppose we wished to find all unique ways to make change for $ ( centsusing an arbitrary set of coins you can probably think of various ways to implement this and how to store the unique combinations as you come up with them one way is to write generator that yields lists of coins (represented as integers)def make_change(amountcoins=[ ]hand=none)hand [if hand is none else hand if amount = yield hand for coin in coinsensures we don' give too much changeand combinations are unique if coin amount or (len(hand and hand[- coin)continue for result in make_change(amount coincoins=coinshand=hand [coin])yield result the details of the algorithm are not that important (can you think of shorter way?then we can write appendixpython language essentials www it-ebooks info