Font size
WorksheetsPython Unit 11 - Collections: Dictionary, List, Set, Tuple
Total questions: 40
Worksheet time: 20mins
To create a set, the following would be used -
( )
[ ]
{ }
{ key : value }
To create a list, the following would be used -
( )
[ ]
{ }
{ key : value }
To create a tuple, the following would be used -
( )
[ ]
{ }
{ key : value }
To create a dictionary, the following would be used -
( )
[ ]
{ }
{ key : value }
What type of data collection is this?
[ 3, 4, 5, 6 ]
Dictionary
List
Set
Tuple
What type of data collection is this?
( 3, 4, 5, 6 )
Dictionary
List
Set
Tuple
What type of data collection is this?
{ 3, 4, 5, 6 }
Dictionary
List
Set
Tuple
What type of data collection is this?
{ student: "Bill", grade: 92, class: "Python" }
Dictionary
List
Set
Tuple
What is a list in python?
A collection of variables
A collection of items assigned to a single variable
A statement that cant be changed
A Christmas list
Which of these codes is correct for creating a list of numbers?
num = ('10','29','37','109')
num = ['10','29','37','109']
num == ('10','29','37','109')
num = ['10 29 37 109']
What is the index number for 'Spain'?
['England','Brazil','Spain','France']
0
1
2
3
How do i remove something from a list?
variable.append()
variable.delete()
variable.remove()
variable=(new variable)
Which code would i use to add an item to a list?
variable.add()
variable.append()
append.variable()
add.variable()
variable.sort() will do what?
Sort the list into alphabetical order
Sort the list into reverse order
Create a new list
Error
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
What would this code show on the screen?
7
6
5
4
An error
Each item in a list has an "address" or index. The first index in a Python list is what?
1
0
a
A
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
del mariana_islands[0]
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Tinian', 'Rota']
['Saipan', 'Tinian']
['Saipan', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands[0] = 'Guam'
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Guam', 'Tinian', 'Rota']
['Saipan', 'Tinian', 'Rota', 'Guam']
['Guam', 'Saipan', 'Tinian', 'Rota']
nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]
The list will be slice and show Harry and Jesse only. Which piece of code below would do this?
print(nameList[1:3])
print(nameList[0:1])
print(nameList[1:2])
print(nameList["Harry":"Jesse"])
Tuples in Python are mutable
False
True
What is the output of the program above
<class 'tuple'>
<class 'None'>
<class 'list'>
<class 'int'>
What will be the output?
var1=['sam', "Pam", 12,44]
var1[3]="Ram"
print(var1)
['sam', 'Pam', 12, 44]
['sam', 'Pam', 12, 'Ram']
It will give an error
['sam', 'Pam', 'Ram',44]
The code snippet above outputs
error
None
NaN
-1
Assume the key is the student's id and the value is the grade. Which of the following code will get the grade of student id 102.
grades[102]
grades.get(102)
grades[2]
grades[3]
grades.key(102)
Which of the following statements is used to create an empty set?
{ }
set()
[ ]
( )
If a={5,6,7}, what happens when a.add(5) is executed?
a={5,5,6,7}
a={5,6,7}
Error as there is no add function for set data type
Error as 5 already exists in the set
What will be the output of the following Python code?
>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
>>> a
{2,3}
Error, duplicate item present in list
Error, no method called intersection_update for set data type
{1,4,5}
What will be the output of the following Python code?
>>> a={1,2,3}
>>> b=a.copy()
>>> b.add(4)
>>> a
{1,2,3}
Error, invalid syntax for add
{1,2,3,4}
Error, copying of sets isn’t allowed
What will be the output of the following Python code?
s=set()
type(s)
<’set’>
<class ‘set’>
set
class set
Select all of the types of data collections that are ORDERED.
(that means items can be referenced by location or key)
Dictionary
List
Tuple
Set
Select all of the types of data collections that ALLOW duplicate values.
Dictionary
List
Tuple
Set
Select all of the types of data collections that ALLOW individual items within the collection to be changed or modified.
Dictionary
List
Tuple
Set
Which command will allow you to attempt to remove an item from a set without raising an error if it isn't in the set.
.remove( )
.discard( )
.pop( )
.delete( )
Which command will allow you to remove the last item in a collection.
.remove( )
.discard( )
.pop( )
.delete( )
Which command will allow you to attempt to remove an item from a set and will raise an error if it isn't in the set
.remove( )
.discard( )
.pop( )
.delete( )
Which method will combine the contents of two sets WITHOUT changing one of the existing sets?
.update( )
.union( )
.add( )
.intersection( )
