Python Getting Started

Basic instructions for starting lab assignments
Getting started on a new python program can be a bit intimidating at first. Here are step-by-step instructions to editing, saving, and running a program.
Getting to the right directory
The first step is to open a terminal and get to the proper directory where you want to save the file. Start by opening a terminal window by clicking on the terminal icon terminal on the center toolbar. You will automatically be placed in your home directory. Typically you do not want to put files here, but rather you want to edit files in a cs21 sub-directory. Use the cd command to get to proper directory. For example, for lab 01, you need to save files in the cs21/labs/01 directory.
onion[~]$ cd cs21/labs/01/
onion[01]$ ls
onion[01]$ pwd
/home/adas/cs21/labs/01
Use TAB-completion to help navigate the directories quickly.

If you get a No such file or directory error when you type cd, double check your spelling, or use mkdir to create the directory that is missing. Instructions for using mkdir will typically be posted on the lab assignment or inclass exercise.

If you get a Not a directory error when you type cd, you went one level too far. See a sample error and fix below:

onion[~]$ cd
onion[~]$ ls
Desktop/  Documents/  cs21/
onion[~]$ cd cs21/labs/00/bio.txt 
-bash: cd: cs21/labs/00/bio.txt: Not a directory
onion[~]$ cd cs21/labs/00/        
onion[00]$ pwd 
/home/adas/cs21/labs/00
onion[00]$ ls
bio.txt
onion[00]$ 
Edit the file
Once you are in the proper directory, use vim or gvim to edit the file. The easiest way to call vim is to specify the filename after the vim command.
onion[01]$ vim acres_to_yards.py
If the file already exists, the above command will edit the file. If the file does not exists, the file will be created the first time you save your vim session. Press i to start editing in vim. When you would like to save, type <ESC>:w. To go back to editing, type i again.

Remember the key elements of a basic program:

  1. A block comment in triple quotes describing in plain english what the program does at a high level
  2. A function definition that will contain as a body your main program
  3. A call to the function defined above so that your program will actually do something when run

A full sample program is shown below:

"""
  A sample python program

  Author: Andrew Danner
  Date(s): 02 September 2008
"""

def welcome():
  print "welcome to cs21"

welcome()

Run your progam
I prefer to have one terminal window open for editing in vim and one terminal for running my program in python. Click on the terminal icon terminal on the center toolbar again to open a second terminal window. Use cd as described above to navigate to the same directory as the file you are editing. Once in the proper directory, type python followed by the name of your program.
onion[01]$ python acres_to_yards.py
If there are errors or things you would like to change, go to your vim window and make changes. Don't forget to save your changes before trying to run the program again using python.
Clean up and handin21
When you are done editing your python file, remember to quit vim using <ESC>:wq. Run handin21 from the linux prompt to submit your work to your instructor. You can run handin21 as often as you like, but the program will only keep the latest copy. handin21 will submit all files in the current lab directory and only these files, so be sure your files are in the right directory as outlined in this document.