← Backend

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.

Questions by level

Sample questions

A few questions from each level with the correct answer and an explanation.

Python · Junior

  1. Which of these built-in types is immutable?
    Show the answer

    Correct answer: tuple

    Tuples, strings, numbers and frozensets can't change after creation. Lists, dicts and sets can be modified in place.

  2. What is the difference between == and is?
    Show the answer

    Correct answer: == compares values, is checks that both names point to one object

    == calls __eq__ and compares content. is compares identity. Use is for singletons like None, and == for values.

  3. How should you check that a variable is None?
    Show the answer

    Correct answer: if x is None:

    None is a singleton, so PEP 8 recommends is None. == None usually works but can be fooled by a custom __eq__.

  4. What does 7 // 2 return?
    Show the answer

    Correct answer: 3

    // is floor division: it rounds down to the nearest integer. / always returns a float, so 7 / 2 is 3.5.

All 75 questions with answers →

Python · Middle

  1. What does yield turn 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 next yield, so values are produced lazily.

  2. Why is sum(x * x for x in range(10**7)) lighter on memory than sum([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.

  3. Which methods make an object an iterator?
    Show the answer

    Correct answer: __iter__ and __next__, which raises StopIteration

    An iterable only needs __iter__ returning an iterator. An iterator also has __next__. for loops call both and stop on StopIteration.

  4. 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 x and also forwards send(), throw() and the subgenerator's return value.

All 75 questions with answers →

Python · Senior

  1. 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 gc module only exists to find cycles that refcounting can't.

  2. 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.

  3. 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 or weakref.finalize for cleanup.

  4. When is weakref the right tool?
    Show the answer

    Correct answer: Caches or observers that shouldn't keep objects alive

    A weak reference doesn't raise the refcount. WeakValueDictionary entries vanish once nothing else holds the object.

All 75 questions with answers →

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 →