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 lab assigment directories and starting point files
Run update21 to get the next lab assignment starting point files. The first time you run update21 it will create subdirectories for all lab assignments. Subsequent runs will copy over starting point files for each weekly lab assignment. You will cd into the specific lab number subdirectory and do all your work in that directory. Here is example output from the very first run (note $ is the unix prompt):
$ update21
 course directory does not exist
 running /home/newhall/classes/cs21/s17/scripts/init.sh
 Initial setup complete
 type 'cd cs21' to see files

$ ls             # ls list the contents of the current directory
 cs21/
$ cd cs21        # change into the cs21 subirectory
$ ls             
 labs/
$ pwd            # pwd prints the path of the current directory
 /home/tnas/cs21
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.
$ cd                    # starting from your home directory
$ cd cs21/labs/01/
$ ls
$ pwd
/home/tnas/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, try some of the following:

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:

$ cd
$ ls
Desktop/  Documents/  cs21/
$ cd cs21/labs/00/bio.txt 
-bash: cd: cs21/labs/00/bio.txt: Not a directory
$ cd cs21/labs/00/        
$ pwd 
/home/tnas/cs21/labs/00
$ ls
bio.txt
$ 
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.
$ 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, your name, and the date
  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
  (Tia Newhall, 1/16/2017)
"""
def main():
  print("welcome to cs21")

main()

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.
$ 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 (e.g. cs21/labs/01),and only these files, so be sure your files are in the right directory as outlined in this document.
$ pwd
/home/tnas/cs21/labs/01
$ vim acres_to_yards.py
$ handin21                        # handin21 lists newly changed files 
Submitting the following files:   # since your last handin21 run: 
labs/01/acres_to_yards.py
You can also run handin21 with -v to list recent submissions:
$ handin21 -v
Updates in the past week:
 ...

At the due date and time, we grab the most recent version of your lab solution that you submitted via handin21. We strongly encourge you to not run handin21 after the due date just in case you forget to run handin21we can verify the date of your solution by the file modification date (but remember to run handin21).

Logging out and xlock
Always remember to log out when you are done working in the lab. To log out, choose the logout icon from the center toolbar to get the log out menu. PLEASE only select logout (don't reboot or shutdown the machine).

If you are going to step out of the lab for a few minutes, it is a good idea to run xlock your machine. You can do this by typing in xlock at the shell prompt, or by choosing the xlock icon on the center toolbar.

You should not xlock a machine for more than about 10 minutes so that other students can use the machine; xlock is for a short break.