CPTS 111, Fall 2011, Sections 6&7 Exam 2 Review

advertisement
CPTS 111, Fall 2011, Sections 6&7
Exam 2 Review
Object-oriented programming concepts

Know how to create an object: by calling the class constructor. For example following creates a
Point object (an instance of Point class):
p = Point(1,2)

Know how to invoke a method of an object. For example, following invokes getX()method on
the object p:
p.getX()

Nearly everything is an object in Python, such as int, str, list. Here are some example
usages and methods we’ve seen on these objects:
>>> a = 12
>>> a.bit_length()
4
>>> b = [4, 2, 3]
>>> b.sort()
#sort() changes the list object b and returns None
>>> b
[2, 3, 4]
>>> b.reverse()
#reverse() changes the list object b and returns None
>>> b
[4, 3, 2]
>>> s = "name"
>>> s.upper() #upper() RETURNS the desired str
'NAME'
>>> s
'name'
#strings are immutable; s is still same
>>> s.capitalize()
#capitalize() RETURNS the desired str
'Name'
>>> s
#strings are immutable; s is still same
'name'
>>> s = s.upper() #change s by assigning a value to it
>>> s
'NAME'
>>> s.lower()
#lower() RETURNS the desired str
'name'
>>> s
'NAME'
>>> "{}--*--{}".format("hello", "world")
'hello--*--world'
>>> "{2} {0} {1}".format('a', 'b', 'c')
'c a b'
>>> "{:.^5}//{:*>5}//{:$<5}".format('a', 'b', 'c')
'..a..//****b//c$$$$'
>>> "{:d}---{:.2f}---{:b}---{:x}".format(25, 12.456, 35, 15)
'25---12.46---100011---f'
>>> "This is a sentence".split() #by default splits on space
['This', 'is', 'a', 'sentence']
>>> "aa12cc12dd12ee".split('12')
['aa', 'cc', 'dd', 'ee']
>>> "--*--".join(["word1", "word2", "word3"])
'word1--*--word2--*--word3'
>>> " ".join(["word1", "word2", "word3"])
'word1 word2 word3'
>>> "".join(["h", "ell", "o"])
'hello'
Graphics library

Know about GraphWin, Text, Entry, Point, Line, Rectangle, Circle objects; how to create them
and how to manipulate them.
Strings

A type of a "sequence" like lists and tuples which implies that following operations are all valid
on strings:
o You can loop over them
o You can learn their length by len()
o You can access individual elements of them by indexing notation
o You can access a subset of its elements by slicing notation
First row in the following table shows a string content; values in the second and third row
indicate the offsets to use while referring to the individual elements of the string.
h
0
-5
e l
1 2
-4 -3
l
3
-2
o
4
-1
Here is a demonstration of available string access operations:
>>> s = "Hello"
>>> s[0]
'H'
>>> s[-1]
'o'
>>> s[len(s)-1]
'o'
>>> s[:3]
'Hel'
>>> s[2:]
'llo'
>>> s[1:3]
'el'




Strings are immutable like tuples and unlike lists.
"+" operator performs string concatanation when used with two string operands
"*" operator performs string multiplication when used with one string and one integer operand
Strings are made out of characters, and characters are stored/represented as numbers in a
computer. ASCII code (American Standard Code for Information Interchange) maps characters to
numbers. ASCII is the standard encoding scheme for characters.



You can embed special characters in a string using escape sequences such as: '\n' (newline),
'\t' (tab).
ord() function returns the ASCII code of the given character.
chr() function returns the corresponding written character for the given numeric code
Some example questions to work on
1. Write a function called reverse()that takes a string argument and returns the reversed
string. Make sure you actually create the reversed string and return it from the function. Do not
just print characters of the original string in reverse order. Here is a demonstration of the proper
behavior of this function.
>>> reverse("hello")
'olleh'
>>> result = reverse("hello world")
>>> result
'dlrow olleh'
2. [Hard] Write a function called reverseSentence()that takes a string argument and returns
the reversed string in a way that the word order in the given sentence is reversed (but not the
words themselves). Make sure you actually create the reversed string and return it from the
function. Assume each word in a sentence will be separated from each other with one space.
(Hint: You can do this in a couple of ways but one way involves splitting the given string on
space, going over the resulting list in reverse order while accumulating the elements on a new
list, joining that list on space and returning the resulting string). Here is a demonstration of the
proper behavior of this function.
>>> reverseSentence("hello world")
'world hello'
>>> reverseSentence("This is a big big sentence")
'sentence big big a is This'
3. Check out Ch. 5, programming exercise number 5 and 6.
Download