Python · All levels
Python Interview Preparation: Core Concepts and Common Questions
Revise the Python fundamentals interviewers ask about most — data types, mutability, functions, OOP, generators, exceptions and the GIL — with clear model answers.
Published 4 Dec 20264 min read
Python interviews for freshers and early-career roles usually test whether you understand how the language behaves, not just its syntax. Use this guide to revise, then practise explaining each answer out loud in two or three sentences.
Data types and mutability
Q: What is the difference between mutable and immutable types?
An immutable object cannot be changed after it is created; any "change" creates a new object. int, float, str, tuple and frozenset are immutable. list, dict and set are mutable and can be modified in place.
Q: Why can a tuple be a dictionary key but a list cannot? Dictionary keys must be hashable: their hash must not change during their lifetime. Immutable built-ins such as tuples (containing only hashable items) are hashable; lists are not.
Q: What is the difference between is and ==?
== compares values; is checks whether two names refer to the same object. Use is mainly for None checks: if x is None.
Lists, tuples, sets and dictionaries
| Type | Ordered | Mutable | Duplicates | Typical use |
|---|---|---|---|---|
| list | Yes | Yes | Yes | Sequences you modify |
| tuple | Yes | No | Yes | Fixed records, dict keys |
| set | No | Yes | No | Membership tests, de-duplication |
| dict | Insertion-ordered | Yes | Unique keys | Key → value lookups |
Q: Why is checking x in my_set faster than x in my_list?
Sets use a hash table, so membership checks take average O(1) time; lists must scan elements, which is O(n).
Functions
Q: What are *args and **kwargs?
*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dictionary.
Q: What is the problem with a mutable default argument? Default values are evaluated once, when the function is defined. A default list is therefore shared between calls:
def add(item, bucket=[]): # bug: same list every call
bucket.append(item)
return bucket
def add_fixed(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
Q: What is a lambda? A small anonymous function written in one expression, e.g. sorted(users, key=lambda u: u["age"]).
Comprehensions, iterators and generators
Q: What is a generator and why use one?
A generator function uses yield to produce values one at a time and pauses between them. It does not build the whole sequence in memory, which makes it ideal for large files or streams.
def read_lines(path):
with open(path) as f:
for line in f:
yield line.rstrip()
Q: List comprehension vs generator expression? [x*x for x in nums] builds a list immediately; (x*x for x in nums) produces values lazily.
Object-oriented Python
Q: Explain __init__ and self. __init__ initialises a new instance; self is the instance the method is called on.
Q: Class attribute vs instance attribute? A class attribute is shared by every instance; an instance attribute belongs to one object.
Q: What are @staticmethod and @classmethod? A static method receives no automatic first argument. A class method receives the class (cls) and is often used for alternative constructors.
Q: What are dunder methods? "Double underscore" methods such as __str__, __len__ and __eq__ let your objects work with built-in operations.
Decorators
A decorator is a function that takes a function and returns a new one, adding behaviour without changing the original code:
import functools, time
def timed(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
start = time.perf_counter()
try:
return fn(*args, **kwargs)
finally:
print(f"{fn.__name__} took {time.perf_counter() - start:.3f}s")
return wrapper
Exceptions
Q: What do try, except, else and finally do? else runs only if no exception occurred; finally always runs and is used for clean-up. Catch specific exceptions (except ValueError) rather than a bare except:.
Memory and the GIL
Q: How does Python manage memory? CPython uses reference counting plus a cyclic garbage collector for reference cycles.
Q: What is the GIL? In CPython, the Global Interpreter Lock lets only one thread execute Python bytecode at a time. Threads still help with I/O-bound work; for CPU-bound work, use multiprocessing or native extensions. (Recent CPython versions offer an experimental free-threaded build — mention it only if asked.)
Q: Shallow copy vs deep copy? copy.copy copies the outer object but shares nested objects; copy.deepcopy recursively copies everything.
Practice checklist
- Reverse a string, check a palindrome, count character frequency with
collections.Counter. - Remove duplicates from a list while preserving order.
- Read a CSV file and compute an average per group.
- Write a class with
__str__and__eq__, and a decorator that logs calls. - Explain the time complexity of every solution you write.
- #python
- #freshers
- #programming