#!/usr/bin/env python # # shows the use of some functions/methods, but primary # purpose is to show the use of the % formatting strings # that help us format our output. # # don't worry about the math/computation, but focus on how # the output format is used to line up/format the output # # ALSO - please notice the use of blank lines to *group* # related chunks of code. You should do the same. # # e.bonakdarian oct 2009 import math ######################################################## def poor_output(): # without any formatting line = 1 for i in range(-100, 1200, 100): print 'line:', line,' - ', i, ' times', math.pi, 'is', (i * math.pi) line = line + 1 ######################################################## def nicer_output(): # using output formatting, code is easier # to format and read. # make changes to the formats and see what # can change. # line = 1 for i in range(-100, 1200, 100): print('line: %3d: - %5d times %12.10f is %16.10f' % (line, i, math.pi, (i*math.pi))) line = line + 1 ######################################################## def main(): poor_output() print '------------------------------------------' print 'nicer output' print nicer_output() print '------------------------------------------' print 'some string formatting' print poet = 'Neruda' print 'poet: %s!' % poet print 'poet: %20s!' % poet print 'poet: %-20s!' % poet main()