


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Key points in this Introduction to Computing Using Python lecture are: String Manipulation, Useful String Methods, Call Stack, Mutable Object, Objects, Modifies Object, Test Cases, Tracing Control Flow . Objectives of this course are: 1.Fluency in (Python) procedural programming 2. Competency in object-oriented programming 3. Knowledge of searching and sorting algorithms
Typology: Slides
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















def make_netid(name,n): """Returns a netid for name with suffix n Netid is either two letters and a number (if the student has no " middle name) or three letters and a number (if the student has " a middle name). Letters in netid are lowercase. Example: make_netid('Walker McMillan White',2) is 'wmw2' Example: make_netid('Walker White',4) is 'ww4' Precondition: name is a string either with format ' " ' or ' '; " names are separated by spaces. n > 0 is an int.""" Prelim 1 Review 5
def make_netid(name,n): """Returns a netid for name with suffix n.""” name = name.lower() # switch to lower case fpos = name.find(' ') # find first space first = name[:fpos] last = name[fpos+1:] mpos = last.find(' ') # see if there is another space if mpos == -1: return first[0]+last[0]+n # remember, n is not a string else : middle = last[:mpos] last = last[mpos+1:] return first[0]+middle[0]+last[0]+n Prelim 1 Review 7
def lname_first(s): """ Precondition : s in the form" """ " first = fname(s) last = lname(s) return last + ',' + first " def lname(s): """ Prec : see last_name_first""" end = s.find(' ') return s[end+1:] Prelim 1 Review 10 1 2 3 1 2 s first last s end 4 Omitting this is okay. Line 2 is not complete. 'John Doe' 'John' 'John Doe' lname_first: 1 2 lname: 1 2
Line 1 is complete. Counter is next line.
def shift(p): """Shift coords left Precondition: p a point""" temp = p.x p.x = p.y p.y = p.z p.z = temp
Prelim 1 Review 14
t = colormodel.RED # refers to something in colormodel t = rgb.red() # call to function "in" rgb t = rgb.r # attribute r of rgb, but there's no such attribute t = rgb.red # . is the way to access t = colormodel.rgb.red # refers to something in rgb in #colormodel Prelim 1 Review 16
Prelim 1 Review 17
def increase10(rgb): """Increase each attribute by 10% (up to 255)""" red = rgb.red # puts red attribute value in local var red = 1.1*red # increase by 10% red = int(round(red)) # convert to closest int rgb.red = min(255,red) # cannot go over 255
rgb.green = min(255,int(round(1.1rgb.green))) rgb.blue = min(255,int(round(1.1rgb.blue))) Prelim 1 Review 19 Procedure: no return
Prelim 1 Review 20 def multcap(x): """Returns: min of nearest int to x1.1 and 255. Precond: x a number""" return min(int(round(x1.1)), 255) def increase10(rgb): """Increase each attribute by 10% (up to 255)"""
alist = map(multcap, [rgb.red, rgb.green, rgb.blue]) rgb.red = alist[0] rgb.green = alist[1] rgb.blue = alist[2] Procedure: no return
Prelim 1 Review 22
def difference(len1,len2): """Returns: Difference between len1 and len Result is returned in inches Precondition: len1 and len2 are length objects" len1 is longer than len2""" feetdif = (len1.feet-len2.feet)* inchdif = len1.inches-len2.inches # may be negative return feetdif+inchdif Prelim 1 Review 23