

The file has two columns of data separated by white space. #! /usr/bin/env python3 """ Generate samples of a simple sin() wave and save them to a file. This is a good time to make the kids familiar with the terminology Manipulation is one of the big steps in becoming a confident programmer. This is very important: this kind of file close () main ()Īt this point we are comfortable with basic programming patterns for append ( triplet ) return summed_data def write_file ( fname, dataset ): f = open ( fname, 'w' ) for triplet in dataset : f. append (( x, y )) return dataset def append_sums ( dataset ): sum_y = 0 summed_data = for pair in dataset : sum_y = sum_y + pair triplet = ( pair, pair, sum_y ) summed_data. split () x = float ( words ) y = float ( words ) dataset. #! /usr/bin/env python3 """show a simple paradigm for writing a file after reading it and adding some content to it""" def main (): fname = 'simpledata.dat' # the file we wrote out by hand dataset = read_file ( fname ) print ( 'I just read file %s with %d lines' % ( fname, len ( dataset ))) print ( 'I will now print the first 10 lines' ) for i in range ( 10 ): print ( dataset ) print ( 'I will now modify the data' ) summed_data = append_sums ( dataset ) write_file ( fname + '.sums', summed_data ) print ( 'I wrote the modified data to %s ' % ( fname + '.sums' )) def read_file ( fname ): dataset = f = open ( fname, 'r' ) for line in f. Appendix: An itinerary for guest lectures

A workshop on programming by yourself (!)
