generator functions generator function or generator method is one which contains yield expression when generator function is called it returns an iterator values are extracted from the iterator one at time by calling its __next__(method at each call to __next__(the generator function' yield expression' value (none if none is specifiedis returned if the generator function finishes or executes return stopiteration exception is raised in practice we rarely call __next__(or catch stopiteration insteadwe just use generator like any other iterable here are two almost equivalent functions the one on the left returns list and the one on the right returns generator build and return list def letter_range(az)result [while ord(aord( )result append(aa chr(ord( return result return each value on demand def letter_range(az)while ord(aord( )yield chr(ord( we can iterate over the result produced by either function using for loopfor examplefor letter in letter_range(" "" ")but if we want list of the resultant lettersalthough calling letter_range(" "" "is sufficient for the left-hand functionfor the right-hand generator function we must use list(letter_range(" "" ")generator functions and methods (and generator expressionsare covered more fully in this method provides the del [keysyntax if the key is not present the sortedlist remove(call will raise valueerror exception if this occurs we catch the exception and raise keyerror exception instead so as to match the dict class' api otherwisewe return the result of calling the base class implementation to delete the item with the given key from the dictionary itself def setdefault(selfkeyvalue=none)if key not in selfself __keys add(keyreturn super(setdefault(keyvaluethis method returns the value for the given key if the key is in the dictionaryotherwiseit creates new item with the given key and value and returns the value for the sorteddict we must make sure that the key is added to the keys list if the key is not already in the dictionary