Homework 9

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

You cannot use a for loop to iterate over the characters in a string. T/F

False

A dictionary can include the same value several times but cannot include the same key several times. T/F

True

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. T/F

True

If the + operator is used on strings, it produces a string that is a combination of the two strings used as its operands. T/F

True

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised. T/F

True

In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead. T/F

True

Which method would you use to determine whether a certain substring is present in a string? endswith(substring) find(substring) startswith(substring) replace(string, substring)

find(substring)

If the start index is ________ the end index, the slicing expression will return an empty string. equal to less than greater than less than or equal to

greater than

Which method would you use to get all the elements in a dictionary returned as a list of tuples? pop items list keys

items

In a dictionary, you use a(n) ________ to locate a specific value. element item datum key

key

Which would you use to get the number of elements in a dictionary? sizeof size length len

len

Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary? items pop popitem list

pop

What will be displayed after the following code executes? mystr = 'yes' yourstr = 'no' mystr += yourstr * 2 print(mystr) yes + no yes + no yesnono yesnoyesno yes + no * 2

yesnono

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 displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'CA' in cities: del cities['CA'] cities['CA'] = 'Sacramento' print(cities) {'CA': 'Sacramento', 'NY': 'Albany', 'GA': 'Atlanta'} {'CA': 'Sacramento'} {'NY': 'Albany', 'GA': 'Atlanta'} ['CA': 'Sacramento']

{'CA': 'Sacramento', 'NY': 'Albany', 'GA': 'Atlanta'}

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'FL' in cities: del cities['FL'] cities['FL'] = 'Tallahassee' print(cities) {'FL': 'Tallahassee'} {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'} KeyError {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta', 'FL' 'Tallahassee'}

{'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'}

What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'FL' in cities: del cities['FL'] cities['FL'] = 'Tallahassee' print(cities) KeyError {'GA': 'Atlanta', 'FL': 'Tallahassee', 'NY': 'Albany', 'CA': 'San Diego'} {'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'} {'FL': 'Tallahassee'}

{'GA': 'Atlanta', 'FL': 'Tallahassee', 'NY': 'Albany', 'CA': 'San Diego'}

What is the value of the variable phones after the following code executes? phones = {'John' : '5555555', 'Julie' : '5557777'} phones['John'] = '5556666' {'John' : '5556666', 'Julie' : '5557777'} {'John' : '5556666'} {'John' : '5555555', 'Julie' : '5557777'} This code is invalid.

{'John' : '5556666', 'Julie' : '5557777'}

What will be assigned to the string variable pattern after the following code executes? i = 3 pattern = 'z' * (5 * i) 'zzzzzzzzzzzzzzz' 'z * 15' 'zzzzz' Nothing; this code is invalid

'zzzzzzzzzzzzzzz'

What are the valid indexes for the string 'New York'? -1 through -8 0 through 7 -1 through 6 0 through 8

0 through 7

Indexing of a string starts at 1 so the index of the first character is 1, the index of the second character is 2, and so forth. T/F

False

The following code will display 'yes + no': mystr = 'yes' yourstr = 'no' mystr += yourstr print(mystr) T/F

False

The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters. T/F

False

What will be assigned to the variable s_string after the following code executes? special = '1357 Country Ln.' s_string = special[4:] '57 C' 'Coun' '1357' ' Country Ln.'

' Country Ln.'

What will be assigned to the variable some_nums after the following code executes? special = '0123456789' some_nums = special[0:10:2] '02020202020202020202' '0123456789' '24682468' '02468'

'02468'

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 the value of the variable string after the following code executes? string = 'Hello' string += ' world!' ' world!' 'Hello world!' 'Hello' Nothing; this code is invalid

'Hello world!'

What is the number of the first index in a dictionary? 1 0 the size of the dictionary minus one Dictionaries are not indexed by number.

Dictionaries are not indexed by number.

What will display after the following code executes? password = 'ILOVEPYTHON' if password.isalpha(): print('Invalid, must contain one number.') elif password.isdigit(): print('Invalid, must have one non-numeric character.') elif password.isupper(): print('Invalid, cannot be all uppercase characters.') else: print('Your password is secure!') Invalid, must have one non-numeric character. Invalid, must contain one number.Invalid, cannot be all uppercase characters. Your password is secure! Invalid, must contain one number.

Invalid, must contain one number.

What does the get method do if the specified key is not found in the dictionary? It returns a default value. It throws an exception. It does nothing. You cannot use the get method to specify a key.

It returns a default value.

What will be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna'] -1 False 0 KeyError

KeyError

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('/') ['03', '/', '07', '/', '2018'] ['3', '/', '7', '/', '2018'] ['3', '7', '2018'] ['03', '07', '2018']

['03', '07', '2018']

Which would you use to delete an existing key-value pair from a dictionary? delete del remove unpair

del

Which method would you use to determine whether a certain substring is the suffix of a string? startswith(substring) find(substring) replace(string, substring) endswith(substring)

endswith(substring)

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the ________ operator. in included isin isnotin

in

What is the return value of the string method lstrip()? the string with all leading tabs removed the string with all leading whitespaces removed the string with all whitespaces removed the string with all leading spaces removed

the string with all leading whitespaces removed


Kaugnay na mga set ng pag-aaral

MT 100: Week 7 - Knowledge Check

View Set

Environmental Unit 7 Test (Ch. 16-17) Review

View Set

Yahoo DSP Certification Training

View Set