HomeBlogPython Interview Questions and Answers (2026)

IT JobSupport blog

Python Interview Questions and Answers (2026)

A working reference for Python developer and data-engineering interviews. Answers are deliberately short. To rehearse them out loud with feedback, see interview preparation.

Language and data model

What is the difference between a list and a tuple?

Lists are mutable and used for homogeneous, variable-length sequences. Tuples are immutable, can be dictionary keys or set members, and are conventionally used for fixed heterogeneous records. Tuples are also slightly lighter and faster to construct.

Explain mutable default arguments.

A default argument is evaluated once, at function definition. def f(x, items=[]) shares one list across all calls that use the default, which causes surprising accumulation. The fix is def f(x, items=None): items = items or [].

What is the difference between is and ==?

== calls __eq__ and compares value. is checks identity (same object in memory). Use is only for singletons like None. Relying on is for small ints or short strings works by accident due to interning and will break.

What are *args and **kwargs?

*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict. On the call side, * and ** unpack an iterable or mapping into arguments.

How does Python's garbage collection work?

Primarily reference counting — an object is freed when its count hits zero. A cyclic garbage collector runs periodically to clean up reference cycles that counting alone cannot. You rarely manage this manually; avoid holding references longer than needed.

What is a context manager?

An object with __enter__ and __exit__ used via with, guaranteeing setup and cleanup even on exceptions — file handles, locks, database transactions. contextlib.contextmanager lets you write one as a generator.

Functions, iterators, generators

What is the difference between a generator and a list comprehension?

A list comprehension builds the whole list in memory. A generator expression yields items lazily, one at a time, using constant memory — essential for large or infinite streams. You can only iterate a generator once.

Explain decorators.

A decorator is a callable that takes a function and returns a replacement, used to add behaviour (timing, caching, auth, retries) without changing the function body. @wraps from functools preserves the wrapped function's metadata.

What does yield do?

It suspends a function, returning a value to the caller and preserving local state, so execution resumes after the yield on the next next(). It turns a function into a generator. yield from delegates to a sub-generator.

What is a closure?

A nested function that captures variables from its enclosing scope and keeps them alive after the outer function returns. Use nonlocal to rebind a captured variable.

Concurrency

What is the GIL and when does it matter?

The Global Interpreter Lock allows only one thread to execute Python bytecode at a time in CPython. It means threads do not speed up CPU-bound pure-Python work. It does not block I/O — threads still help for I/O-bound work — and C extensions and NumPy release it. For CPU parallelism use multiprocessing or a native library.

threading vs multiprocessing vs asyncio — when do you use each?

asyncio for high-concurrency I/O in a single thread with async libraries. threading for I/O concurrency with blocking libraries or simpler code. multiprocessing for CPU-bound work that needs real parallelism, at the cost of process overhead and serialising data between processes.

What is async/await?

async def defines a coroutine; await suspends it until an awaitable completes, letting the event loop run other coroutines meanwhile. It gives you cooperative concurrency — nothing is preempted, so a blocking call inside a coroutine stalls the whole loop.

Web and data

In Django, what is the N+1 query problem and how do you fix it?

Iterating a queryset and accessing a related object per row triggers one query per row. Use select_related (SQL join, for foreign keys / one-to-one) and prefetch_related (separate query + join in Python, for many-to-many / reverse FK).

What is the difference between select_related and prefetch_related?

select_related does a single SQL JOIN and is for single-valued relationships. prefetch_related runs a second query and joins in Python, for multi-valued relationships. Using the wrong one either does nothing useful or creates a huge join.

How do you keep a pandas transformation fast on a large DataFrame?

Vectorise — use column operations and built-in methods instead of apply or Python loops. Choose efficient dtypes (categorical, smaller numeric types), filter early, avoid repeated copies, and for data that does not fit in memory move to chunking, Polars or a database.

How would you design an ETL pipeline that must be re-runnable?

Make each step idempotent (writing the same partition twice produces the same result), separate extract / transform / load so a failure is resumable, use a watermark or run-id for incremental loads, validate row counts and schema between stages, and make failures loud.

How do you structure tests for a Python project?

pytest with fixtures for setup, small focused unit tests for logic, a smaller set of integration tests against real dependencies (via Testcontainers or a test database), factories instead of fixtures-with-lots-of-data, and coverage as a guide rather than a target.

Practising these

If you have a Python interview soon, book a mock interview — the gap is usually not knowledge, it is explaining it clearly under time pressure. If you are already in a Python or data role and the day-to-day work is the problem, see Python job support.

Need help on the job, not just the theory?

Senior engineers help you deliver real tasks over screen-share — Java, Python, AWS, DevOps and JavaScript.

← All articles