WEEK02: numbers and strings
------------------------------------------------
F: QUIZ 1, string and list indexing, slicing, and len() function
LAB2: due next Tuesday
REVIEW...functions we've learned so far:
input()
raw_input()
range()
float()
int()
str()
type()
and today we will learn len()
STRINGS and LISTS are sequences!!
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.py .
try to write these programs:
$ python its.py
first name: jeff
last name: hollinsworth
your ITS username = jhollin1
$ python splitphrase.py
This program does some cool string processing.
Please enter a phrase below.
phrase: computer science is really fun!!
computer science
--------------------------------
is really fun!!