NEW
Font size
WorksheetsCodeHS Basic Data Structures in Python
Total questions: 26
Worksheet time: 20mins
s = 0
for d in range(0, 5, 0.1):
s += d
print(s)
Syntax Error
Type Error
Name Error
Traceback Error
Output of: max(''please help '')
please help
Error
p
s
What command is used to shuffle a list ‘L’?
L.shuffle()
random.shufflelist(L)
shuffle(L)
random.shuffle(L)
What is used to define a block of code (body of loop, if else etc.) in Python?
Indentation
Curly Braces
Parenthesis
Quotation
In the following code, i is a/an _______?
i = (2)
String
Tuple
Int
Boolean
What is the output of the following program?
language = ['P', 'y', 't', 'h', 'o', 'n']
print(language[:-4])
['t']
['P','y']
['P','y','t']
['t','h','o','n']
Suppose a tuple test contains 5 elements. How can you set the 3rd element of the tuple to ‘Python’?
test[2] = 'Python'
test[3] = 'Python'
test(2) = 'Python'
Elements of tuple cannot be changed
What is the output of the following program?
print((1, 2) + (3, 4))
(4, 6)
(1,2,3,4)
((1,2),(3,4))
Error
names = "{1}, {2} and {0}".format('John', 'Bill', 'Sean')
print(names)
John, Bill, Sean
Bill, Sean, John
Bill, John, Sean
John, Sean, Bill
How can you change
num = {‘one’: 1, ‘two’: 3}
to
num = {‘one’: 1, ‘two’: 2}
num[2] = 'two'
num[1] = 'two'
num['two'] = 2
num['two'] = '2'
What is the output of the following program?
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
print(squares.pop(4))
print(squares)
16
{1: 1, 2: 4, 3: 9, 5: 25}
16
{1: 1, 2: 4, 3: 9, 4:16, 5: 25}
4
{1: 1, 2: 4, 3: 9, 5: 25}
4
{1: 1, 2: 4, 3: 9, 4:16, 5: 25}
What is the output of the following code?
i = 0
while i < 3:
print(i)
i += 1
else:
print(0)
0 1 2 3 0
0 1 2 0
0 1 2
Error
print('abcefd'.replace('cd', '12'))
abcefd
ab1cef2
Error
ab12ef
What is the output of the following expression if X=345?
print(“%06d”,%X)
345000
000345
000000345
345000000
What is the output of the code shown below?
L=list('HELLO')
p=L[0], L[-1], L[1:3]
'a={0}, b={1}, c={2}'.format(*p)
Error
a='H', b='O', c=['E',L']
a='H', b='E', c=['L',L','O']
a='H', b='E', c='L'
What is the output of the following?
True = False
while True:
print(True)
True
False
Infinite Loop
Error
What is the output of the following?
x = 123
for i in x:
print(i)
123
1 2 3
Error
1,2,3
Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
[(2**x) for x in range(0, 13)]
[(2**x) for x in range(1, 13)]
[(x**2) for x in range(0, 13)]
[(x**2) for x in range(1, 13)]
What is the output of the code shown below?
l1=[1,2,3]
l2=[4,5,6]
[x*y for x in l1 for y in l2]
a) [4, 8, 12, 5, 10, 15, 6, 12, 18]
[18, 12, 6, 15, 10, 5, 12, 8, 4]
[4, 5, 6, 8, 10, 12, 12, 15, 18]
[4, 10, 18]
What is the output of the following?
print([i+j for i in "abc" for j in "def"])
a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
[‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
[[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
[[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
What is the output when following code is executed ?
names = ['Amir', 'Bear', 'Charlton', 'Daman']
print(names[-1][-1])
n
D
Daman
Error
If a=(1,2,3,4), a[1:-1] is
[2,3]
(2,3)
[2,3,4]
Error, slicing doesn't exist
Is the following piece of code valid?
a=1,2,3,4
print(a)
Yes
No
What is the output of the following code?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
1 A 2 B 3 C
1 2 3
"A" "B" "C"
1:"A" 2:"B" 3:"C"
What is the output of the following piece of code?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
"A"
True
4
Error
What is the output of the following code?
a={"a":1,"b":2,"c":3}
b=dict(zip(a.values(),a.keys()))
b
{‘a’: 1, ‘b’: 2, ‘c’: 3}
Error
{'a','b','c'}
{1: ‘a’, 2: ‘b’, 3: ‘c’}
