ecs10-10-27

ecs10-10-27 - Assignment ECS 10 10/27 Due Wds Due Wds Night...

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: 10/27/2008 Assignment ECS 10 10/27 Due Wds. Due Wds. Night Understand Understand before you start: Lists Lists for loops Indexing Indexing lists and strings The The split method The The in operator The The range function All All a computer does… The The things we learned in the first month: Floating Floating point, integer, string and Boolean (and, or, not) expressions Variables Variables if statements if statements while while loops The The find method string = ‘$23.95’ decimal = string.find(‘.’) decimal decimal gets the value 3. find find returns the first index of the string argument in the parenthesis. string = ‘romania’ string.find(‘oman’) If string is not found, returns -1 …are …are really all that a computer does The The rest is `syntactic sugar’ Big Big exception: internet. Replacing parts of strings Common Common problem with number strings (you might encounter in assignment) - there's commas inside them. We We could write a function to remove the commas. commas. You should all know how to do this. Or, Or, we could see if there is an appropriate string method…. The replace method Google Google “Python string methods” 1 10/27/2008 The replace method string = ‘407,018’ popString = string.replace(‘,’, ‘’) population = int(popString) Replaces Replaces all copies of the first argument with the second. Here, Here, replaces all commas with the empty string; that is, eliminates commas. s = ‘Flinch’ s = s.replace(‘Fl’, ‘Gr’) Get data from file Get Get access to the file: inFile = open(‘article.txt’, ‘r’) variable to refer to file read – it's an input file; similarly w for write, i.e., an output file name of file File type data File File is a new kind of data. Mostly Mostly use it with file methods. Variables Variables are in your computer’s memory. Wh Where are the files? After After we’re done… close close method releases file after we're done. inFile.close() variable we have used to refer to file. Reading one Line readline readline method returns lines of file as strings. line = inFile.readline() Typical file reading loop inFile = open('article.txt','r') line = inFile.readline() while line != '': process a lliine line = inFile.readline() inFile.close() Use while because Use while because we don’t know how many lines there are. variable to hold string return value variable referring to file 2 10/27/2008 Why not read whole file? Might Might be very big. Take Take the parts we need, while we read, throw out excess immediately. Not a problem in this course, but when you are are dealing with, eg. all students at UC Davis, eg. data can get very big. That’s That’s why programs can run out of memory. Organize into sentences sentence = '' for word in words: sentence = sentence + ' ' +word if '.' in word and '$' not in word: print sentence,'\n' sentence = '' 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