15-105 SPRING 2009 [CORTINA]

Using Python - Mac OS & Linux Instructions

INSTALLING PYTHON

If you are using Mac OS or Linux, a Python interpreter is already available for you.

CREATING A PYTHON DIRECTORY

Using the Terminal application, type the command ls to list all the folders and file in your current folder. (Folders are also known as directories.) Then use the cd command to "change directory" (or change folder) to a subdirectory in the current directory. For example, once you start the Terminal window, you can enter ls (lowercase L followed by lowercase S) and see something maybe like this:

Desktop		Downloads 	Movies		Pictures	Sites
Documents	Library		Music		Public

Then typing cd Documents would take you into the Documents folder. You can make a subdirectory named 15-105 under Documents by typing

mkdir 15-105

when you are in the Documents directory. Then you can move to that folder by typing cd 15-105. Now that you're in this subdirectory, you can create your Python programs here (e.g. gcd.py). If you don't see the Documents directory, you can always just make the 15-105 directory as shown above from your "home" (starting) directory.

EDITING PYTHON PROGRAMS

You can use any text editor to write your Python programs. Be sure you can save files in plain text format. For MacOS and Linux, you can use the editor pico through the Terminal window.

To use pico, navigate to the directory/folder where you want your file using the Terminal window, and enter

pico filename

For example, if you want to enter code for the GCD program, you might enter:

pico gcd.py

You will see your Terminal screen change with a blank are to type and a series of commands along the bottom:

^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Page ^K Cut Text   ^C Cur 
Pos
^X Exit      ^J Justify   ^W Where Is  ^V Next Page ^U UnCut Text ^T To Spell

You can type your program text in the blank area, using the arrow keys to move the cursor around and using the delete/backspace key to delete text where the cursor is. You can access the commands shown along the bottom by holding the CONTROL key down and pressing the appropriate letter. Here are the most useful commands:

Control-K   Cuts the line of text where the cursor sits
            (use more than once in sequence to cut multiple lines).
Control-U   Pastes the cut text at the location of the cursor.
Control-Y   Scroll up one page at a time.
Control-V   Scroll down one page at a time.
Control-C   Tells you what line the cursor is on.
Control-R   Loads the editor with the contents of a file at the
            current cursor location.
Control-O   Saves the contents of the editor into a file.
Control-X   Exits out of pico (and saves if necessary).

If a command needs additional info (like a file name), pico will prompt you at the bottom of the screen. Note that the command options might change on the bottom of the screen in this case.

If you start pico (as shown above) and the file already exists, pico will load the file so you can edit it. If the file doesn't exist, you will start with a blank editor. Load only plain-text files into pico. Do NOT try to load in an image file, music file, or other binary encoded file or you will get loads of garbage in the editor.

RUNNING PYTHON

INTERACTIVE MODE
Open a Terminal window and type python to start the interactive Python interpreter. You can now enter python commands one at a time with immediate feedback.

COMMAND LINE MODE (TO RUN A COMPLETE PROGRAM)
You can navigate to a folder that has a Python program file (use the cd command) and run the program directly.

Once you have a Python program written and saved in your 15-105 subfolder, you can run it by typing python programname. The programname should end with the suffix .py to flag that it is a Python program. For example:

python gcd.py

Remember, for the command above to work, you must be working in the folder where the python program resides so Python can find it. If you get an error message like "file not found", then the program you want to run is not named correctly or it is not in your current directory. See a CA for extra help.