Chapter 6 Quiz Python
code example: def main( ): students = [["Lizzy", 73, "C"], ["Mike", 98, "A"], ["Joel", 88, "B+"], ["Anne", 93, "A"]] for student in students" for item in student: print(item, end = " ") if __name__ == "__main__": main( ) What would display if the following three lines were added at the end of the main( ) function? students.sort() students.reverse ( ) print(students)
[["Mike, 98, 'A'], ['Lizzy', 73, 'C'],['Joel', 88, 'B+'], ['Anne', 93, 'A']]
Given the following code, what would the list consist of after the second statement? ages = [22, 35, 24, 17, 28] age.insert(3, 4)
ages = [22, 34, 24, 4, 17, 28]
To refer to an item in a list, you code the list name followed by:
an index number in brackets, starting with the number 0
The ______ method adds an item to the end of a list.
append ( )
Which of the following functions randomly selects one item in a list?
choice( )
Code Example 6-1 def main(): students = [["Lizzy", 73, "C"], ["Mike", 98, "A"], ["Joel", 88, "B+"], ["Anne", 93, "A"]] for student in students: for item in student: print(item, end=" ") if __name__ == "__main__": main() What is in the second row of the students list?
"Mike", 98, "A"
What is the value of the total variable after the following code executes? prices = [10, 15, 12, 8] total = 0 i = 1 while i < len(prices): total += prices[i] i += 1 print(total)
35
Given the following list, what is the value of names[2]? names = ["Lizzy","Mike","Joel","Anne", "Donald Duck"]
Joel
Code Example 6-1 def main(): students = [["Lizzy", 73, "C"], ["Mike", 98, "A"], ["Joel", 88, "B+"], ["Anne", 93, "A"]] for student in students: for item in student: print(item, end=" ") if __name__ == "__main__": main() What will display after the code executes
Lizzy 73 C Mike 98 A Joel 88 B+ Anne 93 A
Given the following list, what is the value of ages[5]? ages = [22, 35, 24, 17, 28]
None: Index error
Given the following code, what would be displayed after the code executes? def main(): furry_pets = ["dog", "cat", "ferret", "hamster", "bunny"] feathered_pets = ["canary", "parrot", "budgie", "hawk"] all_pets = furry_pets + feathered_pets new_pets =[] i = 0 for item in all_pets: if item[i][0] == "c": new_pets.append(item) print("The pet store sells:", all_pets) print("These start with the letter c:", new_pets)
The pet store sells: ['dog', 'cat', 'ferret', 'hamster', 'bunny', 'canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['cat', 'canary']
Which of the following is not true about a list of lists?
To delete an item in the outer list, you first have to delete the list in the item.
What will display after the following code executes? def add_item(list, food): food = "apple pie" list.append(food) def main(): lunch = ["sandwich", "chips", "pickle"] food = "banana" add_item(lunch, food) print(lunch) main()
['sandwich', 'chips', 'pickle', 'apple pie']
When a function changes the data in a list, the changed list
does not need the be returned because lists are mutable
To insert the item "melon" after "grapes" in the following list, you would use which of these methods? fruit = ["apple", "banana", "grapes", "mangos", "oragnes"
fruit.insert(3,"melon")
To remove the item "mangos" from the following list, you would use which of these methods? fruit = ['apple', 'banana', 'grapes', 'mangos', 'oranges']
fruit.remove('mangos') or fruit.pop(3)
What would be displayed after the following code snippet executes? costumes = ["ghost", "witch", "elf", "ogre"] name = "elf" if name in costumes: costuems.remove(name) for item in costumes: print(item)
ghost witch ogre
The primary difference between a tuple and a list is that a tuple
is imutable
Given the following code, what is the value of my_name and what does the list consist of after the second statement is executed? names = ["Lizzy", "Mike", "Joel", "Anne", "Donny"] my_name = name.pop()
my_name = "Donny" names = ["Lizzy", "Mike", "Joel", "Anne"]
Which of the following would create a list named numbers consisting of 3 floating-point items?
numbers = [5.3, 4.8, 6.7]
Which of the following creates a tuple of six strings?
vehicles = ("sedan" , "SUV" , "motorcycle" , "bicycle", "hatchback", "truck")
Given the tuple that follows, which of the following assigns the values in the tuple to variables? numbers = (22, 33, 44, 55)
w, x, y, z = numbers
When you use a multiple assignment statement to unpack a tuple.
you assign the tuple to a two or more variable names separated by commas