CS 111: Introduction to Computer Science

Writing and Running Python Programs

This is another exercise for you to do, with nothing to hand in. The goal of this exercise is to make sure you know how to create and run a simple Python program.

  1. This exercise assumes you have gone through the first few tutorials in this Introduction to Unix. In particular, you need to be familiar with the idea of your "working directory" and the "cd" and "ls" commands.
  2. Go to one of the CS labs (CMC 304 or 306) and login to your MacOS account. If the computer you choose is booted into Windows when you arrive, restart it and select MacOS when you are given a choice.
  3. Get the Terminal program onto your dock. The dock is the row of icons for commonly-used programs that appears at the bottom of your Mac desktop. If you see an icon that looks like a computer screen with a ">_" on the screen, then Terminal is ready to run for you. If that icon is not already on your dock, then open "Macintosh HD" and navigate to Applications/Utilities/. In the Utilities folder you will find Terminal. Drag its icon onto your dock.
  4. Get the TextWrangler program onto your dock. You can find TextWrangler in Applications/CarletonApps/.
  5. Launch TextWrangler by clicking on its icon in the dock.
  6. In an empty TextWrangler document, type one line:

    print 'hello, world!'

    Save your document with the file name hello.py. Make sure you keep track of the folder in which you have saved hello.py. You may wish to create a special folder for programming experiments.

  7. Launch Terminal by clicking on its icon in the dock.
  8. In Terminal, cd to the folder where you saved hello.py.
  9. Type the command:

    python hello.py

    (Don't forget to hit the Enter key after you have typed the command.) If everything has gone smoothly, you should see the "hello, world!" greeting. If you see an error message saying something like "hello.py: no such file or directory," you are probably not currently in the directory where hello.py is stored. Use "ls" and "cd" to help you move to the right directory.

  10. While you're here, try running hello.py a different way, using the interactive Python interpreter. Type the command:

    python

    This should display some introductory text, followed bye a prompt that looks like this:

    >>>

    At this prompt, type:

    import hello

    This should cause your hello.py program to be loaded and executed, so you should see your "hello, world!" message again.

    To exit from the interactive Python interpreter, type:

    exit()
  11. All done. Congratulations!