type(abc_generator()out[ ]generator in ]generate pi value pi/ / / / def pi_series()sum while truesum sum / yield *sum - runs forever we can break with counterbut it is not good idea for in pi_series()print(iin ]def firstn(gn)for in range( )yield next(gprint(list(firstn(pi_series() )) context manager is used to allocate and release some sort of resource when we need it which means that before we start block we open fileand when we are going outthe file is automatically released if we don' closeit remains open in file system closing programit would close good practice is to always close with context managersthe benefit is no need to close the issue is with the exceptions with withthe exception is caught and handled context manager is general concept the concept is as follows with device()before check device start device we enter the block we do something after we execute stop block in case of exceptions we are sure that the after part will be executed in ]import csv with open('example txt'' 'as outcsv_out csv writer(outcsv_out writerow(['date''events']