This preview shows page 1. Sign up to view the full content.
Unformatted text preview: New Assignment ECS10
10/24 Due Due Wednesday. Should Should be quick once you understand the concepts. Lists, for loops, and string methods (today’s topic). topic). Loop Loop over a list
beastList = [‘cow’, ‘sheep’, ‘duck’] i=0 while i < len(beastList): print ‘A’, beastList[i], ‘is an animal’ i = i+1 i is the index variable For loop
beastList = [‘cow’, ‘sheep’, ‘duck’] for beast in beastList: print ‘A’, beast, ‘is an animal’ Shorter Shorter and sweeter, but exactly the same effect. String String variable beast contains each of the beast contains items in the list in turn. No No index variable. Loop Loop over a string
strIn = ‘530-752-4767’ strOut = ‘’ for char in strIn: if char != ‘-’: strOut = strOut+char Same Same effect as previous version. Prettier, Prettier, shorter. char char takes on values 5, then 3, then 0, then -, … Loop on integers
count = 0 while count < 5: print count count = count+1 Another very common structure. 1 Loop over integers Loop Loop over integers for count in [0,1,2,3,4]: print count for count in range(5): print count Shorter with a for loop. for loop. Prettier with the range function. Range Range function
for count in range(5): print count Range function
string = ‘’ for count in range(5): string = string + ‘ha’ Standard Standard way to do something a fixed number of times. You You don’t have to type in a long sequence. range(5 range(5 ) is a built-in function builtProduces Produces the list [0,1,2,3,4] range(1,6) produces range(1,6) produces the list [1,2,3,4,5] while while loop effective interest rate
balance = 100.0 annualRate 7.0 annualRate = 7.0 monthlyRate = annualRate/12.0 month = 0 while month < 12: print "month ",month balance = balance+monthlyRate/100.0*balance month = month+1 for loop effective interest rate
balance = 100.0 annualRate annualRate = 7.0 monthlyRate = annualRate/12.0 for month in range (12): print "month ",month balance = balance+monthlyRate/100.0*balance Two Two lines shorter than while version…. 2 for vs while
Anything Anything you can do with a for loop, you could also do with a while. for for loop will be a little shorter and tidier. Wh While loops are much versatile. For For loops are only good if you know how many times they will run before you start (12 months, length of list,…) You You couldn’t write the compound interest program with a for loop. Making Making a list from a big string
Divide Divide string by cutting out all copies of some character: sentence = ‘How do you get the words?’ words = sentence.split(‘ ’) Produces Produces a list of littler strings (here, the words in the sentence). Notice Notice the question mark ends up as part of the last string, ‘words?’ The split method
If If split was a normal function, it would be called like this: split(sentence, ‘ ’) Both arguments inside parenthesis. arguments insi Instead, Instead, it looks like this: sentence.split(‘ ’) The The first argument, then a period, then the method name split, then the second split argument in the parenthesis. Splitting Splitting on newline
MultiMulti-line strings can be put in triple quotes. Each Each line ends with the invisible newline newline character. Refer to newline in Python with ‘\n’. lines = poem.split('\n') Produces a list of lines, each of them a string. Splitting Splitting on whitespace
Cutting Cutting out everything invisible (spaces, newlines, tabs….): words = poem.split() No No argument to split method. The in operator
Checks Checks to see if one string is part of another sentence = ‘Practice makes perfect.’ if ‘act’ in sentence: print ‘Found it!’ The The string ‘act’ is part of the sentence. So So the Boolean expression ‘act’ in sentence ….is ….is True, and the program prints ‘Found it!’ 3 Using in with lists
Checks Checks to see if an item is in a list. beasts = [‘cow’, ‘goat’, ‘rabbit’] if ‘cow’ in beasts: print ‘Have a cow.’ This This prints ‘Have a cow.’, because the string ‘cow’ is one of the items in the list. The The expression ‘ow’ in beasts ….is ….is False. Boolean Boolean operators
Examples Examples of Boolean operators: reply == ‘r’ x <= 11 ‘I’ in ‘team’ ‘cow’ in [‘sheep’, ‘pig’, ‘rabbit’] Each Each of these has value either True or False The The in operator produces True if the data item on the left is part of the sequence on the right. 4 ...
View Full Document
This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.
- Winter '07