Go through as much of this python tutorial as you can stand. If this tutorial is too fast-paced or too difficult, consult this page for a list of python tutorials at all levels. This tutorial is also pretty good.
Here are some simple python scripts that you should write. Don't forget the magic line:
#! /usr/bin/env python
in the first line of your file. Make your files executable by typing
chmod +x <filename>
at the Unix prompt and test them. NOTE: scripts designated
[Advanced] are optional but are worth extra credit if your total mark
is less than perfect. Also note that in python-speak, the words "script" and
"program" mean exactly the same thing. Also also, you won't have to
make your scripts executable in this way for any labs other than this one; we
cover this here because it's a common way to use python and you should know
how it works. You're free to continue to make your later scripts executable
if you like, of course. For this lab only, don't put the .py
suffix on your program names; for all subsequent labs, do add the
.py suffix.
Make sure that scripts that require command-line arguments check these arguments for validity (correct number of arguments and correct types), and output a usage message if the arguments are invalid. See the style guide for more details on how to do this (specifically, this section). This applies to ALL programs you hand in in this track; if you don't do this I'll make you rewrite the offending program(s).
While you're at it, run your completed programs through the style checker to check your code formatting style. If it complains about tabs, see this section of the style guide for advice on how to configure emacs to not output tabs. If you're not using emacs, you're on your own.
A script called hello that prints "hello, world!".
A script called solve_quadratic_equation that computes
solutions for arbitrary quadratic equations. Recall that quadratic equations
are of the form a*x^2 + b*x + c = 0, where x is the variable
that you need to solve for and a, b, and
c are real numbers that you enter. Your program will need to
import the math library. It will be invoked like
this:
solve_quadratic_equation 2 3 4
(where a = 2, b = 3, and c = 4) and
will give this kind of output:
Quadratic equation: 2*x^2 + 3*x + 4 = 0
Solution 1: <whatever>
Solution 2: <whatever>
If the solutions are complex, it should print that there are no real
solutions. Print the solutions as numbers, not as formulas. Assume that all
the arguments are numbers and that a will always be nonzero.
Use the float function to convert the strings corresponding
to the command-line arguments for a, b, and c to floating-point numbers.
NOTE: Make sure that if the user inputs incorrect arguments
(e.g. too few or too many arguments or arguments that are non-numbers) that
the program exits gracefully with a usage message. Actually, checking for
non-numbers is best done with exception handling, which we cover in next
week's lecture, so you aren't responsible for it, but if you can do it
(i.e. because you've learned it from the tutorials) it's extra credit.
Hint: calling float() or string.atof() on an
argument that can't be converted to a float raises a ValueError
exception.
[Advanced]: Don't assume that all arguments are numbers; print out a usage message if any of them aren't. Use exception handling for this.
[Advanced]: Make it work for complex solutions too. Note that
python handles complex numbers but the sqrt function doesn't.
You might find the cmath (complex math) module useful.
[Advanced]: Make it work for the cases where a = 0,
b = 0, and/or c = 0 too.
A script called count_lines that reads in a file (the
filename is a command-line argument) and prints out the file name and how
many lines there are in the file. Don't use the readlines
method of file objects, because that reads the entire file into memory, which
is a bad idea for large files. Again, make sure that if the program is
called with e.g. too many or too few arguments that a usage message is
printed and the program aborts. [Advanced]: Make it work for an
arbitrary number of file names on the command line. In this case, print out
a separate line for each file giving the file name and the number of lines in
that file.
A script called capitalize that reads all the lines from
a file, capitalizes them, and writes them out to a different file. The
command line should look like this:
capitalize myfile myfile_caps
where myfile is the original filename that you read from and
myfile_caps is the output filename that you write to.
Use the upper function of the string module to
capitalize the lines. Note that in this context, "capitalize" means that all
occurrences of lower-case letters are to be made into the corresponding
capital letters, as opposed to e.g. just capitalizing the first letter
of each word. Once again, make sure that if the program is called with
e.g. too many or too few arguments that a usage message is printed and
the program aborts. Also make sure that you explicitly call the
close() method on any file you open.
Run the program "http-getfile.py" as follows:
python http-getfile.py
or
python http-getfile.py <url> <filename>
to download some random web pages of your choice. Notice how little code (19 lines) it requires to access the internet when using python. NOTE: there is nothing to hand in for this; it's just for fun.
[Advanced]: For serious Linux users: download the python source code from the python home page, and compile it and install it from scratch on a Linux machine. Make sure that your path is set up so that your newly-compiled python will be selected instead of the python version that comes with your computer.
That's plenty for this week.