Python Interview Questions and Answers
Python for backend developers: data types and mutability, functions and closures, classes and the data model, generators, decorators and context managers, type hints, asyncio, the GIL and free threading, memory and performance, pytest, packaging, FastAPI and Django.
225 questions for Python interviews with answers and explanations - Junior, Middle and Senior levels. Sample questions, online quizzes and coding tasks.
- Asyncio
- Typing
- pytest
- FastAPI
- Django
Questions by level
Junior
Core concepts and syntax · 50 questions
Middle
Practice, nuances and optimization · 50 questions
Senior
Architecture, internals, edge cases · 50 questions
Sample questions
A few questions from each level with the correct answer and an explanation.
Python · Junior
- Which of these built-in types is immutable?
Show the answer
Correct answer:
tupleTuples, strings, numbers and frozensets can't change after creation. Lists, dicts and sets can be modified in place.
- What is the difference between
==andis?Show the answer
Correct answer:
==compares values,ischecks that both names point to one object==calls__eq__and compares content.iscompares identity. Useisfor singletons likeNone, and==for values. - How should you check that a variable is
None?Show the answer
Correct answer:
if x is None:Noneis a singleton, so PEP 8 recommendsis None.== Noneusually works but can be fooled by a custom__eq__. - What does
7 // 2return?Show the answer
Correct answer: 3
//is floor division: it rounds down to the nearest integer./always returns a float, so7 / 2is 3.5.
Python · Middle
- What does
yieldturn a function into?Show the answer
Correct answer: A generator function that returns an iterator when called
Calling it runs nothing yet. Each
next()resumes the body until the nextyield, so values are produced lazily. - Why is
sum(x * x for x in range(10**7))lighter on memory thansum([x * x for x in range(10**7)])?Show the answer
Correct answer: The generator produces values one at a time instead of building a list
A list comprehension materializes ten million ints first. A generator expression keeps just one value in flight.
- Which methods make an object an iterator?
Show the answer
Correct answer:
__iter__and__next__, which raisesStopIterationAn iterable only needs
__iter__returning an iterator. An iterator also has__next__.forloops call both and stop onStopIteration. - What does
yield from other_gen()do?Show the answer
Correct answer: Delegates to another generator and passes its values through
It replaces
for x in other: yield xand also forwardssend(),throw()and the subgenerator's return value.
Python · Senior
- How does CPython free most objects?
Show the answer
Correct answer: Reference counting frees them at once; a cyclic GC handles reference cycles
When a refcount drops to zero, the object is freed right away. The
gcmodule only exists to find cycles that refcounting can't. - Why does the cyclic garbage collector use generations?
Show the answer
Correct answer: Most objects die young, so new objects are scanned more often than old ones
Scanning every container object on each pass would be slow. Survivors move to older, rarely scanned generations. 3.14 switched to an incremental collector.
- What risk comes with defining
__del__on a class?Show the answer
Correct answer: It's not guaranteed to run at a predictable time, or at all at exit
Cycles delay finalization, and exceptions in
__del__are only printed. Use context managers orweakref.finalizefor cleanup. - When is
weakrefthe right tool?Show the answer
Correct answer: Caches or observers that shouldn't keep objects alive
A weak reference doesn't raise the refcount.
WeakValueDictionaryentries vanish once nothing else holds the object.
Coding tasks
Write your solution right in the browser, and tests check it instantly. JavaScript interview classics (debounce, deepClone, Promise.all, an LRU cache), TypeScript tasks on generics, narrowing and utility types checked by the real compiler, Node.js backend logic (middleware, rate limiters, job queues), SQL queries in real PostgreSQL, and React components and hooks like a todo list, tabs, useDebounce and useInterval.
Open tasks →