Python interviews mix concept questions (mutability, data structures, functions, OOP, generators, decorators, the GIL) with coding tasks solved in idiomatic Python. Strong candidates explain behaviour precisely, avoid common traps such as mutable default arguments, choose the right built-in data structure, and state the time complexity of their solutions. The 30 questions below cover what's asked most.

Beginner questions

1. What are Python's main built-in data types? int, float, complex, bool, str, list, tuple, dict, set, frozenset, bytes and NoneType.

2. What is the difference between a list and a tuple? Lists are mutable; tuples are immutable, slightly lighter, and can be dictionary keys if their contents are hashable.

3. What does mutable mean? The object can change in place. Lists, dicts and sets are mutable; ints, strings and tuples are not.

4. What is the difference between == and is? == compares values; is checks whether two names point to the same object. Use is mainly for None.

5. How do dictionaries work, and how fast are they? They are hash tables mapping hashable keys to values, with average O(1) lookup, insert and delete, and they preserve insertion order (Python 3.7+).

6. What are list comprehensions? Concise syntax for building lists: [x * x for x in nums if x > 0]. There are also dict and set comprehensions and generator expressions.

7. What is the difference between append and extend? append adds one item (even a list, as a single element); extend adds each item of an iterable.

8. How does slicing work? seq[start:stop:step], with stop excluded; s[::-1] reverses a sequence.

9. What are *args and **kwargs? They collect extra positional arguments into a tuple and keyword arguments into a dict.

10. How are exceptions handled? With try/except/else/finally; catch specific exceptions, not a bare except, and use finally (or context managers) for cleanup.

Intermediate questions

11. What is the mutable default argument trap? def add(x, items=[]) reuses the same list across calls. Use items=None and create a new list inside.

12. What is a shallow vs deep copy? A shallow copy copies the outer container but shares nested objects; copy.deepcopy copies everything recursively.

13. What are generators? Functions using yield that produce values lazily, one at a time, saving memory for large or infinite sequences.

14. What is an iterator? An object with __iter__ and __next__ that yields items until StopIteration; for loops use this protocol.

15. What is a decorator? A function that takes a function and returns a wrapped version, used for logging, timing, caching (functools.lru_cache) and access control.

16. What is a context manager? An object used with with that sets up and tears down a resource (files, locks, database connections) reliably.

17. What are lambda functions? Small anonymous functions, typically used as sort keys: sorted(users, key=lambda u: u['age']).

18. What does the scope rule LEGB mean? Names are looked up in Local, Enclosing, Global, then Built-in scopes; global and nonlocal change assignment targets.

19. When would you use a set? For fast membership tests and removing duplicates; set lookups are average O(1) versus O(n) for lists.

20. What is the difference between @staticmethod and @classmethod? A static method receives no automatic first argument; a class method receives the class, which is useful for alternative constructors.

Advanced questions

21. What is the GIL? In the standard CPython interpreter, the Global Interpreter Lock lets only one thread execute Python bytecode at a time. Threads still help for I/O-bound work; for CPU-bound work use multiprocessing or libraries that release the GIL (NumPy). Recent Python versions offer an experimental free-threaded build.

22. Threads, processes or asyncio? asyncio for many concurrent I/O tasks such as network calls; threads for simpler I/O concurrency; processes for CPU-bound parallelism.

23. How does Python manage memory? Mainly reference counting, plus a cyclic garbage collector for reference cycles.

24. What are dunder methods? Special methods like __init__, __repr__, __eq__, __len__ and __iter__ that make objects work with built-in syntax.

25. What are dataclasses? @dataclass generates __init__, __repr__ and comparisons for classes that mainly hold data.

26. What are type hints for? Documentation and static checking (mypy, IDEs) and runtime validation in libraries like Pydantic; they don't enforce types at runtime by default.

27. How does inheritance and MRO work? Python supports multiple inheritance and resolves methods using the C3 method resolution order, visible via Class.__mro__; super() follows it.

28. What are virtual environments? Isolated per-project package installations (venv) so dependencies don't conflict.

29. How would you profile slow Python code? Measure with cProfile or timeit, then fix the hot spot: better algorithms, built-ins, vectorisation with NumPy, or caching.

30. Why are strings immutable, and how do you build long strings? Immutability makes strings hashable and safe to share; build long strings with ''.join(parts) rather than repeated +=.

Which data structure should you reach for?

NeedUseWhy
Count occurrencescollections.CounterClean and fast
Group itemscollections.defaultdict(list)No key-exists checks
Queue / BFScollections.dequeO(1) pops from both ends
Top-k / priorityheapqO(log n) push and pop
MembershipsetAverage O(1) lookup
Sorted searchbisectBinary search on sorted lists
Average comparisons to check membership among 1,000,000 items
List (linear scan, average)500,000
Sorted list + bisect≈20
Set (hash lookup)≈1

Arithmetic: n ÷ 2 for a linear scan, log₂ n for binary search, constant on average for hashing.

How should you answer coding questions?

  1. Clarify inputs, outputs and edge cases.
  2. State a simple approach and its complexity, then improve it.
  3. Write clean, idiomatic code with meaningful names.
  4. Test with small and edge-case inputs out loud.
  5. Mention alternatives and trade-offs.

What are common output-prediction questions?

Interviewers like short snippets that test precise understanding. Examples: what does a = [1, 2]; b = a; b.append(3); print(a) print? ([1, 2, 3], because both names refer to one list.) What does [[0] * 3] * 3 create? (Three references to the same inner list, so changing one row changes all three; use a comprehension instead.) What does print(0.1 + 0.2 == 0.3) print? (False, because of floating-point representation; compare with math.isclose.) What happens when you modify a list while iterating over it? (Items get skipped; iterate over a copy or build a new list.) Practising these builds the precise mental model that also prevents real bugs.

What do data-focused Python interviews add?

For data analyst and data science roles, expect Pandas tasks: filtering, grouping, merging, handling missing values and pivoting, plus NumPy vectorisation and basic plotting. You may be asked to clean a messy CSV live. Our Pandas tutorial covers the operations that come up most.

Being able to explain why a vectorised Pandas operation is faster than a loop, and when to use merge versus concat, often matters as much as the answer itself, because it shows you understand what the library does underneath.

How should you prepare?

Revise each concept with a five-line example you run yourself, solve DSA problems daily in Python, and prepare to walk through two projects. Program Zero teaches Python from scratch in the Python course, followed by DSA; see how to learn Python from scratch and Python projects for beginners.