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)

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

  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]

  print mylist[1]

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

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

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

   cp /home/jk/inclass/forloopsWorksheet  .

try to write these programs:

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

$ python 
 width: 10
height: 4
@        @
@        @

   cp /home/jk/inclass/  .
   cp /home/jk/inclass/  .