Homework #1: Introduction to the Class

In this first homework, we'll test out some basic python skills. As mentioned in class, think of these first few homeworks and lectures as learning how to draw or use a paint brush. Once you have some basic technique then you'll be able to keep up with the later parts of the course.

Note: Since quite a few of you use mac at home, I'm worked out a short screencast video of how to get python running on your machine in interactive or batch mode. This should help clear up any problems people have working from home. (sorry the volume is so low, i'll work on getting a microphone if i do this again, but if you listen with headphone you should be able to hear better, or just watch!)

Note: there are 4 steps!

Step 1: Install Python on your home computer

Good news, if you home computer is a Macintosh or Linux-based computer python should already be installed. To run it you just need to locate the Terminal program on your computer and get into the command-line (in Applications/Utilities folder). You will, however, need to find a good text editor. I recommend textmate but there are many alternatives online (smultron is free, see also this list of python-aware editors). It is up to you which one you prefer to use.

If you run windows, Python doesn't come pre-installed, but is fortunately very easy to install. Go to this link and download the Python 2.5.1 Windows (binary) installer. This includes the python IDE (IDLE) which includes a simple text editor, although you can swap it out for another one. To run a script in batch mode open a new files, save to disk someplace, then choose "Run Module" from one of the drop down menus.

Step 2: Open an interactive python shell and enter some commands

Ok now, you've got python, let's test it in interactive mode. Enter the following command one at a time into the python interpreter. Try to understand what each line is doing. Does anything seem odd or unexpected to you? Don't worry if you don't understand exactly why things are doing what they, are but just keep a mental note and ask in class why a particular command generated output that you did not expect. At the end of your session, copy the output into an email and send it to me with any questions you may have had.

print "hello, world"
yearsold = 25
print "I am %s years old" % yearsold
yearsold = 26
print "I am %s years old" % yearsold
yearsold = 23.45
print "I am %s years old" % yearsold
yearsold = 100.0/33.0
print "I am %s years old" % yearsold
yearsold = 100/33
print "I am %s years old" % yearsold
a = 1
print a
# this is a comment it does nothing
# after you type the '#' python ignore the rest
print "hello, world" # see
myname = raw_input("What is your name? ")  # enter your name when prompted
print myname
print "Hi, my name is %s" % myname
myname = raw_input("Enter a different name: ")
print "Hi, my name is now %s" % myname
a = 1
b = 2
c = b
print "c is equal to %s" % c
b = 5
print "c is still equal to %s" % c
print "b is now equal to %s" % b
a = a + b
print "a is now equal to %s, can you figure out why?" % a

Step 3: Get a coffee, and work through this online tutorial at your own pace