Python Chapter 3: Lists
What item was deleted from the list? fruits = ['orange', 'bannana', 'apple'] del fruits[0]
'orange'
What is the proper syntax of the remove() method? A. fruits.remove() B. remove(0, fruits)
A
What is a List?
A list is an ordered set of information.
What is the output of this code? fruits = ['orange', 'bannana', 'apple'] yellow = 'bannana' fruits.remove(yellow) print(yellow.title() + 's are gross!')
Bananas are gross!
What is len() used for?
Determining the length of objects such as lists.
True or False: You can access items deleted by the del keyword.
False
True or False: This code will run without an error: fruits = ['orange', 'bannana', 'apple'] print(fruits[3])
False: The list only has indexes off 0, 1, and 2.
What is the output of the following code? fruits = ['orange', 'bannana', 'apple'] print('I love ' + fruits[0] + 's and ' + fruits[1] + 's too!')
I love oranges and bananas too!
What is the reverse() method used for and when should you use it?
It reverses the order of the list and should be used when you want the reverse chronological order of a list.
What is the output of this code? fruits = ['orange', 'bannana', 'apple'] print(fruits[0].title())
Orange
What is the difference between sort() and sorted()?
sort() is written as list_name.sort() and permanently changes the list. sorted() is written as sorted(list_name) and temporally changes the list.
What argument can you place in the parentheses of the sort() method to make it sort in reverse?
sort(reverse=True)
What is the simplest way to add data to a list?
Use the append() method.
When should you use the del keyword to remove information from a list?
When you know the exact index of the item.
What is the output of this code? fruits = ['orange', 'bannana', 'apple'] fruits.sort() print(fruits.sort())
['apple', banana', 'orange']
What is the output of the following code? fruits = ['orange', 'bannana', 'apple'] popped_fruit = fruits.pop() print(fruits) print(popped_fruit)
['orange', banana'] apple
What is the output of the following code? fruits = ['orange', 'bannana', 'apple'] fruits.insert(1, 'berries') print(fruits)
['orange', berries', banana', 'apple']
What is the output of this code? fruits = ['orange', 'bannana', 'apple'] fruits[0] = 'carrots' print(fruits[0])
carrots
What is the key difference between del and pop()?
del removes items permanently while pop() allows the removed values to be stored in variables.
Which index is 'apple' equal to? fruits = ['orange', 'bannana', 'apple']
fruits[2]
What does the pop() method do?
The pop() method removes an element from a list but does not delete it permanently. You must know the value of the item to use the pop() method.
What is the output of this code? new_list = [] new_list.append('trees') new_list.append('bushes') print(new_list[0].title() + ' and ' + new_list[1] + ' are common plants.')
Trees and bushes are common plants.
True or False: Accessing the -1st index of a list will return the last item in the list.
True
True or False: pop() can be used to remove any item from a list. For example, pop(3).
True
True or False: sort() permanently alters a list.
True