knerr cs21 notes...

back to schedule

WEEK02: numbers and strings
---------------------------------------------------------------
 W: string and list indexing, slicing, and len; for loop worksheet

LAB2: due next Tuesday
QUIZ1: Friday (do the practice quiz)

string:
  s = "hello"
  mystr = "this is fun!"

list:
  mylist = ["dog", "cat", "pony", "fish"]
  nlist = range(10)

both lists and strings are sequences, so they can be used in for loops!

  for c in "hello":
    print c

  for word in ["dog", "cat", "pony", "fish"]:
    print word

you can also access elements of a string or a list with an index:

  print mystr[0]
  't'

  print mylist[1]
  "cat"

the len() function gives you the length of a string or list:

>>> mylist = ["dog", "cat", "pony", "fish"]
>>> mystr = "this is fun!"
>>> len(mystr)
12
>>> len(mylist)
4

try the for loop worksheet -- make sure you understand what each loop does

   cp /home/jk/inclass/forloopsWorksheet  .

try to write these programs:

$ python its.py 
first name: jeff
 last name: hollinsworth
your ITS username = jhollin1


$ python box.py 
 width: 10
height: 4
@@@@@@@@@@
@        @
@        @
@@@@@@@@@@


   cp /home/jk/inclass/its.py  .
   cp /home/jk/inclass/box.py  .