| Author: | Christopher Haynes |
|---|---|
| Email: | chaynes@indiana.edu |
| Affiliation: | Indiana University |
| Course: | BL CSCI A201 |
| Date: | 2008-03-06 |
Contents
>>> s = 'I never, never did anything...'
>>> s.upper()
'I NEVER, NEVER DID ANYTHING...'
>>> import math
>>> math.sqrt(4)
2
>>> s.lower()
'i never, never did anything...'
>>> s.find('never')
2
>>> s.find('Never')
-1
>>> s.index('never')
2
>>> s.index('Never')
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
>>> s.find('never', 3)
9
>>> s.index('never', 3)
9
>>> word = s[0: s.index('never')]
>>> word
'I '
>>> word.strip()
'I'
>>> s[0 : s.index('never')].strip()
'I'
>>> '123'.isdigit()
True
>>> ' 123 '.isdigit()
False
>>> ' 123 '.strip().isdigit()
True
>>>
General method call syntax:
object_expression . method_name ( expression ,..., expression )
We have seen that the second argument of the find and index methods is optional and defaults to 0 if it is missing
FYI: many functions and methods takes some number of optional argument, or even an unlimited number of arguments
variable numbers of arguments
there is even related mechanism for "keyword" arguments, but we do not need these features in this course
Since strings are immutable, wording such as in the last three points above means that a copy of the string s is returned with just the indicated changes.
Which of the following is a method call to find the index of 'a' in s?Answer: C
What does the following print?s = 'AIR RAID SIRENS' s.lower() print s
Answer: A
What does '[812]'[1:4].isdigit() return?Answer: A
Next we run the View > Source command (in IE and Firefox browsers)
Then we cut the URL from the browser URL window into the population_test.py program that follows, run it, and compare the printed text with the browser's source (they should be the same)
# population_test.py, by chaynes@indiana.edu
import http
population_url = 'http://www.census.gov/main/www/popclock.html'
def main():
text = http.get(population_url)
print text ##
## insert datamining code here
main()
How can we locate the temperature?
One possible answer: between id="wclocknum"> and the following <, as in population.py
# population.py, by chaynes@indiana.edu
import http
population_url = 'http://www.census.gov/main/www/popclock.html'
def main():
text = http.get(population_url)
start_text = 'id="wclocknum">'
start = text.index(start_text) + len(start_text)
stop = text.index('<', start)
print text[start : stop]
main()
View Roger Ebert's recommended movie list at http://rogerebert.suntimes.com/apps/pbcs.dll/section?category=REVIEWS05
How can we locate the movie list in the source text?
First, we note that the list is between the strings -- main content -- and !Middle1
How can we locate each successive movie title?
See ebert_movies.py
# ebert_movies.py, by chaynes@indiana.edu
'''
Get the text of Roger Ebert's recommendations page and print
the recommended movie titles.
'''
import http
ebert_recommends_url = 'http://rogerebert.suntimes.com/apps/pbcs.dll/'
ebert_recommends_url += 'section?category=REVIEWS05'
# Search between the first occurrence of '-- main content--' and the next
# '!Middle1' for movie names. Names start after the first b> after
# 'href=/apps/pbcs.dll' and end with the following '<'.
def main():
text = http.get(ebert_recommends_url)
begin = text.index('-- main content --')
endRecs = text.index('!Middle1', begin)
while True:
i = text.find('href="/apps/pbcs.dll', begin)
if i == -1 or i > endRecs:
break
begin = text.find('b>', i) + 2
end = text.find('<', begin)
print text[begin : end]
main()
def similar(s1, s2):
"""
Return a boolean value indicating if strings s1 and s2 are equal,
ignoring case (case insensitive) and leading and trailing whitespace.
>>> similar(' Ni ', 'ni')
True
>>> similar('eggs', ' EGGS')
True
>>> similar('eggs', 'egg')
False
>>>
"""
#...
def similar(s1, s2):
"""
Return a boolean value indicating if strings s1 and s2 are equal,
ignoring case (case insensitive) and leading and trailing whitespace.
>>> similar(' Ni ', 'ni')
True
>>> similar('eggs', ' EGGS')
True
>>> similar('eggs', 'egg')
False
>>>
"""
return s1.lower().strip() == s2.lower().strip()
def inches1(length):
"""
Return the number of inches (an int) in length, a string that should be
in the format <inches>", where <inches> is a non-negative integer, Return None
if length is not a string in the proper format.
>>> inches1('3"')
3
>>> inches1('3') # None returned by this and following tests
>>> inches1('+3"')
>>>
"""
#.
#.
#.
#.
#.
#.
#.
#.
def inches1(length):
"""
Return the number of inches (an int) in length, a string that should be
in the format <inches>", where <inches> is a non-negative integer, Return None
if length is not a string in the proper format.
>>> inches1('3"')
3
>>> inches1('3') # None returned by this and following tests
>>> inches1('+3"')
>>>
"""
if length == '' or length[len(length) -1] != '"':
return None # no double quote
else:
# remove the double quote character
length = length[0 : len(length) - 1]
if not length.isdigit():
return None # <inches> is not an integer
return int(length)
def inches2(length):
"""
Return the number of inches (an int) in length, a string in the format
<feet>'<inches>", where both <feet> and <inches> are non-negative integers,
Return None if length is not in the proper format.
>>> inches2('2\'3"')
27
>>> inches2("bodus") # None returned by this and following tests
>>> inches2('11"')
>>>
"""
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
def inches2(length):
"""
Return the number of inches (an int) in length, a string in the format
<feet>'<inches>", where both <feet> and <inches> are non-negative integers,
Return None if length is not in the proper format.
>>> inches2('2\'3"')
27
>>> inches2("bodus") # None returned by this and following tests
>>> inches2('11"')
>>>
"""
index = length.find("'")
if index == -1:
return None # Missing ' mark
else:
feet_string = length[0 : index]
inches_string = length[index + 1 : len(length)]
if feet_string.isdigit():
feet = int(feet_string)
else:
return None # <feet> is not an integer
if inches_string == '':
return None # no <inchex>"
elif inches_string[len(inches_string) - 1] != '"':
return None # no double quote at end
else:
# remove the double quote character
inches_string = inches_string[0 : len(inches_string) - 1]
if inches_string.isdigit():
inches = int(inches_string)
else:
return None # <inches> is not an integer
return feet * 12 + inches
def inches1(length):
"""
Return the number of inches (an int) in length, a string that should be
in the format <inches>", where <inches> is a non-negative integer, Return None
if length is not a string in the proper format.
>>> inches1('3"')
3
>>> inches1('3') # None returned by this and following tests
>>> inches1('+3"')
>>>
"""
#.
#.
#.
#.
#.
#.
#.
#.
def inches1(length):
"""
Return the number of inches (an int) in length, a string that should be
in the format <inches>", where <inches> is a non-negative integer, Return None
if length is not a string in the proper format.
>>> inches1('3"')
3
>>> inches1('3') # None returned by this and following tests
>>> inches1('+3"')
>>>
"""
if length == '' or length[len(length) -1] != '"':
return None # no double quote
else:
# remove the double quote character
length = length[0 : len(length) - 1]
if not length.isdigit():
return None # <inches> is not an integer
return int(length)
What is the value of 'a'.strip == 'a' ?Answer: B
What does the test print?def mystery(s, n):
"""
>>> mystery('Chamilot', 2)
#...
>>>
"""
while 0 < n:
s = s[1 : len(s) - 2]
n = n - 1
print s
Answer: C