Q1
Why does def add_item(item, bucket=[]): bucket.append(item) retain items across separate function calls?
Default argument values in Python are evaluated ONCE when def executes at import/definition time, not on each function call. Mutating bucket mutates that single shared list object. Always default to bucket=None and initialize [] inside the function body.
Q2
Why does a = [[]]*3; a[0].append(1) modify all three sublists, while [[] for _ in range(3)] does not?
List multiplication [[]] * 3 copies the *reference* to the exact same inner list three times. A list comprehension evaluates [] three separate times, creating three distinct list objects in memory.
Q3
What is Python's late-binding closure trap inside funcs = [lambda: i for i in range(3)]?
All three lambdas capture the loop variable i by reference rather than its value during each iteration. When called after the loop finishes, every lambda resolves i to 2. Bind eagerly using default arguments: lambda i=i: i.
Q4
Why does is return True for 256 is 256 and False for 257 is 257 in interactive Python shells?
CPython interns small integers from -5 to 256 at startup (is checks object memory identity, whereas == checks value equality). Always use == for values and reserve is strictly for None, True, False, or sentinel objects.
Q5
What happens if you mutate a dictionary's keys while iterating over for k in d: vs for k in list(d):?
Adding or deleting keys during iteration raises RuntimeError: dictionary changed size during iteration because CPython tracks the dict version/size counter. Iterating over list(d) snapshots keys safely beforehand.
Q6
Why does += behave differently on a tuple containing a list: t = ([1],); t[0] += [2]?
The expression mutates the inner list (__iadd__) successfully, THEN attempts to rebind t[0] = ... back into the immutable tuple—raising TypeError: 'tuple' object does not support item assignment EVEN THOUGH t[0] now actually contains [1, 2]!
Q7
Why can catching bare except: break Ctrl+C (KeyboardInterrupt) and sys.exit()?
Both KeyboardInterrupt and SystemExit inherit from BaseException, NOT Exception. A bare except: swallows termination signals and locks CLI scripts; always use except Exception as exc:.
Q8
How does yield save memory compared to returning a list when parsing a 10 GB CSV file?
A generator function yields one record at a time without materializing all rows in RAM—keeping memory usage O(1) while chaining transformations lazily through pipelines.
Q9
Why does copy.copy() fail to isolate nested lists inside dictionaries while copy.deepcopy() succeeds?
copy.copy() duplicates only the outer container, keeping shared references to nested mutable values. deepcopy() recursively clones every nested mutable object while maintaining a memo dict to handle circular references.
Q10
What does else on a Python for or while loop actually mean?
A loop's else clause runs ONLY if the loop completed normally without hitting a break statement—ideal for search loops without needing an awkward found = False boolean flag.