Python Middle interview questions and answers
75 questions for a Python (Middle) interview - with correct answers, explanations and links to the docs.
- 1.What does
yieldturn a function into? - 2.What does this code print?
- 3.Why is
sum(x * x for x in range(10**7))lighter on memory thansum([x * x for x in range(10**7)])? - 4.Which methods make an object an iterator?
- 5.What does
yield from other_gen()do? - 6.What is a decorator?
- 7.Why use
functools.wrapsinside a decorator? - 8.How is a decorator with arguments, like
@retry(times=3), built? - 9.What does this code print?
- 10.What does
@functools.lru_cachedo? - 11.What does
functools.partial(pow, exp=2)return? - 12.Which methods does an object need to work with the
withstatement? - 13.What happens if
__exit__returnsTrue? - 14.What does
@contextlib.contextmanagerlet you write? - 15.How do you give a dataclass field a fresh empty list for each instance?
- 16.What does
@dataclass(frozen=True)give you? - 17.What does
__slots__do in a class? - 18.What does
@propertylet you do? - 19.What is the difference between
@classmethodand@staticmethod? - 20.What determines which parent method
super()calls with multiple inheritance? - 21.What does inheriting from
abc.ABCwith an@abstractmethodachieve? - 22.What is a
typing.Protocolfor? - 23.What happens at runtime when you call
def f(x: int)withf("hello")? - 24.How do you annotate a value that may be a
strorNonein modern Python? - 25.When is
typing.TypedDicta good fit? - 26.If a class defines
__eq__but not__hash__, what happens? - 27.What does
collections.defaultdict(list)do on a missing key? - 28.What does
Counter("banana").most_common(1)return? - 29.Why use
collections.dequefor a queue instead of a list? - 30.What does
heapqprovide? - 31.Why must data be sorted before
itertools.groupby? - 32.Python's
sortedis stable. What does that mean? - 33.What does the walrus operator
:=do? - 34.What does the
*mean indef connect(host, *, timeout=10):? - 35.What does
/mean indef f(a, b, /, c):? - 36.What does
{**a, **b}produce for two dicts? - 37.What does the GIL in CPython prevent?
- 38.Which tool fits CPU-heavy pure Python work on a standard CPython build?
- 39.What does
ThreadPoolExecutor.mapsuit best? - 40.What does
awaitdo inside anasync deffunction? - 41.What happens if you call
time.sleep(5)inside a coroutine? - 42.What does
asyncio.gather(a(), b(), c())do? - 43.What does calling a coroutine function without
awaitdo? - 44.What is
asyncio.run(main())for? - 45.What is a pytest fixture?
- 46.What does
scope="session"on a pytest fixture mean? - 47.What does
@pytest.mark.parametrizedo? - 48.In
app/service.py,from app.client import fetchis used. What should a test patch? - 49.What is pytest's
monkeypatchfixture good for? - 50.Why use
logginginstead ofprintin an application? - 51.What is the recommended way to get a logger in a module?
- 52.Why write
logger.info("user %s", user_id)instead of an f-string? - 53.What does
raise NewError(...) from errdo? - 54.What should custom exceptions in an application usually inherit from?
- 55.What does this function return?
- 56.What causes an
ImportErrorwith "partially initialized module"? - 57.What does
__all__in a module control? - 58.What is
pyproject.tomlfor? - 59.Why commit a lock file like
uv.lockfor an application? - 60.What does
f"{total=}"print whentotal = 42? - 61.Why is
a is bunreliable for comparing integers? - 62.What does an
Enummember compare equal to? - 63.What is the problem with
datetime.now()without arguments in a backend? - 64.Why does
json.dumps({"at": datetime.now()})fail? - 65.What is the default timeout in
requests.get(url)? - 66.In FastAPI, what does declaring a parameter as a Pydantic model give you?
- 67.What does
Depends()do in FastAPI? - 68.In FastAPI, a route uses a blocking DB driver. How should it be declared?
- 69.What causes the N+1 query problem in the Django ORM?
- 70.When should you use
prefetch_relatedrather thanselect_relatedin Django? - 71.When does a Django QuerySet actually hit the database?
- 72.What does
copy.deepcopydo with an object that references itself? - 73.Why is
"".join(parts)preferred overs += partin a long loop? - 74.What does
@pytest.fixture(autouse=True)do? - 75.What should
__eq__return when it gets an object of an unsupported type?