This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Midterm ECS10
10/24 Mostly programs looked good. Scores should appear on myUCDavis soon. If you got 4 or less on the program, you need to work more on the skills in Chapters 1 and 2. k h kill i Ch d2
There is another (no credit) practice program you should do this week on the Web page. I will be in Lab hours on Thurs (1-3 in 2020 SLB). (1- Next Project
Draw stars! Start using graphics. New data type list New kind of loop - for Loop on string
strIn = "530-752-4767" "530-752i=0 strOut = "" while i < len(strIn): len(strIn): char = strIn[i] strIn[ if char != "-": "strOut = strOut+char i = i+1 Eliminates the dashes. Lists
A string is a sequence of characters, numbered starting from 0. A list is a sequence of anything, numbered starting from zero. zero intList = [5,0,3,5,6,8,9,0,4,4,4] Index into a list just like indexing into a string. intList intList # This is the integer 6 List of strings
beastList = ["cow","sheep","duck"] ["cow","sheep","duck"] i=0 while i < len(beastList): len(beastList): print "A",beastList[i],"is an animal" "A",beastList[ i = i+1 Length function len() works just the same as it len() does on strings len(beastList) has the value 3. len(beastList) Same structure as loop on strings. 1 Sequences
Lists and strings are two kinds of sequences. sequences. The len() function works on sequences (not on len() floats, or ints, or Booleans). ints, You Y can i d sequences to get elements. index l There is another kind of sequence in Python, the tuple, tuple, which is very like a list (does fewer things, but does them faster). We will mostly ignore tuples in this course. Very common structure
You have a big list, and you want to do the same thing for every item in the list.
Add up scores for all students. Draw all lines in star star. Convert all strings to integers. .... For loop
beastList = ["cow","sheep","duck"] ["cow","sheep","duck"] for beast in beastList: beastList: print "A",beast, "is an animal" "A",beast, Shorter and sweeter, but exactly the same effect. Goes through items in list, starting with beastList, beastList, then beastList, beastList.... beastList, beastList.... String variable beast takes on each of these values in turn No index variable! variable! Loop on string
strIn = "530-752-4767" "530-752strOut = "" for char in strIn: strIn: if char != "-" "strOut = strOut+char Same effect as previous version. Prettier, shorter. char takes on values 5, then 3, then 0, then -, ... Loop on integers
count = 1 while count < 11: print count count = count+1 for loop
for count in range(1,11): print count Shorter and sweeter, but exactly the same effect Standard way to do something a fixed number of times. range(start,stop) range(start,stop) is a built-in function builtProduces the list [1,2,...,9,10] 2 while loop effective interest rate
balance = 100.0 annualRate = 7.0 monthlyRate = annualRate/12.0 annualRate/12.0 month = 0 while month < 12: print "month ",month balance = balance+monthlyRate/100.0*balance balance+monthlyRate/100.0*balance month = month+1 for loop effective interest rate
balance = 100.0 annualRate = 7.0 monthlyRate = annualRate/12.0 annualRate/12.0 for month in range (0,12): print "month ",month balance = balance+monthlyRate/100.0*balance balance+monthlyRate/100.0*balance Two lines shorter than while version.... for vs while
Anything you can do with a for loop, you could also do with a while. for loop will be a little shorter and tidier. While l Whil loops are much more versatile. h il For loops are only good if you know how many times they will run before you start (12 months, length of list,...) Example: doubling time
principal = 100.0 annualRate = 7.0 monthlyRate = annualRate/12.0 annualRate/12.0 balance = principal month = 0 while balance < 2* principal: balance = balance + balance*monthlyRate/100 balance*monthlyRate/100 month = month+1 Can't do this with a for loop! 3 ...
View Full Document
This note was uploaded on 04/14/2008 for the course ECS 10 taught by Professor Amenta during the Fall '07 term at UC Davis.
- Fall '07