Python

Python interview questions and answers

Python interviews in India split three ways: backend with Django or FastAPI, data work with pandas and SQL, and automation or testing. The language questions overlap almost completely. The second half of the interview does not, so find out which track you are being interviewed for before you prepare.

Asked of: Backend, data analyst, data engineer, automation and QA roles. Every answer below describes what a strong response covers rather than a script to memorise, because the follow-up question is where these rounds are actually decided.

What gets tested
01

Data types and mutability

Lists against tuples, what can be a dictionary key, and the copying behaviour that surprises people in nested structures.

02

Functions, scope and decorators

Default arguments, closures, and decorators. Most hook-style follow-ups here are closure questions wearing a costume.

03

OOP and the object model

Classes, dunder methods, inheritance and the method resolution order.

04

Concurrency

The GIL, and choosing between threads, processes and asyncio for a workload you can describe.

05

The track you are hired for

pandas and SQL for data roles, Django or FastAPI and the ORM for backend, pytest and Selenium or Playwright for automation.

For freshers

Python interview questions for freshers

What is the difference between a list and a tuple?

A list is mutable, a tuple is not, and that is why a tuple can be a dictionary key or a set member while a list cannot. Add the practical reason you would choose a tuple: to signal that this collection is not meant to change.

What is wrong with def add(item, target=[])?

The default list is created once when the function is defined, so every call without an argument shares and grows the same list. Use None as the default and build the list inside. This is the most asked Python gotcha in the country.

is or ==?

is compares identity, == compares value. Small integers and short strings are cached, so is can appear to work and then fail on larger values. Use is for None and == for everything else.

Shallow copy or deep copy?

A shallow copy duplicates the outer container and shares the objects inside, so mutating a nested list shows up in both. copy.deepcopy walks the whole structure. Say when the cost of a deep copy is worth paying.

List comprehension or generator expression?

A comprehension builds the whole list in memory, a generator produces items one at a time and can only be consumed once. For a large file or query result, the generator is the one that keeps your process alive.

What do *args and **kwargs do?

They collect extra positional and keyword arguments. Show one real use, usually a wrapper passing arguments through to the function it decorates, rather than reciting the definition.

How does a dictionary work, and what can be a key?

A hash table, so keys must be hashable, which in practice means immutable. Note that dictionaries have kept insertion order since 3.7, because interviewers still ask as if they do not.

Explain try, except, else and finally.

else runs when no exception was raised, finally runs either way and is where cleanup belongs. Say why a bare except is a bad habit: it swallows KeyboardInterrupt and hides the bug you were trying to find.

What is a decorator?

A function that takes a function and returns a replacement, applied with the @ syntax. Mention functools.wraps and why it matters: without it the wrapped function loses its name and docstring, which breaks logs and tooling.

How does a for loop work internally?

It calls iter() to get an iterator, then next() until StopIteration. Being able to say that, and to write a small class with __iter__ and __next__, puts you ahead of most fresher candidates.

For experienced candidates

Python interview questions for experienced candidates

What is the GIL, and how do you work around it?

One thread executes Python bytecode at a time in the standard interpreter. I/O releases it, so threads still help for network and disk work, and CPU-bound work needs processes. Note that free-threaded builds exist now, but do not claim production experience with them unless you have it.

Threads, processes or asyncio for this workload?

Give the decision rule rather than a preference: many concurrent I/O waits go to asyncio, CPU-bound work goes to multiprocessing, and threads suit blocking I/O in a library that has no async version. Then say which you have actually shipped.

Your async endpoint is slow under load. What would you check?

A blocking call inside a coroutine, which stalls the entire event loop for everyone. That includes a synchronous database driver, requests, and heavy CPU work. Move it to a thread or process executor, or use the async client.

How would you process a file too large for memory?

Stream it with a generator and process line by line or in chunks, keeping only what you need. If the interviewer is from a data team, connect it to chunked reads in pandas or to a database-side aggregation instead.

What is a context manager, and when have you written one?

__enter__ and __exit__, or contextlib.contextmanager, guaranteeing cleanup even when an exception is raised. A real example beats the definition: a database transaction, a temporary directory, a timer around a block.

Explain the method resolution order.

C3 linearisation decides which parent class wins for an attribute, and super() follows that order rather than jumping to a fixed parent. This is asked when the team has a real inheritance hierarchy, so ask whether theirs is deep.

dataclass, NamedTuple, dict or a Pydantic model?

A dict when the shape is genuinely dynamic, a NamedTuple when it is small and immutable, a dataclass for internal structures with behaviour, and Pydantic when the data arrives from outside and needs validating. Pick by where the data comes from.

How does Python manage memory, and what causes a leak?

Reference counting plus a cycle collector. A leak in Python almost always means something is still referenced: a growing module-level cache, a closure holding a large object, or a list you append to and never clear.

What do you mock in a test, and what do you leave alone?

Mock the boundary you do not own, such as a payment gateway or an email API. Do not mock the thing under test, and be careful mocking your own database, because that is where tests start passing while production breaks.

Are type hints worth it if they are not enforced at runtime?

Yes, because the checker runs in CI and in your editor, where it catches the wrong-shape argument before review does. Be straight that they document intent and do not validate input, which is what Pydantic is for.

What candidates get wrong here
  • Ask early whether the role is backend, data or automation. Preparing pandas for a Django interview wastes the half of your preparation that mattered.
  • Say which Python version you work in. Typing and dataclass answers have moved a lot between 3.8 and 3.12.
  • "I would look up the syntax" is a fine answer about a library and a poor one about a concept. Know which of the two you are being asked.
  • Do not put asyncio on your resume if the only async you have written is a FastAPI route calling a blocking library. That is the first thing a senior interviewer probes.

Answer them out loud before someone asks

Reading a question and answering it under a follow-up are different skills. Practise these against an AI interviewer that pushes back and scores your answer, or check your resume first with the free ATS resume checker.

Start My Free Mock Interview
Other skills