First Summer 2008


Lecture Two: Our First Python Programs.
The first two programs simply discuss printing strings:

print """
     4
    4
   4
  4   4
 44444444
      4
      4
"""

The next program is almost the same:

print """
     \"
    \"
   \"
  \"   \"
 \"\"\"\"\"\"\"\"
      \"
      \"
"""

However special characters require special treatment.

There isn't much that we can say about the next one:

print """
          .8.           8 888888888o          ,o888888o.
         .888.          8 8888    `88.       8888     `88.
        :88888.         8 8888     `88    ,8 8888       `8.
       . `88888.        8 8888     ,88    88 8888
      .8. `88888.       8 8888.   ,88'    88 8888
     .8`8. `88888.      8 8888888888      88 8888
    .8' `8. `88888.     8 8888    `88.    88 8888
   .8'   `8. `88888.    8 8888      88    `8 8888       .8'
  .888888888. `88888.   8 8888    ,88'       8888     ,88'
 .8'       `8. `88888.  8 888888888P          `8888888P'
 """

And with the next one things become really relevant (meaningful):

a = raw_input("Enter the lenght of the first side: ")
b = raw_input("Enter the lenght of the second side: ")
c = raw_input("Enter the lenght of the third side: ")

a = float(a)
b = float(b)
c = float(c)

s = (a + b + c) / 2.0

area = (s * (s - a) * (s - b) * (s - c)) ** 0.5

print "The area of the triangle is:", area 

Next one is a bit tricky. Can you say what is tricky about it?

a = raw_input("Enter your text:")
a = a.replace("e", "egge")
a = a.replace("a", "egga")
a = a.replace("e", "egge")
a = a.replace("i", "eggi")
a = a.replace("o", "eggo")
a = a.replace("u", "eggu")
print a

Here's the program that calculates the maximum:

one = int(raw_input("Please enter the first number: "))
two = int(raw_input("Please enter the second number: "))
print one, "+", two, "=", one + two
print one, "-", two, "=", one - two
print one, "*", two, "=", one * two
print "avg(" + str(one) + ", " + str(two) + ") = ", (float(one) + two) / 2
print "dist(" + str(one) + ", " + str(two) + ") = ", abs(one - two)
print "max(" + str(one) + ", " + str(two) + ") = ", (one + two + abs(one - two)) / 2
print "min(" + str(one) + ", " + str(two) + ") = ", (one + two - abs(one - two)) / 2

Here's the last program, that gives change:

due = raw_input("Due: ")
pay = raw_input("Pay: ")

change = int(100 * float(pay)) - int(100 * float(due))

print change / 25, " quarters"
change = change % 25

print change / 10, " dimes"
change = change % 10

print change / 5, " nickels"
change = change % 5

print change, " cents"

If we understand these programs we are ready for the new lab and the homework.


Last updated: May 07, 2008 by Adrian German for A201/A597