Q31. Which statement is true about tuples?
Think about whether you can change elements after creation.
Answer: Tuples are immutable
Explanation: Once created, tuple elements cannot be changed; lists are mutable.
Explanation: Once created, tuple elements cannot be changed; lists are mutable.
Q32. Which tuple method returns the index of a value?
Method name suggests position.
Answer: index()
Explanation: tuple.index(value) returns the first index of value; count() returns occurrences.
Explanation: tuple.index(value) returns the first index of value; count() returns occurrences.
Q33. Unpack values a,b from tuple t = (1,2).
t = (1, 2)
= t
= t
Use comma-separated variable names on the left.
Answer: a, b
Explanation: a, b = t assigns 1 to a and 2 to b via tuple unpacking.
Explanation: a, b = t assigns 1 to a and 2 to b via tuple unpacking.
Q34. Create a set from list lst = [1,2,2,3] to remove duplicates.
lst = [1,2,2,3]
s = (lst)
s = (lst)
Use the constructor that creates unique-element collection.
Answer: set
Explanation: set(lst) converts the list to a set, removing duplicate values.
Explanation: set(lst) converts the list to a set, removing duplicate values.
Q35. Which method adds a single element to a set?
One element vs iterable of elements.
Answer: add()
Explanation: set.add(x) inserts a single element; update() accepts an iterable to add multiple elements.
Explanation: set.add(x) inserts a single element; update() accepts an iterable to add multiple elements.
Q36. Which operation gives elements common to both sets A and B?
Think of overlap between sets.
Answer: intersection
Explanation: A & B or A.intersection(B) returns elements present in both sets.
Explanation: A & B or A.intersection(B) returns elements present in both sets.
Q37. Create a dictionary with key "name" and value "Alice".
d = {: "Alice"}
Keys are strings and must be quoted.
Answer: "name"
Explanation: Dictionary keys that are strings must be quoted: {"name": "Alice"}.
Explanation: Dictionary keys that are strings must be quoted: {"name": "Alice"}.
Q38. Safely get value for key 'age' with default 0.
d = {"name":"A"}
age = d.( 'age', 0 )
age = d.( 'age', 0 )
Method returns default if key missing.
Answer: get
Explanation: d.get('age', 0) returns 0 if 'age' is not present, avoiding KeyError.
Explanation: d.get('age', 0) returns 0 if 'age' is not present, avoiding KeyError.
Q39. Update dictionary d with key 'age' = 25 using a method.
d = {"name":"A"}
d.({"age":25})
d.({"age":25})
Method merges another dict into this one.
Answer: update
Explanation: d.update({"age":25}) adds or updates key-value pairs in the dictionary.
Explanation: d.update({"age":25}) adds or updates key-value pairs in the dictionary.
Q40. Remove key 'name' and return its value.
d = {"name":"A", "age":20}
val = d.( "name" )
val = d.( "name" )
Method removes key and returns its value.
Answer: pop
Explanation: d.pop('name') removes the key and returns its associated value.
Explanation: d.pop('name') removes the key and returns its associated value.
Q41. Remove duplicates from list while preserving order.
lst = [1,2,2,3,1]
seen = set()
res = [x for x in lst if (x not in seen and (seen.add(x) or True))]
# The expression uses seen.add(x) to track seen items
seen = set()
res = [x for x in lst if (x not in seen and (seen.add(x) or True))]
# The expression uses seen.add(x) to track seen items
Use a set to track seen items inside a comprehension.
Answer: Use comprehension with seen.add(x) trick
Explanation: The comprehension keeps first occurrences and preserves order by checking membership in a set and adding when first seen.
Explanation: The comprehension keeps first occurrences and preserves order by checking membership in a set and adding when first seen.
Q42. Which list method removes an element by value?
One removes by value, another by index.
Answer: remove()
Explanation: list.remove(value) deletes the first matching value; pop() removes by index and returns it.
Explanation: list.remove(value) deletes the first matching value; pop() removes by index and returns it.
Q43. Which method splits a string into words?
Method returns a list of substrings.
Answer: split()
Explanation: s.split() splits on whitespace by default and returns a list of words.
Explanation: s.split() splits on whitespace by default and returns a list of words.
Q44. Fill in to count character frequencies using dict.get.
s = "aba"
freq = {}
for ch in s:
freq[ch] = freq.(ch, 0) + 1
freq = {}
for ch in s:
freq[ch] = freq.(ch, 0) + 1
Use a safe dictionary access method with default value.
Answer: get
Explanation: freq.get(ch, 0) returns current count or 0 if missing, then adds 1.
Explanation: freq.get(ch, 0) returns current count or 0 if missing, then adds 1.
Q45. Access the city of user 'u1' from nested dict users = {'u1':{'name':'A','city':'X'}}
users = {'u1':{'name':'A','city':'X'}}
city = users[]['city']
city = users[]['city']
Use the outer key as a string inside brackets.
Answer: "u1"
Explanation: users["u1"] returns the inner dict; ['city'] accesses the city value.
Explanation: users["u1"] returns the inner dict; ['city'] accesses the city value.
0 Comments