All interview questions

Python Interview Questions

Practice predicting what Python will do before running a snippet, then explain which guarantees come from the language and which depend on the runtime.

Try answering aloud before opening each explanation.

Try the Python practice demo

Foundations

Does assigning one list variable to another copy the list?

No. Assignment binds the new name to the same object. Mutating through either reference affects both; rebinding one name does not.

When should you use is rather than ==?

Is checks identity, as in value is None. Equality compares values according to the object's equality implementation. Do not rely on identity for equal numbers or strings.

Why does a list used as a parameter default retain earlier calls' data?

The default is evaluated once when the function is defined. Later calls without that argument reuse the object. Use a sentinel such as None and create a fresh list within the call.

What makes a value eligible as a dictionary key?

A key must be hashable: its hash must remain stable and equal keys must have equal hashes. A tuple is hashable only when its elements are hashable.

What is the difference between an iterable and an iterator?

An iterable can supply an iterator; an iterator itself remembers position and yields items with next until exhaustion. Not every iterable can be traversed a second time.

Applied

Why does copying a nested dictionary with dict() not isolate nested changes?

It copies the outer mapping but retains references to the original values. Copy the nested objects you intend to mutate; use a deep copy only when duplicating the full object graph is appropriate.

How does a generator help process a file larger than memory?

It can yield one item at a time instead of retaining the whole file. Keep the file open for the duration of iteration, and avoid downstream operations such as list() that re-materialize all records.

How can a context manager preserve cleanup when parsing throws?

The with statement invokes exit handling once entry succeeds, whether the block completes or raises. A context manager can close a file there; suppressing an exception should be a separate deliberate decision.

Do type annotations validate values passed by a caller?

Not by themselves. A static checker can find inconsistent calls in code, but an untrusted JSON payload needs runtime validation. Return a precisely typed value after checking it.

Why do callbacks created in a loop sometimes all use the last loop value?

A closure resolves the same outer variable when called, after the loop may have changed it. Bind the current value in a factory or by a per-function default argument.

Production scenarios

When would you use processes rather than threads for Python work?

In conventional GIL-enabled CPython, processes can run CPU-bound Python bytecode in parallel, at the cost of startup and data transfer. Threads often work well for blocking I/O; check the runtime and measure the actual workload.

Why does an async handler stall its neighbors during a blocking call?

The event loop thread cannot run other tasks until the call returns. Use an asynchronous library or offload blocking I/O to a bounded executor instead of assuming async def makes synchronous code non-blocking.

How do you diagnose slow membership checks over a growing list?

Profile first. List membership scans elements; a set is usually faster for repeated lookups of hashable values. Account for memory, construction cost and whether duplicates or ordering matter.

Why should you catch a specific exception when reading a configuration file?

A missing file may justify a documented default; malformed content or denied access should not silently take the same path. Catch the expected failure near the operation and preserve useful context for other errors.

How do you stop one large input from exhausting memory in a worker?

Set explicit size limits and process records in bounded batches or streams. Measure retained objects and resident memory under representative load so buffers and queued work are included.

Further reading

Official documentation

Related question sets