Learn › Class 11 › Computer Science / Informatics Practices › Lists, tuples, dictionaries
Lists, tuples and dictionaries
Work it out on paper first. Then check the hint or the answer.
1. L = [4, 8, 15, 16, 23, 42]. What is L[-2]?
Hint
−1 is the last item.
Answer and explanation
Answer: 23
L[-1] is 42, so L[-2] is 23.
2. For the same L, what is len(L[1:5])?
Hint
Count indexes 1 to 4.
Answer and explanation
Answer: 4
L[1:5] is [8, 15, 16, 23], which has 4 items.
3. d = {'a': 1, 'b': 2}, then d['c'] = 3 runs. What is len(d)?
Hint
Assigning to a new key adds a pair.
Answer and explanation
Answer: 3
d now has keys 'a', 'b' and 'c'.
4. t = (1, 2, 3) * 2. What is len(t)?
Hint
* repeats the tuple.
Answer and explanation
Answer: 6
t is (1, 2, 3, 1, 2, 3), with 6 items.