Python Interview Questions With Code and Output (2026)
Eight core Python interview questions with short answers and runnable code samples, grounded in the official Python documentation.
Last updated: 21 September 2026 · By the Asuraa Team
What are the most common Python interview questions?
The most common Python interview questions test core concepts: data types, mutability, functions, comprehensions, object-oriented basics, generators and decorators. DataCamp's Python interview guide (updated 30 April 2026) groups its 41 questions into basic, intermediate and advanced sections. It also adds a section on Python for AI and machine learning.
Below are eight concept questions with short answers. We ran every code sample in Python 3.11, and the outputs shown are the real ones.
What is the difference between a list and a tuple?
A list is mutable and a tuple is not. The Python glossary defines a mutable object as one whose state is allowed to change during the program.
t = (1, 2)
t[0] = 9 # TypeError: 'tuple' object does not support item assignment
d = {(1, 2): "ok"} # a tuple can be a dict key
{[1, 2]: "x"} # TypeError: unhashable type: 'list'
The glossary says an object is hashable if its hash value never changes during its lifetime. Hashability is what lets an object be a dictionary key or a set member.
Why is a mutable default argument dangerous?
Default values are created once, when the function is defined, so they are shared between calls. The Python programming FAQ explains this and recommends using None as the default.
def add_item(item, bucket=[]):
bucket.append(item)
return bucket
print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] (same list reused)
def add_item_ok(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
print(add_item_ok(1)) # [1]
print(add_item_ok(2)) # [2]
Interviewers like this question because it shows whether you understand when Python evaluates things.
What is the difference between a shallow copy and a deep copy?
A shallow copy copies the outer container but shares the inner objects, while a deep copy copies everything. The Python FAQ suggests copy.copy() or copy.deepcopy() for the general case.
import copy
a = [[1, 2], [3]]
b = copy.copy(a)
c = copy.deepcopy(a)
a[0].append(99)
print(a) # [[1, 2, 99], [3]]
print(b) # [[1, 2, 99], [3]] (inner list is shared)
print(c) # [[1, 2], [3]] (fully independent)
What is the difference between == and is?
The operator == compares values, and is checks whether two names point to the same object. Two equal lists can still be different objects.
x = [1, 2]
y = [1, 2]
print(x == y) # True
print(x is y) # False
Use is for singletons such as None, and == for comparing values.
Why do lambdas in a loop all return the same value?
They look up the loop variable when they are called, not when they are defined. The Python FAQ explains that the variable is defined in the outer scope and accessed at call time.
sq = [lambda: i * i for i in range(3)]
print([f() for f in sq]) # [4, 4, 4]
sq = [lambda n=i: n * n for i in range(3)]
print([f() for f in sq]) # [0, 1, 4]
The fix is the FAQ's own: bind the current value as a default argument.
What is a generator, and when would you use one?
A generator produces values one at a time using yield, so it does not build the whole sequence in memory. The glossary lists generators alongside iterables, which return their members one at a time.
def squares(n):
for i in range(n):
yield i * i
g = squares(4)
print(next(g)) # 0
print(list(g)) # [1, 4, 9]
Use one for large or streaming data. After next(g), the remaining values are consumed by list(g), which is a common follow-up.
What is a decorator?
A decorator is a function that returns another function, applied with the @ syntax. The glossary describes it as syntactic sugar: @staticmethod above a function is equivalent to f = staticmethod(f) after it.
import functools
def log_call(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
print("calling", fn.__name__)
return fn(*args, **kwargs)
return wrapper
@log_call
def add(a, b):
return a + b
print(add(2, 3)) # prints "calling add", then 5
The functools.wraps line keeps the original function's name. Mentioning it shows you know the detail.
What is the GIL?
The global interpreter lock is the mechanism CPython uses so that only one thread runs Python bytecode at a time. The glossary adds that the lock is released during I/O and by some extension modules doing heavy work such as hashing.
It also says that as of Python 3.13, the GIL can be disabled with a build configuration. Safe interview answers stick to those documented points: threads help with I/O-bound work, and CPU-bound work needs a different approach such as separate processes, which is our own summary and not a quote.
How should you prepare for Python questions?
Run every example yourself and explain the output aloud. Here is a plan you can follow, and it is our suggestion.
| Days | Focus | Output |
|---|---|---|
| 1-2 | Data types, mutability, copying | Short notes with runnable examples |
| 3-4 | Functions, arguments, scope, closures | Five small functions with tests |
| 5 | Generators, decorators, comprehensions | One example of each |
| 6 | Coding problems and complexity | Three problems explained aloud |
| 7 | Your projects | A two-minute walkthrough of each |
Our guide on how to learn Python for jobs covers the learning side. For the wider process, see technical interview questions and how to prepare.
What do most guides on Python interview questions get wrong?
Most guides give a long list of definitions. These are the gaps.
- They give answers without output. Reading that a tuple is immutable is weaker than seeing the TypeError yourself.
- They stop at definitions. Interviewers follow up with "what would this print?", so practise predicting outputs.
- They skip the why. Knowing why default arguments are shared is more useful than knowing that they are.
- They ignore the role. A data role needs pandas and SQL alongside Python, so pair this with our SQL interview questions and data scientist interview questions.
FAQ
What are the most common Python interview questions?
Common questions cover data types, lists versus tuples, mutability, comprehensions, functions and arguments, object-oriented basics, generators, decorators and copying objects. DataCamp's guide (updated April 2026) also includes newer topics such as async code and LLM use in Python for some roles.
What is the difference between a list and a tuple in Python?
Both hold ordered items, but a list is mutable and a tuple is not. Trying to assign to a tuple item raises a TypeError. Because a tuple of immutable items is hashable, it can be used as a dictionary key, and a list cannot.
What is the GIL in Python?
The Python documentation describes the global interpreter lock as the mechanism CPython uses so that only one thread executes Python bytecode at a time. It is released during I/O. Since Python 3.13, a build option can disable it, according to the same glossary.
Why should you avoid a mutable default argument in Python?
Python creates default values once, when the function is defined, so a default list or dictionary is shared across calls. The Python FAQ recommends using None as the default and creating a new object inside the function. This is a very common interview trap.
How do I prepare for a Python interview as a fresher?
Learn the fundamentals well, run every example yourself, and be ready to explain why the output is what it is. Practise coding problems aloud, and prepare to discuss the Python projects on your resume. Interviewers usually follow a code answer with a why question.
Do Python interviews include coding questions?
Usually yes. DataCamp's guide includes array and optimisation problems alongside concept questions, and HackerRank lists data structures and algorithms among common areas. Expect to write short functions, explain your approach and discuss time and space complexity.
Final thoughts
Python interviews reward understanding over memory. Run each example, predict the output before you press enter, and be ready to explain why.
If you are also weighing openings, our page on Python jobs in India is a good next step.
Related articles
Technical Interview Questions: How to Prepare (India, 2026)
What technical interview questions test, how to prepare from the job description, and how to think aloud, with a worked example.
Software Engineer Interview Questions With Worked Answers
Software engineer interview questions by round, with worked examples for coding, system design basics and behavioural answers, plus a preparation plan.