WorksheetsPython Lists Worksheet Questions
Total questions: 35
Worksheet time: 18mins
What will be the output?
lst = [1, 2] * 2
print(lst)
[1, 2, 1, 2]
[2, 4]
Error
[1, 2, 2, 4]
Which method adds an element at a specific index?
append()
insert()
extend()
add()
What is the result of slicing?
a = [10, 20, 30, 40]
print(a[1:-1])
[20, 30]
[10, 20]
[30, 40]
[20]
What will be the output?
a = [3,1,2]
a.sort()
print(a)
[3,2,1]
[1,2,3]
Error
[2,1,3]
Which creates a list?
(1,2,3)
{1,2,3}
[1,2,3]
<1,2,3>
What does this produce?
x = [1,2,3]
y = x
y.append(4)
print(x)
[1,2,3]
[1,2,3,4]
Error
[4]
Output?
print([1,2,3] + [4,5])
[1,2,3,[4,5]]
[1,2,3,4,5]
(1,2,3,4,5)
Error
Which removes the first matching element?
delete()
remove()
pop()
discard()
Output?
a = [10, 20, 30, 40]
print(a[::-1])
[40,30,20,10]
[10,20,30,40]
Error
[30,20]
What will be printed?
lst = [1, 2, 3]
lst.extend([4, 5])
print(lst)
[1,2,3,4,5]
[1,2,3,[4,5]]
[4,5,1,2,3]
Error
Which creates a tuple?
t = (1,2,3)
t = [1,2,3]
t = {1,2,3}
t = <1,2,3>
Single-element tuple?
(5)
(5,)
[5,]
{5}
Output?
t = (1,2,3)
print(len(t))
2
3
Error
1
Can we add an element to a tuple?
Yes, using append()
Yes, using insert()
No, tuple is immutable
Yes, using update()
What is returned by index()?
t = (1,2,3,2)
print(t.index(2))
1
3
[2]
Error
Output?
t = (1,2,3)
t2 = t + (4,)
print(t2)
(1,2,3)
Error
(1,2,3,4)
(4,1,2,3)
Output of count()?
t = (1, 1, 2, 3)
print(t.count(1))
0
1
2
Error
Which is mutable?
tuple
list
both
none
Which statement defines a dictionary?
d = {}
d = []
d = ()
d = <>
Output?
d = {"a":1}
d["b"] = 2
print(d)
{"a":1}
{"b":2}
{"a":1,"b":2}
Error
Removing an element?
remove()
delete()
pop()
discard()
What types can be dictionary keys?
list
set
tuple
dict
Why can’t lists be keys?
Lists are slow
Lists are mutable and unhashable
Lists are large
Lists store duplicates
What does hashing help with?
Storing values in order
Fast key lookup
Sorting keys
Encrypting data
Output?
d = {"x":10, "y":20}
print(len(d))
1
2
3
Error
Output?
d = {"a":1, "b":2}
Nothing is printed
{"a":1, "b":2}
Error
2
print(d.get("c"))
0
None
Error
"c"
Output?
d = {}
d[1] = "A"
print(d)
Error
{1:"A"}
{"1":"A"}
{}
What is the result?
A = {1,2}
B = {2,3}
print(A | B)
{2}
{1,2,3}
{1}
Error
Intersection?
A = {1,2,3}
B = {2,4}
print(A & B)
{1}
{2}
{1,2,3,4}
{}
Difference?
print({1,2,3} - {2})
{2}
{1,3}
{1,2,3}
{}
Which creates a set?
{1,2,3}
[1,2,3]
(1,2,3)
<1,2,3>
Output?
s = {1,2,3}
s.add(4)
print(s)
{1,2,3}
{4}
{1,2,3,4}
Error
What happens?
s = {1,2,3}
s.remove(5)
{1,2,3,5}
{1,2,3}
KeyError
None
Which is valid set comprehension?
{x for x in range(5)}
[x for x in range(5)]
(x for x in range(5))
