Lab 1: First python programs

Comp 150, Dordal, January 27 (Mozart's 250th birthday), 2006

Follow along with the following as I talk about them in class. Your assignment is to email me a working python file.

To start python:

Then create a file named something like "lab1.py" in your startup directory, and start up any text editor on it. After saving your work, you can load the file with the command
	import lab1

The first thing you should try is to paste the following function into your lab1.py file,

def hello() :
    print "hello",
    print ", world"
save and load it, and then run it within python using lab1.hello(). Do you get one line of output or two? Try removing the comma at the end of the first print line and see how things change.

As of this writing, the only way I can get the lab version of python to accept a changed function definition in a file is to exit and restart python after saving the file, and then re-importing. This is annoying. Make sure you're comfortable loading a changed file (either using the exit/restart workaround or by some other method) before proceeding further.

Late-breaking news: You can reload a module with
      >>>  reload(lab1)

Your actual assignment is to complete the following three functions and email the lab1.py file to me (pld@cs.luc.edu).

1. Quadratic Formula

On page 56 of Zelle is a function that prompts the user for the three coefficient input values a, b, and c, and then prints out the two roots of the quadratic equation a*x*x + b*x + c = 0. You are to modify this as follows:

To test, run lab1.quad(1,-3,2) and a couple other examples. You may assume the discriminant b*b-4*a*c is always positive; that is, ignore cases when it isn't.

2. Factorial limit

Here is a definition of the factorial function:

def fact(n):
    prod = 1
    for factor in range(1,n):
        prod = prod * factor
    return prod
Paste this into your lab1.py file. Then define a python function f(n) = n/fact(n)**(1/n) and see what happens to f(n) as you make n large. Does it seem to approach a limit? Note that you'll have the same integer-division problem with the exponent (1/n) as you had in the preceding example; try 1.0/n. You also have another problem as n gets large. How large an n will work here?

3: boldify

Web pages are based on html-format files. Html is a text-based format for representing text that may contain font changes. For example, to express bold:

      These words are bold.

you put "<b>" before the bold words and "</b>" after:

      These <b>words</b> are <b>bold</b>.

What you are to do is to write boldify(str, word) that takes a string str and a word word occuring within it, and and adds the <b>/</b> tags above around the first occurrence of the given word within str. That is,

       lab1.boldify("here is a long line", "long")

should return the string

      "here is a <b>long</b> line"

This is pretty easy in python using the following string tools (summarized in Zelle on pages 82 and 94):

Test your boldify using a word in the middle of str and also the first and last word of str. You may ignore the case where word does not occur in str.