Python Interview Questions and Answers
110 Python interview questions across 12 topics: Python Fundamentals, Data Model & Object Protocols, Functions & Functional Patterns, Type Hints & Static Analysis, Exceptions & Resource Safety, Concurrency, Threads & Asyncio, Testing & Quality, Packaging & Tooling, Performance & Profiling, Web, APIs & Data Integration, Web Frameworks & ORMs, and Data Processing & Parallelism. They cover Junior, Mid-level, and Senior levels, and every question has a short answer to give in the interview, a deeper explanation, common mistakes, and links to official documentation with a last-reviewed date.
Topics
Python Fundamentals
10 questions
Data Model & Object Protocols
10 questions
Functions & Functional Patterns
10 questions
Type Hints & Static Analysis
10 questions
Exceptions & Resource Safety
10 questions
Concurrency, Threads & Asyncio
10 questions
Testing & Quality
10 questions
Packaging & Tooling
10 questions
Performance & Profiling
10 questions
Web, APIs & Data Integration
10 questions
Web Frameworks & ORMs
6 questions
Data Processing & Parallelism
4 questions
Python Fundamentals Interview Questions (Python)
- What happens to Python code from the moment you run it?
- What is the difference between mutable and immutable objects in Python?
- When should you choose a list, tuple, set or dict?
- What are list and dict comprehensions for, and when do they hurt readability?
- What is the difference between is and == in Python?
- How does Python decide whether a value is truthy or falsy?
- What are unpacking and starred expressions good for?
- What does slicing do on a list or str, and does it produce a copy?
- What is the difference between a module and a package, and how does import work?
- Why do we use if __name__ == "__main__"?
Data Model & Object Protocols Interview Questions (Python)
- What is the relationship between __eq__ and __hash__ in Python?
- What is the difference between a shallow copy and a deep copy?
- What is the difference between an iterable and an iterator?
- How do generators and yield reduce memory usage?
- Which protocol makes an object usable in a for loop?
- When should you use a property instead of explicit getter and setter methods?
- What does the @dataclass decorator give you?
- What is __slots__ for, and what trade-offs come with it?
- How do MRO and super() work with multiple inheritance?
- Which protocol does a context manager implement?
Functions & Functional Patterns Interview Questions (Python)
- What is the difference between positional-only and keyword-only parameters?
- Why is a list as a default parameter value a problem?
- How do *args and **kwargs work?
- What is a closure, and what is the late-binding problem inside loops?
- How do you write a decorator that preserves the original function's metadata?
- Explain the LEGB rule for name resolution.
- When should you use lambda and when def?
- What is functools.partial for?
- What are the limits of recursion in Python, and when should you choose a loop?
- What does it mean that functions in Python are first-class objects?
Type Hints & Static Analysis Interview Questions (Python)
- Are type hints enforced at runtime in Python?
- How do you express a value that may be one of two types or None?
- What is a Protocol in typing?
- What is the role of TypeVar and Generic in Python?
- When should you use TypedDict with JSON data?
- What are Final and Literal used for?
- When do you use @overload?
- What is the difference between Any and object in type hints?
- What are TypeGuard and TypeIs for in type narrowing?
- How do you use typing to improve module boundaries without overdoing it?
Exceptions & Resource Safety Interview Questions (Python)
- How do you choose the right exception and deal with the exception hierarchy?
- What is the difference between except, else and finally in a try statement?
- When should you create a custom exception?
- What is the difference between the EAFP and LBYL styles?
- What is raise ... from for?
- How does contextlib help with resource management?
- How do you log an exception without losing the traceback?
- How do you design timeouts and retries without multiplying the damage?
- When do you need ExceptionGroup and except*?
- Where should validation of data coming from outside the system be placed?
Concurrency, Threads & Asyncio Interview Questions (Python)
- What is the effect of the GIL in CPython, and what changed with the free-threaded build?
- When do you choose threading and when multiprocessing?
- What is an event loop and what is the role of a coroutine in asyncio?
- What is the difference between a coroutine, a Task and a Future in asyncio?
- What is the practical difference between asyncio.gather and TaskGroup?
- How do you handle cancellation in asyncio?
- How do you run a blocking function inside asyncio?
- What is the role of asyncio.Queue in the producer-consumer pattern?
- When do you need a Lock in concurrent code?
- What is the concurrent.futures module for?
Testing & Quality Interview Questions (Python)
- What is the difference between unit tests and integration tests?
- What is the role of fixtures in pytest?
- How do you test many cases without repeating code?
- How do you use mock and patch correctly?
- How do you test asynchronous (async) code?
- How do you verify that a function raises the correct exception?
- How do you keep time and randomness from making tests flaky?
- How do you test HTTP code without relying on a real network?
- Does a high coverage percentage mean the code is well tested?
- What is a contract test and when is it useful?
Packaging & Tooling Interview Questions (Python)
- Why do we use virtual environments (venv)?
- What is the role of the pyproject.toml file?
- What is the difference between requirements and constraints files?
- What is the benefit of the src layout for organizing packages?
- What does an editable install mean?
- What is the difference between a wheel and a source distribution (sdist)?
- How do you expose a CLI command from a Python package?
- How do you decide which Python versions your project supports?
- How do you configure logging in a library or service without annoying its users?
- How do you separate configuration from secrets in a Python application?
Performance & Profiling Interview Questions (Python)
- Why should you measure before optimizing performance?
- What is the role of cProfile in performance analysis?
- When do you use collections.deque instead of list?
- When do generators improve memory use and when do they not improve time?
- How do you use functools.lru_cache safely?
- Why is str.join usually preferred for assembling strings?
- How do you reduce the cost of inserting many rows into SQLite?
- How do you deal with heavy object creation in a hot loop?
- What does tracemalloc measure and how do you use it to find memory leaks?
- How does the choice of data structure change a program's performance?
Web, APIs & Data Integration Interview Questions (Python)
- How do you handle JSON coming from an API?
- Why use urllib.parse instead of building URLs by concatenating strings?
- What are the basic rules for using an HTTP client in Python?
- How do you prevent SQL injection when using sqlite3?
- How do you manage transactions in sqlite3?
- How do you read and write CSV files correctly?
- What is the problem with naive datetimes in services?
- How do you design pagination in an API?
- How do you keep retries from creating duplicate writes?
- How do you separate the API schema from the domain model?
Web Frameworks & ORMs Interview Questions (Python)
- What is the N+1 problem in the Django ORM and how do you solve it with select_related and prefetch_related?
- How does a request travel through Django from the middleware to the view?
- How does the Depends system work in FastAPI, and when do you use async def or def for routes?
- How does Pydantic v2 validate data, and what is the difference between lax and strict mode?
- How does the Session work in SQLAlchemy 2.0 and why should its scope be limited to one request?
- What is the difference between WSGI and ASGI, and how does Flask manage the request context?
Data Processing & Parallelism Interview Questions (Python)
- Why are vectorized operations in NumPy faster than Python loops, and what is broadcasting?
- What is the chained assignment problem in pandas and how did Copy-on-Write change it?
- How do you reduce memory use and speed up analysis in pandas?
- How do you choose between multiprocessing, free-threaded Python, and subinterpreters for parallel data processing?