Top 20 Python interview questions in 2026 (with sample answers)
These are the questions you'll be asked at almost every Python interview from junior to mid-level. Skip the "what is Python?" softballs other sites pad with — these are the ones that separate hires from passes.
Language fundamentals
1. Mutable vs immutable types — name 3 of each.
Mutable: list, dict, set, bytearray.
Immutable: int, float, str, tuple, frozenset, bytes.
Why it matters: immutables are hashable (can be dict keys / set members); mutables aren't. Default arguments must NEVER be mutable (\def f(x=[]):\ is the classic bug).
2. What's the difference between \is\ and \==\?
\==\ compares values. \is\ compares identity (same object in memory).
Use \is\ only for sentinels — \None\, \True\, \False\. Anywhere else, use \==\. (\x is 0\ works in CPython for small ints due to interning, but you're relying on an implementation detail.)
3. Explain the GIL in one sentence.
The Global Interpreter Lock allows only one Python thread to execute bytecode at a time, so threading speeds up I/O-bound code but NOT CPU-bound code. For CPU-bound parallelism use \multiprocessing\, or the new free-threading mode (PEP 703) coming in 3.13+.
4. List comprehension vs generator expression — when each?
List comp \[x*x for x in range(n)]\ builds the full list in memory. Generator expression \(x*x for x in range(n))\ is lazy — produces one item at a time. Use generators when:
- the list would be huge or infinite
- you only need to iterate once
- you can short-circuit (any/all/next/first match)
5. What does \*args, **kwargs\ actually mean?
\*args\ collects positional arguments into a tuple. \**kwargs\ collects keyword arguments into a dict. Use for forwarding to inner functions (\forward(*args, **kwargs)\) or for variadic APIs.
Data structures
6. What's the time complexity of dict lookup, list lookup, and \in\ for each?
- \
d[k]\: O(1) average, O(n) worst-case (rare with good hash distribution) - \
lst[i]\: O(1) by index - \
k in d\: O(1) average - \
x in lst\: O(n) — common gotcha
If you do many \in\ checks against a collection, convert to set/dict first.
7. set vs frozenset — when each?
\set\ is mutable, can't be a dict key. \frozenset\ is immutable, hashable. Use \frozenset\ when you need a set as a dict key or set member.
8. How would you remove duplicates while preserving order?
Pythonic since 3.7 (dict preserves insertion order):
\\\`python
list(dict.fromkeys(items))
\\\`
The set trick \list(set(items))\ is faster but loses order.
Functions and OOP
9. What is a closure? Give an example where you'd use one.
A function that captures variables from its enclosing scope. Used for stateful factories, decorators, and callbacks:
\\\`python
def counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
c = counter()
print(c(), c(), c()) # 1 2 3
\\\`
10. Explain \@staticmethod\, \@classmethod\, and instance method differences.
- Instance method: takes \
self\, has access to instance state. - \
@classmethod\: takes \cls\(the class), used for alternate constructors (\User.from_dict(...)\) or working with class-level state. - \
@staticmethod\: takes neither, just a function namespaced under the class.
11. Multiple inheritance — what is MRO?
Method Resolution Order — the deterministic order Python walks when looking up a method on an instance. Python uses C3 linearization: left-to-right, depth-first, with diamond resolution. Check \Class.__mro__\.
Multiple inheritance works fine when every class plays nicely with \super()\.
12. \@dataclass\ — what does it give you for free?
\__init__\, \__repr__\, \__eq__\ based on declared fields. With \frozen=True\ it also makes the class hashable. \field(default_factory=list)\ for mutable defaults — never use \[]\ as a default.
Common traps
13. What's wrong with this?
\\\`python
def add(item, items=[]):
items.append(item)
return items
\\\`
The default \[]\ is created once at function definition and shared across all calls. \add("a"); add("b")\ returns \["a", "b"]\. Fix: \def add(item, items=None):\ then \if items is None: items = []\.
14. Shallow vs deep copy?
\b = a.copy()\ (or \list(a)\) creates a new list pointing at the SAME inner objects. For nested structures, mutating inner objects affects both. Use \copy.deepcopy(a)\ to copy recursively.
15. When does \break\ exit only the inner loop?
Always. \break\ exits the nearest enclosing \for\/\while\. To exit multiple loops, factor into a function and use \return\, or use an explicit flag.
Async / concurrency
16. async/await in one breath?
\async def\ defines a coroutine. \await\ suspends until the awaited coroutine resolves, yielding control to the event loop. \asyncio.run(main())\ is the top-level entry point. Coroutines don't run when called — they return a coroutine object that must be \await\ed or scheduled.
17. Why is \time.sleep(1)\ dangerous inside an async function?
It blocks the entire thread, including every other coroutine on the event loop. Use \await asyncio.sleep(1)\ instead.
18. asyncio vs threading vs multiprocessing — when which?
- asyncio — I/O-bound, single-threaded, lightweight (1000s of concurrent tasks). HTTP servers, web scraping.
- threading — I/O-bound where the library is sync-only. Backed by OS threads, GIL-limited.
- multiprocessing — CPU-bound. Separate processes, no GIL constraint, but data is pickled across boundaries.
Pythonic idioms
19. The cleanest way to merge two dicts in 2026?
\\\`python
merged = d1 | d2 # 3.9+
\\\`
\d2\ wins on conflicts. For "create a new dict with d2's values overriding d1":
\{**d1, **d2}\ also works on 3.5+.
20. enumerate vs range(len(...))?
Pythonic:
\\\`python
for i, item in enumerate(items):
...
\\\`
Anti-pattern:
\\\`python
for i in range(len(items)):
item = items[i]
...
\\\`
\enumerate\ is faster, clearer, idiomatic.
How to prep efficiently
Don't memorize. Practice writing answers OUT LOUD as if to a human. Record yourself if you're alone. The single biggest pitfall in real interviews is freezing — not lack of knowledge.
If you want structured drill on these patterns plus the algorithmic side, our Interview Prep track has 100 lessons covering exactly this — from Big-O fundamentals through behavioral STAR stories and a full mock-onsite capstone. First 15 lessons free, no card.