WorksheetsList,Tuples,Dictionary
Total questions: 19
Worksheet time: 12mins
Elements of a tuple can be accessed in the same way as list or string using
Indexing
Slicing
Both indexing and slicing
None of the above
init_tuple = ('Python') * 3
print(type(init_tuple))
A. <class ‘tuple’>
B. <class ‘str’>
C. <class ‘list’>
D. <class ‘function’>
What is the output of the below Python code?
a = {(1,2):1,(2,3):2}
print(a[1,2])
A. Key Error
B. {(2,3):2}
C. 1
D. {(1,2):1}
The code below has a Python function writing to a dictionary. What will it print in the end?
A. 1
B. 2
C. 3
D. 4
What will be the output of the Python code given below?
dict = {'c': 97, 'a': 96, 'b': 98}
for _ in sorted(dict):
print (dict[_])
A. 96 98 97
B. 96 97 98
C. 98 97 96
D. NameError
>>colors = ["red", "green", "burnt sienna", "blue"]
>> "yellow" in colors
What is the result of the yellow in colors expression?
ValueError: 'yellow' is not in list
4
3
False
What’s the main difference between Python lists and tuples?
Lists can hold any data type and tuples can only contain int and str objects.
Lists are immutable and tuples are mutable.
Lists are faster and tuples are slower.
Lists are mutable and tuples are immutable.
Write the Python code that prints out Captain Kirk’s email address from the employee dictionary below:
employee = { "id": 1701, "name": "James T Kirk", "email": "jtkirk@starfleet.com", "position": "captain", }
# Add your code below:
(a)
What will be the output of the following Python code? >>>t = (1, 2) >>>2 * t
A.
(1, 2, 1, 2)
B.
[1, 2, 1, 2]
C.
(1, 1, 2, 2)
D.
[1, 1, 2, 2]
Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t)
What will be the output of the following Python code?
a) [2, 3, 9]
b) [1, 2, 4, 3, 8, 9]
c) [1, 4, 8]
d) (1, 4, 8)
What will be the output of the following Python code?
a) 1
b) 2
c) 5
d) Error
What will be the output of the following Python code?
a) 30
b) 24
c) 33
d) 12
What is the output of the following code
True
False
What will be the output of the following Python code?
A. [1,2]
B. [8,9]
C. [1,3,5,7,9]
D. [1,2,3]
What is the correct command to shuffle the given Python list?
fruit=['apple', 'banana', 'papaya', 'cherry']
A. fruit.shuffle()
B. shuffle(fruit)
C. random.shuffle(fruit)
D. random.shuffleList(fruit)
What will be the output of the following Python code?
A. Exception
B. 5
C. 4
D. None
What will be the output of the following Python code?
print([x**2 for x in range(5)])
[1, 4, 9, 16, 25]
[0, 1, 4, 9, 16]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
What will be the output of the following Python code?
print({x: x**2 for x in range(3)})
A. {0: 0, 1: 1, 2: 4}
B. {0: 0, 1: 1, 2: 2}
C. {0: 0, 1: 1, 2: 3}
D. Error
