PrepPilotPractise free

Core Python interview questions

Data model, collections, comprehensions, generators, decorators, typing

17 questions · py3.12 · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is the difference between a list and a tuple in Python?

A list is mutable, so you can change its elements, while a tuple is immutable and cannot be changed after creation. Lists use square brackets, tuples use parentheses.

How do you copy a list in Python, and what is the difference between shallow and deep copy?

You can copy a list with list.copy() or slicing. A shallow copy only copies the outer list, so nested objects are shared. A deep copy, using copy.deepcopy, recursively copies everything, so changes to nested objects don't affect the original.

What does the with statement do?

The with statement is used with context managers to ensure resources are properly managed. For example, it automatically closes a file after you're done reading or writing.

What does *args do in a function definition?

*args collects any extra positional arguments passed to a function into a tuple, allowing you to accept a variable number of arguments.

Intermediate questions

Explain the time complexity of lookups in a Python dict and set.

Both dict and set use hash tables, so average-case lookup, insert, and delete operations are O(1). However, in rare cases of hash collisions, the worst case can degrade to O(n), but this is uncommon in practice.

What is a generator, and how is it different from a list comprehension?

A generator produces items one at a time using yield, so it doesn't store all items in memory. A list comprehension creates the entire list in memory. Generators are useful for large or infinite data streams.

How do you create a custom exception, and why would you do it?

You create a custom exception by subclassing Exception. This makes your error handling more explicit and helps callers distinguish different error types in your code.

What is a closure in Python?

A closure is an inner function that remembers variables from its enclosing scope, even after the outer function has finished executing. This allows you to create functions with persistent state.

How do you avoid the mutable default argument pitfall?

I use None as the default value and then set the actual default inside the function. For example: def f(x, l=None): if l is None: l = [].

How do you use a property in a Python class?

I use the @property decorator to define a method that acts like an attribute. This lets me control access or computation when getting or setting a value.

What does the Global Interpreter Lock (GIL) mean for Python threading?

The GIL means only one thread can execute Python bytecode at a time, so threads don't help for CPU-bound tasks. For I/O-bound tasks, threads are still useful because they can release the GIL while waiting.

How does a dataclass differ from a regular class?

A dataclass automatically generates methods like __init__, __repr__, and __eq__ based on the fields you define, reducing boilerplate. Regular classes require you to write these methods yourself.

Senior questions

How would you implement a context manager for acquiring and releasing a lock?

See the answer in the app, free

How do you design a class that supports equality comparison and a readable string representation?

See the answer in the app, free

How do you use type hints and mypy to improve code quality in a large project?

See the answer in the app, free

How would you debug a bug caused by shared mutable state in a multi-threaded Python program?

See the answer in the app, free

What are the trade-offs between using threads and processes for parallelism in Python?

See the answer in the app, free

More Python topics