# # Program showing different ways to print # # Mtat Bishop, MHI 289I, Winter 2018 # import math # # the set-up # num = 1 flnum = 2.6 print "The integer is", num, "and the float is", flnum # # two lines of output # print "--------------------------------------" print "Here is one print statement" print "And here is another" raw_input("continue> ") # # one line of output because of the trailing comma # print "--------------------------------------" print "Here is one print statement", print "And here is another" raw_input("continue> ") # # The difference between a , and a + in a print statement # print "--------------------------------------" print "A comma separates this part", "from this part" print "A plus sign separates this part" + "from this part" raw_input("continue> ") # # Note the space between the dollar sign and the number # print "--------------------------------------" print "$", 1.39 raw_input("continue> ") # # No space now, but precision is 6 digits # print "--------------------------------------" print "$%f" % (1.39) raw_input("continue> ") # # Now the precision is 2 digits (.2) # print "--------------------------------------" print "$%.2f" % (1.39) raw_input("continue> ") # # Take the value of pi out to 7 decimal places # print "--------------------------------------" print "The value of pi is %.7f" % (math.pi) raw_input("continue> ") # # This loop shows the decimal points not being aligned # print "--------------------------------------" for i in range(1, 5): print "%.2f" % (math.pi * i) raw_input("continue> ") # # This loop shows the decimal points being aligned # print "--------------------------------------" for i in range(1, 5): print "%5.2f" % (math.pi * i) raw_input("continue> ") # # Here's how you do a print with multiple formats # print "--------------------------------------" print "%d + %d = %d" % (2, 2, 2 + 2) raw_input("continue> ") # # Let's get fancy ... # print "--------------------------------------" print "%(language)s has %(#)03d quote types." % {"#":2, 'language':"Python"} raw_input("continue> ") # # Another way to do the same thing # print "--------------------------------------" print "{0} {1} is different than {0} {2}".format("Python", 2, "3") raw_input("continue> ") # # Now print the value to 7 decimal places # print "--------------------------------------" print "The value of pi is {0:.7f}".format(math.pi) raw_input("continue> ") # # Show the justification of text # print "--------------------------------------" print "This prints to the left in the field: '{0:<6}'".format('text') print "This prints to the right in the field: '{0:>6}'".format('text') print "This prints in the center of the field: '{0:^6}'".format('text') raw_input("continue> ") # # Here's a string format using % # print "--------------------------------------" x = "2 * %f = %f" % (math.pi, 2*math.pi) print x raw_input("continue> ") # # Same thing, using format # print "--------------------------------------" x = "2 * {0:.6f} = {1:.6f}".format(math.pi, 2*math.pi) print x