Lists: indexing, slicing, operators and methods; dictionaries: keys and values, adding, updating, deleting and traversing them.
| a = [10, 20, 30, 40, 50] | a[1] → 20; a[-1] → 50; a[1:4] → [20, 30, 40]; a[::2] → [10, 30, 50] |
| + and *; built-ins | [1, 2] + [3] → [1, 2, 3]; [1, 2] * 2 → [1, 2, 1, 2]; for [4, 9, 2]: len 3, min 2, max 9, sum 15 |
| pop and remove | pop() removes the LAST item and returns it; remove(x) removes the first x, by value |
| Third largest | a.sort() then a[-3]; third smallest: a[2] |
| Dictionary | d = {'Asha': 78}; d['Asha'] = 81 updates; d['Ravi'] = 64 adds; del d['Ravi'] removes |
| len(d) after changes | updating a key: no change; a new key: one more; del: one fewer |
| Traversing | for k in d: if d[k] >= 75: print(k) # keys in the order added |
Answer (a) to (c) for this program.
a = [45, 47, 6, 37, 27]
a.extend([63, 73])
a.insert(1, 6)
p = a.pop()
print(p)
print(len(a))
print(a.index(6))
Working: after insert: [45, 6, 47, 6, 37, 27, 63, 73]; pop() removes the last item
Answer: (a) 73; (b) 7; (c) 1