







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








stack = [3, 4, 5] stack.append(6) stack.append(7) stack [3, 4, 5, 6, 7] stack.pop() 7 stack [3, 4, 5, 6] stack.pop() 6 stack.pop() 5 stack [3, 4] queue = ["Eric", "John", "Michael"] queue.append("Terry") # Terry arrives queue.append("Graham") # Graham arrives queue.pop(0) 'Eric' queue.pop(0) 'John' queue ['Michael', 'Terry', 'Graham']
Functions provided by the string library (requires import string): In first column: s, t represent string values, n represents integer values and list represents a list of strings. function return type description example center(s, n) string Create a new string of n characters with s in the center. Remaining characters are spaces.
string.center('dog',8) queue.pop(0) 'John' >>> queue ['Michael', 'Terry', 'Graham'] ## Dictionaries: Dictionaries are indexed by keys, rather than positions. Dictionaries are mutable, in that the value associated with a key may be ## changed. ## >>> d = { 'COMP51' : 'Cliburn', 'COMP53' : 'Carman', 'COMP151':'Doherty' } ## >>> d ## {'COMP51': 'Cliburn', 'COMP53': 'Carman', 'COMP151': 'Doherty'} ## >>> d['COMP51'] ## 'Cliburn' ## >>> d['COMP51'] = 'Topp' ## >>> d['COMP51'] ## 'Topp' ## Operator precedence: Operators higher in the table have higher precedence. ## Operator Description number of operands ## or logical OR binary ## and logical AND binary ## not x logical NOT unary ## relational (comparison) ## binary ## +, - addition/concatenation and subtraction binary ## *, /, % multiplication, division, remainder binary ## + x , - x positive, negative unary ## ** exponentiation binary ## x [ index ] indexing binary ## x [ index : index ] slicing trinary Functions provided by the string library (requires import string): In first column: s, t represent string values, n represents integer values and list represents a list of strings. function return type description example center(s, n) string Create a new string of n characters with s in the center. Remaining characters are spaces. >>> string.center('dog',8) ' dog ' ljust(s, n) string Create a new string of n characters with s at the left. Remaining characters are spaces. string.ljust('dog',8) 'dog rjust(s, n) string Create a new string of n characters with s at the right. Remaining characters are spaces. string.rjust('dog',8) ' dog' strip(s) string Create a copy of s with leading and trailing whitespace removed. string.strip(' cat ') 'cat' lstrip(s) string Create a copy of s with leading whitespace removed. string.lstrip(' cat ') 'cat ' rstrip(s) string Create a copy of s with trailing whitespace removed. string.lstrip(' cat ')
' cat' capitalize(s) string Create a copy of s with the first character capitalized.
string.capitalize('driver disk')
'Driver disk' capwords(s) string Create a copy of s with the first character in each word capitalized. (Words are separated by whitespace)
string.capwords('three short words')
'Three Short Words' upper(s) string Create a copy of s with all letters converted to upper-case.
string.upper('this is loud') 'THIS IS LOUD' lower(s) string Create a copy of s with all letters converted to lower-case. string.lower('A funKY StriNG') 'a funky string' split(s) list of strings Split a string into a list of substrings. Strings are split at whitespace (word boundaries). string.split('Break this apart.') ['Break', 'this', 'apart.'] split(s, t) list of strings Split a string into a list of substrings. Strings are split at occurrences of the string t. string.split('Break this apart.', 'a') ['Bre', 'k this ', 'p', 'rt.'] join(list) string Concatenate all string in a list to create a single string. list = [ 'One', 'Two', 'Three' ] string.join(list) 'One Two Three'
Functions provided by the string library (requires import string): In first column: s, t and u represent string values. function return type description example count(s, t) integer Determine the number of times that t appears in s.
s = 'Mississippi' string.count(s, 'iss') 2 string.count(s, 'issi') 1 string.capitalize('driver disk') 'Driver disk' capwords(s) string Create a copy of s with the first character in each word capitalized. (Words are separated by whitespace) >>> string.capwords('three short words') 'Three Short Words' upper(s) string Create a copy of s with all letters converted to upper-case. >>> string.upper('this is loud') 'THIS IS LOUD' lower(s) string Create a copy of s with all letters converted to lower-case. >>> string.lower('A funKY StriNG') 'a funky string' split(s) list of strings Split a string into a list of substrings. Strings are split at whitespace (word boundaries). >>> string.split('Break this apart.') ['Break', 'this', 'apart.'] split(s, t) list of strings Split a string into a list of substrings. Strings are split at occurrences of the string t. >>> string.split('Break this apart.', 'a') ['Bre', 'k this ', 'p', 'rt.'] join(list) string Concatenate all string in a list to create a single string. >>> list = [ 'One', 'Two', 'Three' ] >>> string.join(list) 'One Two Three' Functions provided by the string library (requires import string): In first column: s, t and u represent string values. function return type description example count(s, t) integer Determine the number of times that t appears in s. >>> s = 'Mississippi' >>> string.count(s, 'iss') 2 >>> string.count(s, 'issi') 1 find(s, t) integer Determine the position (index) of the first occurrence of t in s, looking from the left. s = 'Mississippi' string.find(s, 'iss') 1 rfind(s, t) integer Determine the position (index) of the first occurrence of t in s, looking from the right. s = 'Mississippi' string.rfind(s, 'iss') 4 replace(s, t, u) string Make a copy of s in which all occurrences of t have been replaced by u. x = 'banana' string.replace(x, 'an', '-an') 'b-an-ana'
In our example, the constructor creates and initializes a single instance variable called self.count. Thus, the statement my_counter = Counter(), creates an object that looks like: Notice how the variable self.count is encapsulated inside the object named counter. We manipulate objects by calling the methods on the object, with the object named pre-pended with a dot. The object name actually gets used as the first parameter to the method, thus the object name becomes associated with the self argument. Thus, the statement my_counter.increment() adds 1 to the count variable inside the object named my_counter. We can create multiple object of the same type, and each has its own copy of the instance variables. For example: my_counter (^) self.count
my_counter (^) self.count
c1 = Counter() c2 = Counter() c1.increment() c1.increment() c2.increment() print c1.getCount(), c2.getCount # prints 2 1
produces the following objects: Here’s an example of a class that defines objects with two instance variables. c1 (^) self.count
c2 (^) self.count
class Scoreboard: def init(self): self.home = 0 self.away = 0 def addHome(self, points): self.home = self.home + points def addAway(self, points): self.away = self.away + points def homeScore(self): return self.home def awayScore(self): return self.away def printScore(self): print "Home:", self.home, "Away:", self.away
Consider what will happen if we run the following code: At the end, we will have the following objects:
p1 = Point(10, 20) p2 = Point(100, 112) p1.move(200, 0) p2.move(50, –50) print p1.getX(), p1.getY() print p2.getX(), p2.getY() p1 (^) self.x 210 self.y 20 p2 (^) self.x 150 self.y 62 class ColoredPoint(Point): def init(self, x, y, color): Point.init(self,x,y) self.color = color def getColor(self): return self.color c = ColoredPoint(1,2,'red') print c.getX(), c.getY(), c.getColor()