Turn in this page for your group
| string method | purpose |
|---|---|
| s.upper() | returns string upper case version of string s |
| s.count(sub) | returns int number of (non-overlapping) occurrences of sub in s |
| s.endswith(sub) | returns boolean depending on whether s ends with sub |
| s.find(sub) | returns int: first index at which sub occurs in s or -1 if no occurrences |
| s.split() | returns list of s split on whitespace |
| s.split(sep) | returns list of s split on sep, a delimiter |
| s.strip() | returns copy of s withOUT leading and trailing whitespace |
| list method | purpose |
|---|---|
| lst.count(elt) | returns number of occurrences of elt in lst |
| lst.index(elt) | returns first/least index at which elt occurs in lst, generates error if elt not in lst |
| lst.append(elt) | append elt to end of lst, None returned |
Functions are applied to objects. For example, len returns
an int: the length of a string or list or other
sequence/iterable. So len("apple") is 5 and
len([1,2,3]) is 3.
Other functions for lists include: sum (returns total, an int, of elements in a list); max,min which return largest and smallest elements in a list; sorted which returns a sorted version of a list; reversed which returns a reversed version of a list.
For example:
| function call | result returned | type returned |
|---|---|---|
| len([1,2,3,"apple"]) | 4 | int |
| max([5,4,1,2,9,3]) | returns 9 | same as list elements |
| max("ape", "bee", "zebra", "wildebeast"] | "zebra" | same as list elements |
| min([5,4,1,2,9,3]) | returns 1 | same as list elements |
| sorted([5,4,1,2,7]) | [1,2,4,5,7] | list |
| reversed([1,2,3,6]) | [6,3,2,1] | list |
max returns the maximal value of a string
as well as a list. What is max("science") and why?
reversed(sorted([5,4,1,2,8])) and
why?
sorted(lst).index(min(lst)) for
any list lst? Why? What's the value if
min is replaced by max?
str.endswith(str[-3:]) True?
st.upper().endswith(st) True?
(Provide a specific example of when it's true and try to generalize)
in operator determines if its left operand occurs
in its right operand and returns a boolean value. This means
'a' in 'stranger' evaluates to True. What
is the value of 5 in [1,2,3,4]?
st[0:2] in st is False?
(Provide a specific example of when it's true and try to generalize)
st[0:2]*2 in st is True?
(Provide a specific example of when it's true and try to generalize)
lst.count(lst[0]) the same
as the value of len(lst) for a list lst?
(Provide a specific example of when it's true and try to generalize)
phrase --- your plan should be in English, not in code.
Answer these questions on the handin sheet.
Turn in this page for your group
phrase using
this method. Name the list variable words.
for loop that will loop over each element of the
list
of words in phrase (use variable words from above) -- don't write the
loop body yet, just the loop.
acro = ""
for loop-you-wrote-above:
acro = acro + ______________
return acro
|
|
|
if statements in
solution A can be replaced by elif and have
the code still work correctly. Explain briefly why.
if statements in solution
A
can be replaced by the last statement, e.g., so that the only statement
in the loop body is bagels = bagels + i +
(i/12)
int isn't a good name for the variable
used
in the for-loop of solution B.