NEW
Font size
WorksheetsChapter 6 - Part Two: How to Work with Lists and Tuples
Total questions: 39
Worksheet time: 20mins
What does the add_to_list() function do in the given code?
The add_to_list() function
def add_to_list(list, item):
list.append(item) # list object changed
The calling code in the main() function
# list object created
inventory = ["staff", "hat", "bread"]
add_to_list(inventory, "robe")
print(inventory) # ["staff", "hat", "bread", "robe"]
# NOTE: no need to return list object
Adds an element to the end of a list
Removes an element from a list
Sorts the list in ascending order
Clears all elements from the list
Why is there no need to return the list object in the add_to_list() function?
The add_to_list() function
def add_to_list(list, item):
list.append(item) # list object changed
The calling code in the main() function
# list object created
inventory = ["staff", "hat", "bread"]
add_to_list(inventory, "robe")
print(inventory) # ["staff", "hat", "bread", "robe"]
# NOTE: no need to return list object
Because the list is immutable.
Because the list is passed by reference.
Because the function automatically returns the list.
Because the list is a global variable.
Based on the hierarchy chart for the Movie List program, which of the following is not a function listed under 'main'?
display menu
list movies
add movie
search movie
What is the purpose of the 'display_menu' function in the Movie List program?
To add a new movie to the list
To display the list of movies
To show the available options to the user
To delete a movie from the list
Which command in the 'display_menu' function is used to add a movie?
list
add
del
exit
How does the 'list' function work in the Movie List program?
It adds a new movie to the list.
It removes a movie from the list.
It displays all movies in the list.
It sorts the movies in the list alphabetically.
What is the purpose of the 'delete' function in the Movie List program?
To add a new movie to the list
To remove a movie from the list
To update the details of a movie
To sort the movies in the list
Which of the following movies is not in the initial 'movie_list'?
Monty Python and the Holy Grail
On the Waterfront
Cat on a Hot Tin Roof
The Godfather
What will happen if the user inputs a number greater than the length of 'movie_list' in the 'delete' function?
The function will delete the last item in the list.
The function will throw an IndexError.
The function will do nothing.
The function will delete the first item in the list.
What is the purpose of the 'while True:' loop in the Movie List program?
To create an infinite loop until a condition is met to break it.
To iterate over a list of movies.
To execute a block of code a specific number of times.
To handle exceptions in the program.
Which command will add a movie to the list in the Movie List program?
A) list
B) add
C) del
D) exit
What will the program print if an invalid command is entered?
Error: Invalid command
Command not recognized
Please enter a valid command
Invalid input, try again
How do you define a list of lists with 3 rows and 4 columns in Python? Use the example of students provided in the image.
students = [['Alice', 'Bob', 'Charlie', 'David'], ['Eve', 'Frank', 'Grace', 'Heidi'], ['Ivan', 'Judy', 'Mallory', 'Niaj']]
students = [['Alice', 'Bob'], ['Charlie', 'David'], ['Eve', 'Frank'], ['Grace', 'Heidi']]
students = [['Alice', 'Bob', 'Charlie'], ['David', 'Eve', 'Frank'], ['Grace', 'Heidi', 'Ivan'], ['Judy', 'Mallory', 'Niaj']]
students = [['Alice', 'Bob', 'Charlie', 'David', 'Eve'], ['Frank', 'Grace', 'Heidi', 'Ivan', 'Judy'], ['Mallory', 'Niaj', 'Oscar', 'Peggy', 'Quentin']]
Fill in the blank: To define a list of lists for movies with 3 rows and 3 columns, you can use the following syntax: movies = [["The Holy Grail", 1975, 9.99], ["Life of Brian", 1979, 12.30], ["The Meaning of Life", 1983, ____.]]
14.50
15.00
13.75
____
What is the title of the movie released in 1975?
Life of Brian
The Holy Grail
The Meaning of Life
Fill in the blank: The movie 'Life of Brian' was released in ____.
1979.
1982.
1975.
1980.
What is the rating of the movie 'The Meaning of Life'?
9.99
12.3
7.5
Fill in the blank: The movie 'Life of Brian' was released in _____.
1979
1982
1975
1985
Fill in the blank: The price of 'The Meaning of Life' is _____.
7.5
10.0
5.0
12.5
What command is used to list all movies in the Movie List 2D program?
add
list
del
exit
The command to exit the Movie List 2D program is ______.
exit
quit
close
terminate
What is the purpose of the 'list' function in the Movie List 2D program?
To add a new movie to the list
To display the list of movies
To sort the movies alphabetically
To remove a movie from the list
What is the purpose of the 'delete' function in the Movie List 2D program?
To add a new movie to the list
To remove a movie from the list
To update the details of a movie
To display all movies in the list
What is the purpose of the 'movie_list' variable in the code?
To store a list of movie titles.
To count the number of movies.
To display movie ratings.
To sort movies by release date.
Which command will add a new movie to the list?
A) list
B) add
C) del
D) exit
What does the count(item) method do in Python lists?
It adds an item to the list.
It removes an item from the list.
It returns the number of occurrences of an item in the list.
It sorts the list in ascending order.
How does the reverse(list) method work in Python?
It returns a new list that is the reverse of the original list.
It reverses the list in place and returns None.
It sorts the list in descending order.
It creates a copy of the list and reverses the copy.
What is the purpose of the sort([key=function]) method in Python lists?
To sort the list in ascending order by default.
To sort the list based on a specified key function.
To reverse the list order.
To shuffle the list randomly.
How does the sorted(list[, key=function]) built-in function differ from the sort method in Python?
sorted() returns a new sorted list, while sort() sorts the list in place.
sorted() can only sort lists, while sort() can sort any iterable.
sorted() is faster than sort() for large lists.
sorted() does not accept a key function, while sort() does.
What is the output of the following code? numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] count = numlist.count(14)
A) 1
B) 2
C) 3
D) 4
What will be the order of elements in 'numlist' after executing the following code? numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] numlist.reverse()
[5, 15, 84, 3, 14, 2, 8, 10, 14, 25]
[25, 14, 10, 8, 2, 14, 3, 84, 15, 5]
[2, 3, 5, 8, 10, 14, 14, 15, 25, 84]
[84, 25, 15, 14, 14, 10, 8, 5, 3, 2]
Fill in the blank: After sorting 'numlist', the order of elements will be ____. numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] numlist.sort()
[2, 3, 5, 8, 10, 14, 14, 15, 25, 84]
[84, 25, 15, 14, 14, 10, 8, 5, 3, 2]
[5, 15, 84, 3, 14, 2, 8, 10, 14, 25]
[3, 2, 5, 8, 10, 14, 14, 15, 25, 84]
What is the result of the following code? foodlist = ["orange", "apple", "Pear", "banana"] foodlist.sort()
["apple", "banana", "orange", "Pear"]
["Pear", "apple", "banana", "orange"]
["orange", "apple", "banana", "Pear"]
Fill in the blank: To sort a list in Python while ignoring case, you can use the key argument in the sort() method like this: foodlist.sort(key=____).
str.lower
str.upper
len
sorted
What is the output of the following code? foodlist = ["orange", "apple", "Pear", "banana"] sorted_foodlist = sorted(foodlist) print(sorted_foodlist)
['apple', 'banana', 'orange', 'Pear']
['Pear', 'apple', 'banana', 'orange']
['orange', 'apple', 'Pear', 'banana']
Fill in the blank: To sort a list in Python while ignoring case, you can use the key argument in the sorted() function as follows: sorted(foodlist, key=______).
str.lower
str.upper
len
int
What does the built-in function min(list) do in Python?
Returns the maximum value in the list
Returns the minimum value in the list
Sorts the list in ascending order
Sorts the list in descending order
What does the built-in function max(list) do in Python?
Returns the minimum value in the list
Returns the maximum value in the list
Sorts the list in ascending order
Sorts the list in descending order
What does the function choice(list) from the random module do in Python?
Selects a random element from a non-empty sequence.
Sorts the list in random order.
Removes a random element from the list.
Duplicates a random element in the list.
