ecs10-12-07

ecs10-12-07 - 12/7/2007 Announcements ECS 10 12/07 FINAL...

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 12/7/2007 Announcements ECS 10 12/07 FINAL Thur Dec 13 1-3pm, 176 Everson 1Open book, open notes. Bring sample programs from class, your programs, etc. Bring a Scantron 2000 Materials for Study On class Web page: example problems. Also, lecture slides, in-class programs. See inespecially midterm review sessions. On myUCDavis: Midterm 2 solutions, Mid O myUCDavis: Mid UCD i l i Midterm 2 sample test, HW solutions. Read the book and work through its examples, if you haven't yet. Topics Everything from Midterm 1 Boolean variable and expressions int(), int(), float(), str() str() Everything f E hi from Mid Midterm 2 Functions return values, local variables Lists String methods for loops Dictionaries Boolean Variables x=5 a = x>2 b = x<4 print (not a) or (not b) What does this program do? Boolean Variables x=5 a = x>2 b = x<4 print (not a) or (not b) The value of a is True The value of b False The value of (not b) is (not False) is True So the program prints True 1 12/7/2007 Dictionaries D = {} # frequency of letters in English D["e"] = 1 D["t"] = 2 D["a"] =3 for i in range(3): print D[i+1] What does this program do? Dictionaries D = {} # frequency of letters in English D["e"] = 1 D["t"] = 2 D["a"] =3 for i in range(3): print D[i+1] It crashes, because D[1] does not exist. Keys here are letters, not integers. How could you store this data and print it out? When to use a List D = # frequency of letters in English D[1] = "e" D[2] = "t" D[3] = "a" for i in range(1,4): print D[i] Better to use a list, since we want to access the data using a small range of integer indices, in order. Dictionaries key = raw_input("Choose a letter: ") if key in D: print D[key] Use this construct if you can't be sure the key is in the dictionary. Keys are letters, not integers. Indexing with a non-key causes a crash. non- Dictionaries D = {} letters = "abcdefg" for i in range(len(letters)): D[letters[i]] = i print D["c"] What does this program do? Dictionaries D = {} letters = "abcdefg" for i in range(len(letters)): D[letters[i]] = i print D["c"] D is a dictionary, letters are keys, values are ints range(len(letters)) range(len(letters)) is [0,1,2,3,4,5,6] D["a"] = 0, D["b"] = 1,... Prints 2 2 12/7/2007 Lists of Lists L = [3, 0] for i in range(3): L = L + [[i, i+1]] What is L after these lines? Lists of Lists L = [3, 0] for i in range(3): L = L + [[i, i+1]] [i, i+1] is a list of integers, with length 2 [[I, i+1]] is a list of lists, with length 1 L is [3, 0, [0, 1], [1, 2], [2, 3]] L is a list of integers and lists, with length 5. How would you get a list of lists? Lists of Lists L = [[3, 0]] for i in range(3): L = L + [[i, i+1]] Now L begins as a list of lists, with length 1. We concatenate a list of lists with a list of lists. Usually a good idea to concatenate lists of the same type of objects, but not required by Python. Functions def cleanString(s): out = s.replace(",", "") inStr = "34,308" cleanString(inStr) x = int(out) What does this program do? Functions def cleanString(s): out = s.replace(",", "") inStr = "34,308" cleanString(inStr) x = int(out) Crashes. The local variable out is still undefined in the main program. Functions def cleanString(s): s = s.replace(",", "") inStr = "34,308" cleanString(inStr) x = int(inStr) How about this one? 3 12/7/2007 Functions def cleanString(s): s = s.replace(",", "") inStr = "34,308" cleanString(inStr) x = int(inStr) Also crashes. Functions don't change their arguments. inStr is defined but cannot be converted. How do you write this program correctly? Functions def cleanString(s): s = s.replace(",", "") return s inStr = "34,308" newStr = cleanString(inStr) x = int(newStr) Information is sent back to the main program by with the return value. In the main program, it has to be assigned to a variable (newStr). Files inFile = open("myFile.txt","r") G = giftStr = inFile.readline() while giftStr != "": giftStr = giftStr[:-1] G = [giftStr] giftStr = inFile.readline() What does this code do? Files inFile = open("myFile.txt","r") G = giftStr = inFile.readline() while giftStr != "": giftStr = giftStr[:-1] G = [giftStr] giftStr = inFile.readline() Typical file-reading while loop. File line is "" after end of file. Every line ends with a "\n" newline character G ends up being a list containing the last string in the file (without newline) Files inFile = open("myFile.txt","r") G = giftStr = inFile.readline() while giftStr != "": giftStr = giftStr[:-1] G = G + [giftStr] giftStr = inFile.readline() In this version G becomes a list of strings (all without newlines) Another way to remove newlines? String Methods fruitStr = " pineapple, pear\n" str1 = fruitStr.strip() fruits = str1.split(",") for item in fruits: print item, len(item) What does this program print? 4 12/7/2007 String Methods fruitStr = " pineapple, pear\n" str1 = fruitStr.strip() fruits = str1.split(",") for item in fruits: print item, len(item) Prints: pineapple 9 pear 5 There is a space at the beginning of " pear" Exceptions def isFloat(s): try: float(s) # Try to do the conversion except: return False # Conversion failed! else: return True # Conversion succeeded Slicing # Example of slicing string = "pineapple" print string[-5:] squareList = [1,4,9,25] print squareList[1:-2] Prints "apple" and then "[4]" String Processing string = " Nov 16\tNASDAQ\t 2634.93\t+0.63%\n" string = string.strip() words = string.split("\t") change = words[-1] if change[0] == "+": h [0] "+" direction = "up" else: direction = "down" change = change[1:] change = change.replace("%","") print "NASDAQ ",direction,"by",change,"percent" 5 ...
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.

Ask a homework question - tutors are online