WorksheetsPython - Lists, Dictionaries and Loops
Total questions: 40
Worksheet time: 30mins
list[a:b] will return the portion of list starting with index a and ending before index b.
True
False
What will be the value of last_jedi after the following code is run?
["kylo", "rey", "finn", "luke"]
["kylo", "rey", "finn"]
3
4
What will be the value of authors after the following code is run?
["Martin", "Rowling", "Tolkien"]
["Martin"]
["Rowling"]
["Tolkien", "Rowling", "Martin"]
What will this code print?
6
1
pants
"pants": 6
What will be the value of colors after the following code is run?
["light blue", "pink"]
["light blue", "yellow", "pink"]
["pink", "yellow"]
["light blue", "yellow"]
What is the correct syntax to write a for loop to iterate through the list grades?
for variable:
for variable in grades
variable in grades:
for variable in grades:
What does the following code do?
It prints [0, 6].
It prints all numbers in range(0, 6) in a list.
It prints all numbers in range(0, 6) separately.
It prints True or False.
Fill in the blank so that print_list(x) prints every other item in list x:
range(0, len(x), -2)
range(0, x, 2)
range(0, len(x), 2)
range(0, len(x), 1)
What will be the value of n after the following code is run?
[6]
[1, 8, 6, 4, 2]
[8, 6, 4, 2, 1]
[8, 4, 2]
What will be the value of n after the following code is run?
[2, 2, 3, 5]
[1, 2, 4, 5]
[1, 3, 3, 5]
[2, 3, 4, 6]
What will be the value of n after the following code is run?
[1, 3, 5]
[3]
[1, 5, 7]
[1, 3, 7]
How many lines will the code below print to the console?
4
5
2
3
What does the zip function do?
It creates pairs of elements when passed integers
It creates pairs of elements when passed two lists
It passes more lists into a function
It pairs integers in dictionary values
Which function can be used to supply an index to each element of a list as you iterate through it?
index()
enumerate()
itemize()
track()
Which of the following is the correct way to create the following list of names: 'Tom', 'Jerry', 'Tweetie', 'Sylvester'?
What list would be created by this command: list(range(2, 14, 4))?
[4, 8, 10, 12]
[4, 8, 10, 12, 14]
[2, 6, 10]
[2, 6, 10, 14]
Is the following list a valid Python list?
Yes, lists can contain multiple data types.
No, lists can only contain one data type.
Which of these commands would create a list of numbers starting at 0 and up to (but not including) 9?
list(9)
list(range(10))
list(range(9))
list(range([0, 9]))
How could we add ['Hawaii', 'Alaska'] to the list states?
Which of the following is the correct way to create an empty list?
empty_list = []
empty_list = {}
empty_list = [0]
empty_list = None
Which of the following would create a range object that starts at 3, goes up to but not including 15 where each number is 4 greater than the previous number?
range(3, 15, 4)
range(3, 4, 15)
range(4, 15, 3)
range(15, 4, 3)
Which of the following is the correct way to add the number 4 to mylist?
mylist + 4
mylist.append(4)
mylist.append([4])
4.append(mylist)
Which of the following lines of code would select the list ['b', 'c'] from mylist:
mylist[1:3]
mylist[1,3]
mylist[1,2]
mylist[1:2]
Which of the following lines of code will correctly sort mylist?
mylist.sorted()
sort(mylist)
mylist.sort
mylist.sort()
Which of the following would be generated by the code snippet?
['b', 'c', 'd']
['b', 'c', 'd', 'e']
['c', 'd', 'e']
['c', 'd', 'e', 'f']
Which of the following commands will return the length of the list mylist?
length(mylist)
len(mylist)
mylist.length()
mylist.len()
Which of the following lines of code would select the letter d from mylist:
mylist[4]
mylist{4}
mylist[3]
mylist(3)
What would be the output of this code:
hello! hello! hello! hello!
2 4 6 8
hello!
2 hello! 4 hello! 6 hello! 8 hello!
What would be the output of this code:
0 1 2 3 4
3 3 3 3 3
5 5 5
0 1 2
Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:
while i > 10:
while i == range(11):
while i <= 10:
while i < 10:
What would be the output of this code:
i i i i
1 2 3
0 1 2
i i i
What would be the output of this code:
1 1 2 3
2
1 1
1 1 3
What would be the output of this code:
["coffee", "tea", "water", "juice", "soda"]
drink
"coffee"
"coffee" "tea" "water" "juice" "soda"
What is the line of code to initialize an empty dictionary called thesaurus?
thesaurus = new Dictionary();
thesaurus = {};
thesaurus = empty_dict();
thesaurus = [];
Which of these is an invalid dictionary (will result in a TypeError when trying to run)?
All of these are valid
What is the value of inventory after this code is run?
Which of these dictionaries has integers as the keys and strings as the values?
What will the following code output?
"soda"
KeyError
["veggie burger", "salad", "soda"]
["hamburger", "fries", "soda"]
What is the output of the following code?
"Moonlight"
"Casey Affleck"
"Emma Stone"
"Zootopia"
("Best Picture", "Moonlight")
("Best Actor", "Casey Affleck")
("Best Actress", "Emma Stone")
("Animated Feature", "Zootopia")
"Best Picture"
"Best Actor"
"Best Actress"
"Animated Feature"
"Best Picture" : "Moonlight"
"Best Actor": "Casey Affleck"
"Best Actress": "Emma Stone"
"Animated Feature": "Zootopia"
What is the output of the following code?
"Best Picture"
"Best Actor"
"Best Actress"
"Animated Feature"
"Moonlight"
"Casey Affleck"
"Emma Stone"
"Zootopia"
("Best Picture", "Moonlight")
("Best Actor", "Casey Affleck")
("Best Actress", "Emma Stone")
("Animated Feature", "Zootopia")
"Best Picture" : "Moonlight"
"Best Actor": "Casey Affleck"
"Best Actress": "Emma Stone"
"Animated Feature": "Zootopia"
