Strings and dictionaries Flashcards
Each element that is stored in a dictionary has two parts: ___
a list and a value.
a list and an index.
a key and a value.
a list and a class.
a key and a value
A dictionary can include the same value several times but cannot include the same key several times.
TRUE
FALSE
TRUE
You cannot use a for loop to iterate over the characters in a string.
TRUE
FALSE
FALSE
The values that are stored in a single dictionary can be of different types.
TRUE
FALSE
TRUE
The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
FALSE
FALSE
What will be assigned to the variable z after the following code executes?
x = '123 Panorama Ave' z = x[4:]
’ Ave’
‘123 ‘
‘Panorama’
‘Panorama Ave’
‘Panorama Ave’
To test if the user input contains only alphabetical letters, you can use the ________ string method.
isalpha()
ascii()
strip()
isletter()
isalpha()
The ________ method returns all the dictionary key-value pairs in the form of a sequence of tuples.
items()
values()
tuples()
sequence()
items()
If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
TRUE
FALSE
TRUE
Dictionaries are immutable objects.
TRUE
FALSE
FALSE
In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.
TRUE
FALSE
TRUE
The following expression is valid: string[0] = ‘k’
TRUE
FALSE
FALSE
If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences in the paragraph.
TRUE
FALSE
TRUE
A dictionary can include the same value several times but cannot include the same key several times.
TRUE
FALSE
TRUE
If you try to retrieve a value from a dictionary using a nonexistent key, an IndexError exception is raised.
TRUE
FALSE
FALSE
What are the valid indexes for the string ‘New York’?
0 through 8 -1 through 6 -1 through -8 0 through 7
0 through 7
-1 through -8
What will be displayed after the following code executes?
mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr * 2
print(mystr)
yesnono yes + no * 2 yes + no yes + no yesnoyesno
yesnono
What will be assigned to the variable s_string after the following code executes?
special = '1357 Country Ln.' s_string = special[ :4]
'7' '7 Country Ln.' '1357' 5
‘1357’
What will be assigned to the string variable pattern after the following code executes?
i = 3 pattern = 'z' * (5 * i)
Nothing; this code is invalid 'zzzzz' 'z * 15' 'zzzzzzzzzzzzzzz'
‘zzzzzzzzzzzzzzz’
Which method would you use to determine whether a certain substring is present in a string?
find(substring) replace(string, substring) endswith(substring) startswith(substring)
find(substring)
What list will be referenced by the variable list_strip after the following code executes?
my_string = '03/07/2018' list_strip = my_string.split('/')
['3', '7', '2018'] ['03', '07', '2018'] ['3', '/', '7', '/', '2018'] ['03', '/', '07', '/', '2018']
[‘03’, ‘07’, ‘2018’]
What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?
{ 1, 2,... 12 : 'January', 'February',... 'December' } { 1 : 'January', 2 : 'February', ... 12 : 'December' } { 1 ; 'January', 2 ; 'February', ... 12 ; 'December'} [ '1' : 'January', '2' : 'February', ... '12' : 'December' ]
{ 1 : ‘January’, 2 : ‘February’, … 12 : ‘December’ }
What will be the result of the following code?
ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna']
KeyError False 0 -1
KeyError
Which would you use to get the number of elements in a dictionary?
sizeof len length size
len
In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the __________ operator.
is in is not in included in
in