Loading samples...
Loading samples...
py-sample-10
Python's collection data structures
Which creates a single-item tuple?
Are tuples mutable?
Which creates an empty set?
What is {1,2,2,3}?
Which dict method returns all keys?
What is d.get('x', 0)?
Which set operation gives common elements?
Can tuple be dict key?
What is frozenset()?
Which removes and returns arbitrary element?
Differentiate between tuple and list with 3 key differences.
[3 marks]Explain tuple packing and unpacking with examples.
[3 marks]What are set operations? Explain union, intersection, difference.
[3 marks]Explain dictionary comprehension with an example.
[3 marks]When to use set vs list? Explain with use cases.
[3 marks]Explain defaultdict and Counter from collections module.
[3 marks]What is dictionary view object? Why are they dynamic?
[3 marks]Explain setdefault() method with practical example.
[3 marks]Can a list be a dictionary key? Why or why not?
[3 marks]Explain how to merge two dictionaries (Python 3.9+ vs older).
[3 marks]Write a program to find symmetric difference of two sets.
[5 marks]Create a word frequency counter using dictionary.
[5 marks]Write a program to invert a dictionary (swap keys and values).
[5 marks]Create a function that returns unique elements preserving order using set for tracking.
[5 marks]Write a program to group words by their first letter using defaultdict.
[5 marks]Create a tuple unpacking program that swaps values of multiple variables.
[5 marks]Predict output: t = (1, 2, 3) print(t[0]) a, b, c = t print(b)
[2 marks]Explanation:t[0] is first element 1. Unpacking assigns a=1, b=2, c=3, so b is 2.
Find error: t = (1, 2, 3) t[0] = 4 print(t)
[2 marks]Explanation:Tuples are immutable. Cannot modify elements after creation.
Predict output: s1 = {1, 2, 3} s2 = {2, 3, 4} print(s1 | s2) print(s1 & s2)
[2 marks]Explanation:| is union (all unique). & is intersection (common elements only).
Find error: d = {'a': 1, 'b': 2} print(d['c'])
[2 marks]Explanation:Key 'c' doesn't exist. Use d.get('c') to avoid error, or check first.
Predict output: d = {'a': 1, 'b': 2} print(list(d.keys())) print(list(d.values()))
[2 marks]Explanation:keys() and values() return view objects. Converting to list gives the keys and values.
Find error: my_set = {[1, 2], [3, 4]}
[2 marks]Explanation:Lists are mutable/unhashable and cannot be set elements. Use tuples instead.
Predict output: t = (1, 2, [3, 4]) t[2].append(5) print(t)
[3 marks]Explanation:Tuple is immutable but mutable objects inside can be modified. The list inside is modified.
Find error: d = {'a': 1, 'b': 2} for k, v in d.items(): if v == 1: del d[k]
[3 marks]Explanation:Cannot modify dict while iterating. Create list of keys to delete first, or use dict comprehension.