CS 111: Introduction to Computer Science
Getting started, with a focus on strings
The lab exercises
Each of these programs contains instructions for you.
To get started, save a copy of each of the files listed below in your working directory,
open each file in turn, and follow the instructions, trying to answer the questions posed there.
You do not need to hand in any of your answers. However, you should try hard to understand
how each of the programs works, line by line. Ask questions during lab, during prefect sessions,
in my office hours, or in class.
There are lots of ways to explore sample programs like these, but here's
a recommended procedure. For each sample program:
- Download the program to your working directory by right-clicking
on the link and choosing "Save Link As" or whatever similar option your
browser gives you.
- Open the sample program in a text editor (TextWrangler is a good choice).
- Read the program with your partner. Discuss what you think the
program will do.
- Run the program (by typing, for example, "python strings0.py" in the
Terminal window. See if your predictions about the program were true.
If not, try to figure out why not.
- Try to answer the questions in the program's comments. You may want
to test your answers by making changes to the program in your text
editor, saving the changes, and running the program again. Play around
with the program until you feel comfortable with all its operations.
- Move on to the next program.
Your assignment, due 11:59PM Wednesday
Write a Python program that asks the user for a string, and then
prints out the following items, one item per line:
- The user's string, but all upper case.
- The user's string, but with all spaces removed.
- The user's string, but with each space replaced by a pound sign (#).
- The first half of the user's string. (It the string has
an odd number of characters, then print the smaller half. For
example, for an 11-character string, print 5 characters.)
- The number of letters in the user's string (see the "isalpha()"
operation at
http://docs.python.org/library/stdtypes.html#id4 for a handy
tool.
- The user's string, but backwards.
Your program should begin with a comment like the ones in my sample
programs that includes the name(s) of the student(s) who wrote the
program. If you work with a partner, you only need to submit one copy
of the program.
Submit this program by 11:59PM on Wednesday, April 8. Submit your program either
by dragging it to the hand-in folder for this course, or by using the
Homework Submission
Program (HSP). HSP is generally pretty convenient if you are working
on one of the 3rd-floor CMC lab computers. If you're on your own computer
connected to ResNet, it's probably a lot easier to drag your work to
hand-in.
Start early, have fun, and keep in touch.