Introduction to Python

To get started, create a directory called intropython. Do so in a terminal window. Feel free to go back and peek at the UNIX tutorial if you need to jog your memory on how to do so. (Don't use Finder to do this; you'll be much faster and will be able to do much more with the computer if you push through for a while and use the UNIX prompt as much as possible. Ask for help if you like.) Use the cd command to navigate to the intropython directory that you have just created.

The first thing you'll need to figure out is which text editor you'd like to use. Word processors (like Microsoft Word, OpenOffice Writer, and so on) are terrible for writing programming code because they insert all sorts of formatting details that are irrelevant to programming. Bare-bones editors (like TextEdit, Notepad, Wordpad, and so on) suffer from the opposite problem: they're so limited in functionality that you can't do anything you'd actually want to do with them. There are a variety of text editors that work well for programming purposes. We have a number of them installed in the department: you can choose which one you want to use. Here are some choices.

Decide on a text editor, and start it up. Feel free to change your mind over the course of the term -- learning new tools is fun.

A first Python program

Save the program hello.py into your intropython directory. To do that, right click on the link for hello.py, and choose "Save Link As..." Open up this program in your text editor, and take a look at it.

To run your program, open up a terminal window (there's an icon in your dock), and type:

python hello.py

in the terminal. If all goes well, your program should print "Hello, world!" to the terminal window. If this doesn't work, or if errors result, get help from from the prof, the prefect, or a lab assistant.

EXERCISE 1: Modify the program so that it prints out "Welcome to Intro CS, [your name]."

Python input

This time, work with at input.py. Save it to your intropython directory, then read it carefully. Run it a couple of times. (Again, get help if you have any error messages.)

EXERCISE 2: What happens if you enter a number when prompted for your name and a name when prompted for a number? If you get a message, what did the message say? What do you think it means? Put your answer to this question as a comment (a line in your Python program that begins with a #) into input.py.

EXERCISE 3: Modify the program so that it asks for the current year, and the year you were born. Your program should then print out the difference between the two, which is (approximately) your age.

Turning in your work

Zip up all of your programs and the answers to the questions into a single file before submitting. On the department Macs, you can do this at a command prompt by typing

zip intropython.zip *

which will create a single file called intropython.zip. This is the file that you should submit.

Submit your work via Moodle.


Authored by Dave Musicant and Amy Csizmar Dalal.