CS21 In Class: week 1

Unix, Python programs: variables, type, input, expressions, for loops

Setting up a subdirectory for in-class work

First run update21 to create a cs21 subdirectory and a labs subdirectory of it. The labs will contain subdirectories for each lab assignment.
$ cd                       # make sure you are in your home directory
$ ls                       # list contents of your home directory
$ update21                 # creates directories: cs21 and cs21/labs/00
$ ls                       # now a new subdirectory cs21 has been created
cd into your cs21 subdirectory create a directory named 'class' and cd into that directory, and then make a directory named 'week01' and cd into that directory:
$ cd cs21                            # cd into your cs21 subdirectory
$ pwd	          

$ mkdir class                        # make a new directory named class   
$ cd class                           # cd into it 
$ pwd		

$ mkdir week01                      # make a subdirectory named week01   
$ cd week01                         # cd into it
$ pwd           

Now copy over all the files from my public/cs21/week01 directory into your week01 directory (remember to add the dot as the destination of the cp command). From your week01 directory:

$ cp ~newhall/public/cs21/week01/* .
$ ls

Weekly In-class Examples

This week we are going to get up to speed on learning some Unix and vim so that we can write python programs. We are also going to try running and writing some python programs. Remember to look at the Getting Started in Python link for more information about running python programs on our system, and my The Useful Links on the course homepage for other Unix, vim, and python references.

This week we will look at the following exmaples in class:

  1. We are going to start by looking at the firstProg.py program together, so go ahead and open it in vim:
    $ vim firstProg.py  
    This is the command to run the program in the python interpreter:
    $ python firstProg.py

  2. Try modifying the firstProg.py in vim to print out a different string, and run it.

  3. Next we will look at add.py. This program follows the general pattern that all (most) programs follow:
    1. input phase: get data values for program variables
    2. compute phase: do something with the program's data (some operations on variables and values)
    3. output phase: display the results to the user

  4. Finally, we will look at loop.py. This is an example of a program that uses a for loop. A loop construct tells the python interpreter to repeat a set of instructions some number of times.

Running Python Programs

You can run your python program in the python interpreter two different ways (we will use the first way most of the time):
  1. Have the interpreter run your program to completion:
    $ python firstProg.py
      Hello There
  2. Run the interpreter in interactive mode on your program (the python interpreter will continue to run after running your program, and you can enter new python commands at the prompt >>>:
    $ python -i firstProg.py
      Hello there
      >>> print ("I love computer science!")
      I love computer science!
      >>> print (13 + 7 + 11)
To exit the python interpreter hold down the control and the D keys together (CNTL-D)

You can also just start the python interpreter in interactive mode without giving it a python code file to run:

  $ python