‹ Other

Python

1 test · 10 questions
Browse all 10 questions ›
Your progress

About these Python questions

Python is the working language of data engineering, machine learning, automation and a large share of backend services, so it shows up in coding interviews and in take-home reviews for all of those roles. Writing Python that runs is easy. The questions that separate candidates are about the places where the language does something you might not expect: a default argument that remembers earlier calls, a copy that isn't a full copy, a closure that sees the last value of a loop variable.

These questions are for developers preparing for interviews, people who learned Python on the job and want to fill the gaps, and anyone working towards the ML and data certifications, where Python is assumed rather than taught. Most are short snippets that ask what gets printed or why a check fails, and they are written against CPython 3.12, the reference implementation.

What the questions cover

  • Functions and arguments. Why a mutable default argument is shared between calls, and how functools.wraps keeps a decorated function's name and docstring.
  • Data model and identity. The difference between is and ==, and why integer identity checks pass in small tests and fail in production. Which types are hashable and can be dictionary keys, and how deep a shallow copy goes.
  • Built-in types. How dictionaries keep insertion order, and what deleting and re-adding a key does to it.
  • Iterators and generators. Why a generator can be consumed only once.
  • Concurrency. Why threads don't speed up pure-Python, CPU-bound work on the standard CPython build with its global interpreter lock, and what does.
  • Exceptions. The exact order in which try, except, else and finally run, including when one of them returns.
  • Scope and closures. Late binding in closures created in a loop or comprehension, and the usual ways to capture the value you meant.

How to use them

There is one practice test of 10 questions, mixing easy, medium and hard. Each explanation walks through what the interpreter actually does and links to the part of the Python documentation that defines it.

  • Predict the output before you check. Write your guess down; it is easy to convince yourself afterwards that you knew.
  • Then run the snippet in a REPL and change it. Swap the default argument for None, add a return to the finally block, or capture the loop variable with a default argument, and see what changes.
  • Learn the rule, not the trick. Most of these follow from a few ideas: names are bound to objects, a default is evaluated once when the function is defined, and closures look names up when they are called.
  • Read the relevant sections of the language reference, on the execution model, compound statements and the data model. They are shorter than they look and answer most "why" questions.

These questions are original, written from the official Python documentation. They are not taken from any real interview or exam.