Font size
Worksheetslists and dictionaries in python smsmb
Total questions: 60
Worksheet time: 38mins
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]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
"John"
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")
To insert an the string "strawberries" in the first position of a list we use
fruit.append("strawberries, 1")
fruit.insert("strawberries",0)
fruit.insert(1, "strawberries")
fruit.insert(0, "strawberries")
Is this the correct code for a list?
register = {"Sam", "Pheobe", Georgia", Richard"}
Yes
No
To print a list called fruitList we use
print(fruitList)
list(print)
fruitList.print()
fruitList(print)
Which colour will be replaced with yellow?
red
green
blue
none, yellow will be added to the list
This list contains items of what data type?
String
Boolean
Float
Integer
Which symbols are used to open and close a list?
( ) round brackets
( ) curly brackets
[ ] square brackets
" " speech marks
The following array is created in a program: " DIREWOLVES = ["Nymeria","Shaggy Dog","Ghost","Lady","Grey Wind","Summer"]
What is the item stored in DIREWOLVES [2] ?
"Shaggy Dog"
"Ghost"
Which of the following facts about arrays is TRUE?
Lists can contain items of different data types
Arrays can only contain items of a single data type
Lists and arrays are the same thing
A list is a type of variable
In an list, what is the first numbered position in which data can be stored?
Position 0
Position 1
What term is used to describe an individual position of an element in an array?
Index
Pointer
Variable
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']
How do i remove something from a list?
variable.append()
variable.delete()
variable.remove()
variable=(new variable)
What is used to separate elements in a list?
Bracket
Comma
Full Stop
Space
What brackets are used to create a list?
()
[]
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
Which of these operators means EQUAL TO?
'='
'=>'
'<='
'=='
names = ["Paul","Phillip","Paula","Phillipa"]
mystring = 'tomato soup'
mystring is the variable name
'tomato soup' is the value of the variable
print ("I aced the TEST!")
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.insert(0, 'Guam')
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Guam', 'Tinian', 'Rota']
['Saipan', 'Tinian', 'Rota', 'Guam']
['Guam', 'Saipan', 'Tinian', 'Rota']
>>>str="hello"
>>>str[:2]
>>>
amount = 2 + 5 * 2
word= "computer"
for num in range(1,6):
print(word)
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']
Suppose list1 is [1, 5, 9], what is sum(list1) ?
1
9
15
none
Which of the following commands will return the length of the list mylist?
length(mylist)
len(mylist)
mylist.length()
mylist.len()
List need not be always----------------------------------------
Non Homogeneous
Homogeneous
sequence
selective
class and section
a
b
d
jp -a
jp =f
name and branch
Which of the following are true of Python dictionaries:
Dictionaries can be nested to any depth.
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary can contain any object type except another dictionary.
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
Consider this dictionary:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What is the result of this statement:
d['bar':'baz']
It raises an exception
(200, 300)
200 300
[200, 300]
Which of the following could be a valid dictionary key:
(3+2j)
('foo', 'bar')
dict(foo=1, bar=2)
'foo'
['foo', 'bar']
A dictionary is an example of a sequence ...
True
False
A Python dictionary stores ...
value - key pairs
key - value pairs
A key can appear in a dictionary ...
Exactly once
Any number of times
One or more times
Which of the following create an empty dictionary (D)?
D = dict()
D = ()
D = []
D = {}
Select correct ways to create an empty dictionary
sampleDict = {}
sampleDict = dict()
sampleDict = dict{}
Dictionary keys must be immutable
True
False
In Python, Dictionaries are immutable
True
False
________________method will returns number of key-value pairs in the given dictionary.
len( )
pop( )
del( )
keys( )
values( )
Square brackets in an assignment statement will create which type of data structure?
( s=[] )
List
Queue
Dictionary
Set
