Python Method Flashcards

 

card 1 of 40

 

"important".upper()

 

the "something.name()" is a method call, somewhat like a function call of the form "name(something)"

'IMPORTANT'

card 2 of 40

 

105.upper()

 

this looks confusing

SyntaxError: the period means decimal point, with numbers

card 3 of 40

 

"CaLiFoRnIa".lower()

 

remember what "important".upper() returns

'california'

card 4 of 40

 

'Anything Else'=="anything else"

 

this is just a review question on strings

False

card 5 of 40

 

'Anything Else'.lower()=="anything else"

 

evaluate the method call first, before comparison

True

card 6 of 40

 

'Anything Else'.lower()=="ANYTHING else".lower()

 

evaluate the method calls first, before comparison

True

card 7 of 40

 

"walking a path".replace('a','EEE')

 

change the 'a' chararacters as directed

'wEEElking EEE pEEEth'

card 8 of 40

 

"Ophelia Oliver".replace('O','')

 

change the 'O' chararacters as directed

'phelia liver'

card 9 of 40

 

"razor thin".index('i')

 

for what integer would the indexing return 'i'

8

card 10 of 40

 

"razor thin".index('k')

 

there is no possible answer here

ValueError: item not found in string

card 11 of 40

 

'Escape from an island'.index('e f')

 

blanks count as characters

5

card 12 of 40

 

'Escape from an island'.index('c p')

 

search for a literally equal match

ValueError: item not found in string

card 13 of 40

 

' as seen on TV '.strip()

 

remove the leading and trailing spaces

'as seen on TV'

card 14 of 40

 

' as seen on TV '.strip().lower()

 

evaluate the method calls left-to-right

'as seen on tv'

card 15 of 40

 

{50:True,35:False,21:True,22:True}.keys()

 

the keys method makes a list of dictionary keys

[50,35,21,22]

card 16 of 40

 

{50:True,35:False,21:True,22:True}.values()

 

the values method makes a list of the values

[True,False,True,True]

card 17 of 40

 

{50:True,35:False,21:True,22:True}.items()

 

the items method makes a list (key,value) pairs

[(50,True),(35,False),(21,True),(22,True)]

card 18 of 40

 

{50:True,35:False,21:True,22:True}.items()

 

the items method makes a list (key,value) pairs

[(50,True),(35,False),(21,True),(22,True)]

card 19 of 40

 

[60,61,62,63,64].index(62)

 

similar to how string index method works

2

card 20 of 40

 

[60,61,62,63,64].index((62,63))

 

similar to how string index method works

ValueError - item not in list

card 21 of 40

 

'mississipi'.count('s')

 

counts up how many times 's' occurs

4

card 22 of 40

 

'mississipi'.count('is')

 

counts up how many times 'is' occurs

2

card 23 of 40

 

'mississipi'.count('xs')

 

counts up how many times 'xs' occurs

0

card 24 of 40

 

[True,False,True,True,False,False,False].count(True)

 

counts up how many times True occurs

3

card 25 of 40

 

{True:'abc',False:'def'}[False].upper()[1:]

 

evaluate indexing, method call, etc, left-to-right

'EF'

card 26 of 40

 

"xxx".join(['once','upon','a','time'])

 

use "xxx" as glue to make a long string

'oncexxxuponxxxaxxxtime'

card 27 of 40

 

' '.join(['once','upon','a','time'])

 

use " " as glue to make a long string

'once upon a time'

card 28 of 40

 

"".join(['once','upon','a','time'])

 

use empty string as glue to make a long string

'onceuponatime'

card 29 of 40

 

"".join(list('reform'))=="reform"

 

look again at flashcards on lists and conversion

True

card 30 of 40

 

'+'.join([0,3])

 

the join method needs a list of strings

TypeError: expected str, found integer

card 31 of 40

 

'+'.join(['0','3'])

 

the join method needs a list of strings

'0+3'

card 32 of 40

 

'-'.join("abcd")

 

the join method can convert a string argument to a list

'a-b-c-d'

card 33 of 40

 

"simply read the data".split('a')

 

the split method is something like un-join

['simply re', 'd the d', 't', '']

card 34 of 40

 

"simply read the data".split(' ')

 

the split method is something like un-join

['simply', 'read', 'the', 'data']

card 35 of 40

 

"simply read  the  data".split(' ')

 

there are two blanks before "the" and two blanks after

['simply', 'read', '', 'the', '', 'data']

card 36 of 40

 

"simply read the data".split()

 

if split() has no argument, it just uses "whitespace"

['simply', 'read', 'the', 'data']

card 37 of 40

 

"simply read   the    data".split()

 

whitespace overcomes extra blanks

['simply', 'read', 'the', 'data']

card 38 of 40

 

'dinner' in "the whitehouse dinner was formal".split()

 

the method call is evaluated before the in-test

True

card 39 of 40

 

' '.join("c u l8r guys".split())

 

evaluate what is within parantheses first

'c u l8r guys'

card 40 of 40

 

' '.join("c u l8r guys").split()

 

evaluate method calls left-to-right

['c','u','l','8','r','g','u','y','s']

 

End of Problem Set

(use browser reload to restart this problem set)