Skip to main content
← all_articles
Interview prep2026-05-17 Β· 14 min read

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):

β–Ά PYTHONeditable Β· runs in your browser

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:

β–Ά PYTHONeditable Β· runs in your browser

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?

β–Ά PYTHONeditable Β· runs in your browser

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 awaited 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?

β–Ά PYTHONeditable Β· runs in your browser

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:

β–Ά PYTHONeditable Β· runs in your browser

Anti-pattern:

β–Ά PYTHONeditable Β· runs in your browser

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 65 lessons covering exactly this. 7 days free, no card for the first lesson.

cta_title

cta_body

cta_button β†’

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.