WEEK05: functions and lists
---------------------------------------------------------------
W: review functions, scope, misc list/object info
LISTS (needed for Lab 5):
- empty list: mylist = []
- add to a list: mylist.append(5)
- can set up lists by hand: mylist = ["fish", "dog", "cat", "pony"]
- lists we've already seen: x = range(10)
- lists are a type of sequence, so we can use them in for loops:
for i in range(15):
or
for animal in mylist:
OBJECTS: everything in python is an object
using object methods: object.method()
Examples of objects and their methods:
mylist.append("unicorn") append is a list method
"HELLO".lower() lower is a string method
IN operator: returns True if item can be found in sequence
>>> mylist = ["fish", "dog", "cat", "pony"]
>>> "jeff" in mylist
False
>>> alphabet = "abcdefghijklmnopqrstuvwxyz"
>>> "k" in alphabet
True
>>> "G" in alphabet
False
>>>
see help function in python to learn about str and list methods:
>>> help(list)
>>> help(str)
>>> name = "jeffrey"
>>> name.isalpha()
True
>>> name.isdigit()
False
>>> name.upper()
'JEFFREY'
>>> name.capitalize()
'Jeffrey'
>>> name.replace("f","z")
'jezzrey'
>>> print name
jeffrey
>>> text = "this is really fun!!"
>>> text.split()
['this', 'is', 'really', 'fun!!']
>>> text.split()
>>> text.split("real")
['this is ', 'ly fun!!']
MUTABLE vs IMMUTABLE:
- strings are immutable (you can't change them), lists are mutable
>>> S = "jeffrey"
>>> L = list(S)
>>> print L
['j', 'e', 'f', 'f', 'r', 'e', 'y']
>>> S[0] = "J"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
(strings are immutable)
>>> L[0] = "J"
>>> print L
['J', 'e', 'f', 'f', 'r', 'e', 'y']
>>> "".join(L)
'Jeffrey'
REVIEW of FUNCTIONS:
def main():
"""
get a string and a letter from the user, then tell
them how many of that letter were in the string
"""
print "\nEnter a string and a letter and I'll tell you how"
print "many of the given letter are in the string...\n"
ustring = raw_input("string: ")
uletter = raw_input("letter: ")
numletter = howMany(ustring, uletter)
if numletter == 1:
print "\nThere is %d %s in that string.\n" % (numletter, uletter)
else:
print "\nThere are %d %s's in that string.\n" % (numletter, uletter)
# --------------------------------------------------------- #
def howMany(s, ch):
"""
given a string, s, and a character, ch, figure out and
return the number of ch's in s
"""
n = 0
for letter in s:
if ch == letter:
n = n + 1
return n
# --------------------------------------------------------- #
main()
- how many parameters does the howMany function have?
- what does it return?
- in the call to howMany() in main, what are the arguments?
YOUR TURN:
- imagine a password program:
$ python passwd.py
password must be at least 8 chars long with 2 digits in it
password: hello
too short!!
BAD password
$ python passwd.py
password must be at least 8 chars long with 2 digits in it
password: hello class this is cool
not enough digits!!
BAD password
$ python passwd.py
password must be at least 8 chars long with 2 digits in it
password: 4getM3n0t
good password
- if main() looks like this, write the passwordCheck() function:
def main():
"""ask for and then check password"""
ndigits = 2
pwlength = 8
print "password must be at least %d chars long with %d digits in it" % (pwlength, ndigits)
pw = raw_input("password: ")
result = passwordCheck(pw, pwlength, ndigits)
if result == True:
print "good password"
else:
print "BAD password"
HOMEWORK for next time:
look at /home/jk/inclass/functionWorksheet.py and write
down what you think the output will be. Then run the
program and check your results.
(www.pythontutor.com)