WEEK01: intro to python, unix, cs21
-----------------------------------
 F: using editor (vim), loops, range, sequences

Announcements:
  - Lab 0 due Saturday night
  - Lab 1 will be posted sometime Sunday


PRACTICE UNIX (change username to your username):

pwd
ls
update21
ls cs21/inclass
ls cs21/labs
cd cs21/labs/w01-intro
cat JK-takehomepay.py


REVIEW of JK-takehomepay.py

"""
estimate montly take home pay

J. Knerr
Fall 2013
"""

salary = raw_input("Salary: ")
salary = float(salary)
# caclulate monthly take home pay
mthp = (salary/12.0) * 0.7  # assumes gvmt takes 30%
print "Approx monthly take-home-pay = $" + str(mthp)

 - how many functions are called in this program?
 - what does the function raw_input() return?
 - what are the comments in this program? why do we add comments?


LOOPS:

  example of a program that uses a loop:

$ python squares.py 

I will print out the squares of all numbers from 1 to n!

Please enter n: 6
----------------------------------
1 x 1  =  1
2 x 2  =  4
3 x 3  =  9
4 x 4  =  16
5 x 5  =  25
6 x 6  =  36
----------------------------------

  for < var >  in < sequence >:
    do this
    and this
    and any indented line
    over and over and over
  code here is not part of loop

 - code block is the 4 indented lines above
 - code block is executed with var = 1st item in sequence
 -             then again with var = 2nd item in sequence
 -             then again with var = 3rd item in sequence
 -             and so on...

  < var > is a variable you choose (i, x, foo, ch, etc)
  < sequence > is a special python set of values, which
    can be any of these:
         - a string of characters, like "Hello!!"
         - a list of integers, like [1,2,3,4,5]
         - a list of anything, like [1,"hi",5.6,"no]
         - all lines in a file (week 7)


>>> for i in [1,2,3,4,5,6,7]:
...   print i
...   print i*"HI! "
...   print "============="
... 
1
HI! 
=============
2
HI! HI! 
=============
3
HI! HI! HI! 
=============
4
HI! HI! HI! HI! 
=============
5
HI! HI! HI! HI! HI! 
=============
6
HI! HI! HI! HI! HI! HI! 
=============
7
HI! HI! HI! HI! HI! HI! HI! 
=============


  an easy way to create a list is with the range function:

>>> range(5)
[0, 1, 2, 3, 4]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,20,2)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

  and try help(range) to get help with the built-in range() function:


>>> help(range) 
Help on built-in function range in module __builtin__:

range(...)
    range([start,] stop[, step]) -> list of integers
    
    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.



  and here's a for loop example using range:

>>> for i in range(20,1,-2):
...    print "----->" + str(i)
... 
----->20
----->18
----->16
----->14
----->12
----->10
----->8
----->6
----->4
----->2


---> can you write this program?

$ python blastoff.py 

countdown start: 7
 
. . . . 7
. . . . 6
. . . . 5
. . . . 4
. . . . 3
. . . . 2
. . . . 1
blastoff!!