The most asked Python interview questions cover five areas: data types, functions, classes, errors, and how Python runs your program. Freshers get definitions and short programs. Experienced developers get questions on memory, concurrency, testing and design. This page has 25 questions, sorted by level, with answers you can say aloud.
How to use these Python interview questions
Read the bold answer first and say it in your own words. Then read the detail, because the follow-up question usually comes from there. For each code example, guess the output before you look.
| Your level | Focus on | What to expect |
|---|---|---|
| Fresher | Q1 to Q12 | Basics, output questions, small programs |
| 1 to 4 years | Q1 to Q19 | Decorators, generators, testing |
| 5 years and above | Q13 to Q25 | Concurrency, memory, design, live problems |
Data roles also ask about pandas. Use the separate Python for analysts questions for that.
Python interview questions for freshers
Q1. What is Python, and why is it called interpreted?
30-second answer: Python is a general-purpose language with dynamic typing, which means a variable has no fixed type. The standard version, CPython, turns your code into bytecode and then runs that bytecode. You do not build a machine-code file yourself.
Bytecode for imported modules is saved in the __pycache__ folder, so the next import is faster.
Interview note: Do not say "Python has no compilation". It has a compile step. Python runs it for you.
Q2. What is the difference between a list and a tuple?
30-second answer: Both keep items in order. A list can change after you create it, and a tuple cannot. Use a list for items that grow, and a tuple for a fixed record such as a map location.
A tuple can be a dictionary key, but only if everything inside it is hashable. The list vs tuple comparison covers memory and speed.
Q3. Which Python types are mutable?
30-second answer: Mutable means the object can change. Lists, dictionaries, sets and bytearrays are mutable. Numbers, strings, tuples, frozensets and bytes are immutable.
a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3]
Both names point to one list, so the change shows through both.
Q4. What is the difference between is and ==?
30-second answer: == checks whether two values are equal. is checks whether two names point to the same object. Use is for None, and == for normal values.
x = [1, 2]
y = [1, 2]
print(x == y) # True
print(x is y) # False
Common mistake: Comparing numbers with
is. It may seem to work for small whole numbers because CPython reuses them. The bug then stays hidden.
Q5. How does a dictionary work?
30-second answer: A dictionary stores pairs of keys and values in a hash table. Python hashes the key to find its place. Lookup, insert and delete take O(1) time on average, and keys must be hashable.
Since Python 3.7, the language promises that a dictionary keeps insertion order. Practise the methods in the Python dictionary guide.
Q6. What is a list comprehension?
30-second answer: It is a short way to build a list by looping over something, with an optional condition. It replaces a loop that only appends items.
squares = [n * n for n in range(6) if n % 2 == 0]
print(squares) # [0, 4, 16]
Use a normal loop when the work needs several steps. See more patterns in Python list comprehension.
Q7. What are *args and **kwargs?
30-second answer: *args collects extra positional arguments into a tuple. **kwargs collects extra named arguments into a dictionary. They let a function accept a flexible number of inputs.
def show(*args, **kwargs):
print(args, kwargs)
show(1, 2, city="Hyderabad")
# (1, 2) {'city': 'Hyderabad'}
Q8. What is wrong with a mutable default argument?
30-second answer: Python creates the default value once, when it runs the def line. So later calls share the same default list. Use None and create the list inside the function.
def add(item, box=None):
if box is None:
box = []
box.append(item)
return box
If you wrote box=[], calling add(1) and then add(2) would give [1, 2] the second time.
Q9. What is the difference between a shallow and a deep copy?
30-second answer: A shallow copy makes a new outer object but shares the inner objects. A deep copy also copies the inner objects.
import copy
a = [[1], [2]]
s = copy.copy(a)
d = copy.deepcopy(a)
a[0].append(9)
print(s[0], d[0]) # [1, 9] [1]
The shallow copy changed because it shares the inner list with a.
Q10. How do exceptions work?
30-second answer: Put risky code in try. except handles an error, else runs when there was no error, and finally runs either way. Catch the exact error you expect.
try:
n = int("12a")
except ValueError:
print("Not a number")
else:
print(n)
finally:
print("Done")
# Not a number
# Done
Q11. What are self and __init__?
30-second answer: self is the object the method is working on. __init__ sets up a new object. Another method, __new__, creates the object just before that.
self is a naming habit, not a keyword. For classes and inheritance, see the OOPs interview questions.
Q12. Find the first character that does not repeat.
30-second answer: Count every character. Then go through the string again and return the first one with a count of 1. This takes O(n) time.
from collections import Counter
def first_unique(text):
counts = Counter(text)
for ch in text:
if counts[ch] == 1:
return ch
return None
print(first_unique("swiss")) # w
State the time and space cost without waiting to be asked. Then try more Python practice problems.
Questions for one to four years of experience
Q13. What is a decorator?
30-second answer: A decorator takes a function and returns another function, usually with something added. Logging, timing and permission checks are common uses.
from functools import wraps
def log(fn):
@wraps(fn)
def inner(*args, **kwargs):
print("calling", fn.__name__)
return fn(*args, **kwargs)
return inner
wraps keeps the original function's name and help text.
Q14. How is a generator different from a list?
30-second answer: A generator makes one value at a time with yield, so it uses little memory. A list stores all its values at once. You can go through a generator only once.
def read_lines(path):
with open(path, encoding="utf-8") as f:
for line in f:
yield line.rstrip("\n")
This reads a large log file one line at a time.
Q15. What is a context manager?
30-second answer: It sets something up and cleans it up later, even if an error happens. You use it with with. A class needs __enter__ and __exit__, or you can use contextlib.contextmanager.
Files, locks and database transactions are good examples. with open(...) closes the file even when the code inside fails.
Q16. What do @staticmethod, @classmethod and @property do?
30-second answer: A static method gets neither the object nor the class. A class method gets the class as cls, which is useful for another way to create objects. A property lets you use a method like an attribute.
class User:
def __init__(self, name):
self.name = name
@classmethod
def from_email(cls, email):
return cls(email.split("@")[0])
print(User.from_email("ravi@site.com").name) # ravi
Q17. What is the method resolution order?
30-second answer: It is the order in which Python searches classes for a method. You can see it with ClassName.mro(). super() calls the next class in this order.
With multiple inheritance, the next class is not always the direct parent. Python builds the order using a rule called C3 linearization.
Q18. What are a module, a package and a virtual environment?
30-second answer: A module is usually one Python file. A package groups modules, usually in a folder. A virtual environment keeps one project's libraries apart from other projects.
Save exact library versions in a lock file or requirements file. Then every machine can install the same set.
Q19. How do you test Python code?
30-second answer: I write small pytest tests for normal, edge and error cases. Fixtures prepare data. I replace network calls with fakes only where my code meets the outside system.
import pytest
def divide(a, b):
if b == 0:
raise ValueError("b cannot be zero")
return a / b
def test_divide():
assert divide(6, 3) == 2
def test_zero():
with pytest.raises(ValueError):
divide(6, 0)
Testing the error case as well as the normal one makes your answer stronger.
Python interview questions and answers for experienced developers
Python interview questions for 5 years experience test your choices. State the problem, what you chose, and what you gave up.
Q20. What is the GIL?
30-second answer: GIL means Global Interpreter Lock. In the standard CPython build, it lets only one thread run Python bytecode at a time. Threads still help when code waits for files or networks.
Python 3.13 added an experimental build without the GIL. Python 3.14 made that separate build officially supported. The normal build still has the GIL.
Q21. When would you use threads, processes or asyncio?
30-second answer: Use asyncio for many waiting tasks when your libraries support it. Use threads for libraries that block while waiting. Use processes for heavy calculation in Python.
| Work | First choice | Cost |
|---|---|---|
| Thousands of API calls | asyncio | Needs async libraries |
| Blocking database driver | Threads | Shared data needs care |
| Heavy Python calculation | Processes | More memory, data copying |
Interview note: A blocking call inside an async function stops the whole event loop. Interviewers like this trap.
Q22. How does Python manage memory?
30-second answer: CPython counts the references to each object and usually frees it when the count reaches zero. A garbage collector finds objects that only point to each other.
Memory can still grow through global caches or lists that never shrink. The tracemalloc module can show which lines use more memory over time.
Q23. A Python API has become slow. What do you do?
30-second answer: I measure first. I check which route is slow, when it began, and where time goes: database, network, CPU or waiting. I fix the largest part and measure again.
Good fixes include fewer database calls, a suitable index, or a dictionary in place of repeated list searches. cProfile and py-spy help when the CPU is the problem.
Q24. Why use type hints if Python does not enforce them?
30-second answer: They help people, editors and tools such as mypy find mistakes before the code runs. Python normally ignores them while running. Outside data needs its own checks, for example with Pydantic.
A Protocol is useful when you care about what an object can do, not which class it comes from.
Q25. How would you process a file larger than memory?
30-second answer: Read it in small parts, keep only the running result, and write output as you go. If you must group by many keys, use a database or split the file by key.
Also mention safe restarts. Save the last finished position and make repeated writes harmless.
Pro tip: Prepare one real story about a bug, one about speed and one about a design choice. Say clearly which part was your work.
A seven-day revision plan
- Days 1 and 2: Types, copying, functions and errors. Write each example from memory.
- Day 3: Classes, method order, properties and decorators.
- Day 4: Strings, dictionaries and ten small problems. Add topics from the DSA interview questions.
- Day 5: Generators, testing and concurrency.
- Day 6: Your project: how data moves, how errors are handled, and one thing you would improve.
- Day 7: A timed mock interview. Speak while you code.
These Python interview questions and answers for freshers and working developers are a base. The job description tells you which framework to add, such as Django or FastAPI. CodeBegun teaches the wider path in its Python full stack course, in class at Madhapur, Hyderabad, or live online.
If you have one evening only, do Q2, Q4, Q8, Q9, Q13 and Q20. They give short questions with easy traps, and each has a clear follow-up. Type the code yourself and change one line to see what happens. That is the fastest way to make these Python interview questions and answers stay with you.

