Introduction to Python

To get started, create a directory called intropython. Feel free to go back and peek at the UNIX tutorial if you need to jog your memory on how to do so. Use the cd command to navigate to the intropython directory that you have just created.

Start up jedit, which is the text editor that we will be using to create and modify Python programs. To do so, type jedit & (don't forget the ampersand) in your terminal window. Play around with it a little bit: try to type some text, save it, close jedit, and see if you can open it again.

A first Python program

Look at the program hello.py. In your editor, type in this program exactly. You might find it easiest to put the two windows side-by-side on your computer screen, or perhaps you might wish to print out hello.py first and work from the printout. Don't copy and paste the code. You'll learn more if you go through the experience of typing it in and fixing any errors. Save your file under the name hello.py.

To run your program, 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, look at input.py. Type this code into jedit, and save the file to your intropython directory. 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 Linux systems, 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.