ecs10-10-22

# ecs10-10-22 - Midterm ECS10 10/22 Mostly Mostly programs...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Midterm ECS10 10/22 Mostly Mostly programs looked OK; few were perfect. Scores Scores should appear on myUCDavis soon. soon. Solutions Solutions up today. ¼ of the class doing poorly of If If you got 5/9 or less on the program, you need to work more on the skills in Chapters 1-3. 1There There is another (no credit) practice program you should do this week on the Web page should do this week on the Web page. Go Go to section. Start Start programs early and go to lab hours early in the week. Try Try installing pyscripter (see software section of Web page) and use it to step through programs. Today Today New New data type – list New New kind of loop – for More More about strings New New assignment will be up tomorrow List days = [‘Mon’, ‘Tues’, ‘Wds’, ‘Thur’, ‘Fri’] days[0] days[0] is the string ‘Mon’, days[2] is the string ‘Wds’ You You can have a list of any kind of data. BP = [140,80] letters = [‘c’, ‘o’, ‘w’] Lists and Strings A list is a sequence of anything, numbered list starting from zero. intList = [5,0,3,5,6,8,9,0,4,4,4] Index into a list. li intList[4] # This is the integer 6 A string is like a list of single characters. string characters. beast = ‘cow’ print beast[2] 1 Function len() len() Tells Tells you how many things are in a list. days = [‘Mon’, ‘Tue’, ‘Wds’, ‘Thur’, ‘Fri’] print len(days) Prints 5 Also works with strings print len(‘cow’) len(‘cow’) Prints 3 Loop over a string strIn = ‘530-752-4767’ i=0 strOut = ‘’ while i < len(strIn): char = strIn[i] if char != ‘-’: strOut = strOut+char i = i+1 Eliminates the dashes. 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 Sequences Lists Lists and strings are two kinds of sequences. sequences The len() The len() function works on sequences (not on floats, or ints, or Booleans). ints, You can index sequences to get elements. There There is another kind of sequence in Python, the tuple, which tuple, which is very like a list (does fewer things, but does them faster). We will mostly ignore tuples in this course. Very Very common structure You You have a big list, and you want to do the same thing for every item in the list. Add Add up scores for all students. Draw all lines in star Draw all lines in star. Convert Convert all strings to integers. …. …. For 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. Goes Goes through items in list, starting with beastList[0] beastList[0], then beastList[1], beastList[2]…. beastList[1] beastList[2] String String variable beast takes on each of these values beast takes in turn No No index variable! 2 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 over a list beastList = [‘cow’, ‘sheep’, ‘duck’] for beast in beastList: print ‘A’, beast, ‘is an animal’ Again, Again, shorter and sweeter. Works exactly the same as previous version. Type Type in both and try them out. 3 ...
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.

Ask a homework question - tutors are online