interview questions
Python interview questions and answers for freshers
The Python questions that come up repeatedly in entry-level interviews, grouped by what is really being tested — with notes on the follow-up that usually decides the outcome.
By the Samyak faculty team · Published · 10 min read
Entry-level Python interviews are more predictable than they feel. A small set of concepts comes up repeatedly, and most questions are probing one of four things: whether you understand references, whether you understand scope, whether you have written real code, and whether you can reason when you do not know.
Grouped that way, preparation gets much easier.
References and mutability — the largest source of questions
What is the difference between a list and a tuple?
A list is mutable, a tuple is not. Tuples can therefore be dictionary keys and are marginally faster to create.
The follow-up that matters: can a tuple ever change? Yes, in a sense — a tuple containing a list is itself immutable, but the list inside it can be modified. Volunteering this shows you understand that immutability applies to the binding, not the whole object graph.
What happens when you pass a list to a function and modify it?
The change is visible to the caller, because the function receives a reference to the same object rather than a copy.
Interviewers ask this constantly, usually as a code snippet with a predicted output. Say “Python passes object references by value” rather than “pass by reference” or “pass by value” — both of those are half-truths and the precise phrasing signals you have actually thought about it.
Why is a mutable default argument dangerous?
Because the default is evaluated once, when the function is defined, not each
time it is called. So def add(item, target=[]) shares one list across every
call that omits target.
The fix is target=None with if target is None: target = [] inside. This is a
favourite question because it separates people who have been bitten by it from
people who have only read about it.
What is the difference between a shallow and a deep copy?
A shallow copy duplicates the outer container but shares the nested objects. A
deep copy duplicates everything recursively. copy.copy versus copy.deepcopy.
Expect a follow-up about when a shallow copy is the bug — usually a list of lists where modifying one row unexpectedly changes another.
What is the difference between is and ==?
== compares values; is compares identity — whether two names point to the
same object. Use is only for None, True and False.
If they mention that a is b sometimes returns True for small integers or
short strings, that is interning, an implementation detail you should never rely
on. Saying so is the right answer.
Scope and functions
Explain the LEGB rule.
Python resolves a name by looking at Local scope, then Enclosing functions, then
Global, then Built-ins. The practical consequence is that assigning to a name
inside a function makes it local for the entire function, which is why reading a
global and then assigning to it raises UnboundLocalError.
What are *args and **kwargs?
*args collects extra positional arguments into a tuple, **kwargs collects
extra keyword arguments into a dictionary. Expect a follow-up on argument order
and on unpacking with * and ** at the call site.
What is a decorator?
A function that takes a function and returns a modified one, usually to add behaviour like logging, timing or access control without editing the original.
Be ready to write a simple one. Mentioning functools.wraps — which preserves
the wrapped function’s name and docstring — is a small detail that reads as
practical experience.
What is a generator and why use one?
A function using yield that produces values lazily instead of building a whole
list in memory. Useful when the sequence is large or infinite, or when you only
need the first few items.
Good follow-up answer: a generator can only be consumed once, which is a common source of confusion when someone iterates it twice and gets nothing the second time.
Data structures and complexity
How is a dictionary implemented, and what does that cost?
A hash table. Average O(1) lookup, insertion and deletion. Keys must be hashable, which is why a list cannot be a key but a tuple can.
When would you use a set over a list?
Membership testing. in on a set is O(1) average; on a list it is O(n). If you
are checking membership inside a loop over a large collection, that difference is
the whole performance story.
What does list comprehension give you over a loop?
Readability first, a modest speed benefit second. Be ready to say when not to use one — nested comprehensions with conditions become unreadable quickly, and a plain loop is the better choice at that point. Interviewers like this answer because it shows judgement rather than preference.
Errors, files and real-world code
What is the difference between an error and an exception?
Syntax errors prevent the code from running at all; exceptions occur during
execution and can be handled. try/except/else/finally — else runs when
no exception was raised, finally always runs.
Why is except: on its own a bad idea?
It catches everything, including KeyboardInterrupt and SystemExit, and hides
bugs you needed to see. Catch the specific exception you expect. This is asked
because it appears constantly in beginner code.
Why use a context manager for files?
with open(...) closes the file even if an exception is raised inside the block.
Expect a follow-up on writing your own context manager, either with a class
implementing __enter__ and __exit__ or with contextlib.contextmanager.
The questions that are not really about Python
These decide more interviews than the technical ones.
Tell me about a project you built.
Have two ready, at two minutes each. Say what problem it solved, one decision you made and why, and one thing you would do differently. The last part is the one candidates skip and interviewers remember — it demonstrates you can evaluate your own work.
You are given code you have never seen. How do you approach it?
Say you would run it first if possible, read the entry point and follow the flow rather than reading top to bottom, and check the tests to learn the intended behaviour. Most of a developer’s job is comprehension, and very few candidates have an articulated method for it.
You do not know the answer. What then?
Say so, then reason out loud toward it. “I have not used that, but based on how X works I would expect…” scores far better than silence or a confident guess. Interviewers are calibrating how you behave when stuck, because that is what most working days involve.