Python interview questions
69 Python interview questions with answers, from junior to mid level.
Foundations
- What is Python?
- What does it mean that Python is an 'interpreted' language?
- What does
age = 30do in Python? - What type is
{"name": "Ana", "age": 30}in Python? - What does this function call print?
- How does Python know which lines belong inside an
ifor a loop? - What does this loop print?
- What does
import jsondo? - What are pip and PyPI?
- What is the difference between
printandreturnin a function?
Core
- What does this print?
- What does this print?
- Why is a bare
except:considered harmful? - What is the main difference between a list
[1, 2]and a tuple(1, 2)? - What does the last line print?
- What is
"python"[-1]? - What is
nums[1:3]ifnums = [10, 20, 30, 40, 50]? - What does this print?
- What is
[x * 2 for x in range(4)]? - What do
7 / 2and7 // 2return in Python 3? - In a method
def area(self):, what isself? - What is
__init__for in a class? - What is
len({1, 2, 2, 3, 3, 3})? - What does this print?
- Why is
with open("data.txt") as f:preferred overf = open("data.txt")? - What does this print?
- What is a virtual environment (
python -m venv .venv) for? - Which exception does
int("abc")raise?
Intermediate
- In CPython (default build, GIL enabled), which workload gets a real speedup from
threading? - What does this print?
- What does this print?
sum(x * x for x in range(10**8))versussum([x * x for x in range(10**8)]). What is the key difference?- Why use
functools.wrapsinside a decorator? print([obj])whereobjdefines both__str__and__repr__. Which one is used forobj?- Inside an
async defFastAPI endpoint you calltime.sleep(2)(orrequests.get). What happens under load? - Roughly how long does
main()take? - Inside a coroutine you write
asyncio.sleep(1)withoutawait. What happens? - What does this print?
- Since which Python version is insertion order of
dictguaranteed by the language spec? - What happens with
f(1, 2)? - What does
@dataclass(frozen=True)give you (with defaulteq=True)? - In
try / except / else / finally, when doeselserun? - Which methods make an object an iterator?
- What does this print?
- What is the idiomatic use of
@classmethod? - Building a large string with
s += piecein a loop is discouraged because… - Which structure gives O(1) append and pop at both ends?
- What happens?
- You call a function decorated with
@functools.lru_cachepassing alist. What happens? - Explain the GIL and how you would speed up a CPU-bound Python job.
- How do decorators work? Write a decorator that logs how long a function takes.
- What is the difference between an iterable, an iterator and a generator?
Advanced
- What does this print?
- A typical implementation of a decorator used as
@retry(times=3)needs how many nested callables? - What does this print?
- What is the main effect of defining
__slots__ = ("x", "y")in a class? - A class defines
__eq__but not__hash__. What happens when you add an instance to aset? - In a context manager, what happens if
__exit__returnsTrue? - Two objects reference each other and nothing else references them. What frees them in CPython?
- Which typing tool lets a type checker accept any object that has a
read()method, without inheritance? - What does
Tachieve here? @propertyis built on which mechanism?- What does
type(type)return? - What does this print?
- What is the main overhead of
multiprocessing.Pool.mapcompared to threads? - What is the difference between
__new__and__init__? - Several threads run
counter += 1on a shared global, 1M times each. Is the final count guaranteed correct with the GIL? - What is the current state of the GIL in modern CPython (3.13+)?
- Explain how asyncio works and when you would choose it over threads or processes.