the full guide

Python interview questions

These are questions commonly asked in Python interviews. Interviewers look for whether you understand why things behave as they do, and not only whether you remember syntax.

30 min Intermediate

What you need: The earlier topics of this module.

Definition

These are questions commonly asked in Python interviews. Interviewers look for whether you understand why things behave as they do, and not only whether you remember syntax.

In simple words

Try saying each answer out loud in your own words before opening it. If you can explain it to a friend, you know it.

Why it matters

These come up constantly, and each one tests an idea that also causes real bugs.

How it works

List vs tuple?

Lists are mutable, and tuples aren't. Tuples can be dict keys when their contents are hashable, and they signal that the data shouldn't change.

is vs ==?

is compares identity, whether it's the same object. == compares values.

Why is a mutable default argument dangerous?

Defaults are created once, when the function is defined, so all calls share one list. Use None and create the list inside.

Shallow vs deep copy?

A shallow copy creates a new outer object, but shares the inner ones. A deep copy duplicates everything.

What are *args and **kwargs?

*args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dict.

What is a decorator?

A function that takes a function and returns a new one, adding behaviour without changing the original's code.

Generator vs list?

A generator produces items one at a time, using little memory, and can be consumed once. A list holds everything in memory.

What is the GIL?

A lock in CPython that lets one thread run Python bytecode at a time. Threads help when waiting, while processes help with heavy calculation.

How does a dict work?

It's a hash table. The key's hash points to a slot, so lookups are O(1) on average. Keys must be hashable.

What does __init__ do, and what is self?

__init__ sets up a new object after it's created. self is the object itself, passed automatically.

What's the difference between @staticmethod and @classmethod?

A class method receives the class as cls. A static method receives neither the class nor the object.

What is a list comprehension, and when should you avoid one?

A short way to build a list from a loop. Avoid it when it gets long or nested, as a normal loop reads better.

In code

One classic question, run for real.

default_trap.py
def bad(item, items=[]):
    items.append(item)
    return items

def good(item, items=None):
    items = [] if items is None else items
    items.append(item)
    return items

print(bad(1), bad(2))
print(good(1), good(2))

Output

[1, 2] [1, 2]
[1] [2]

Common mistakes

  • Memorising answers without running the code. Interviewers ask follow-ups.
  • Saying "I don't know" and stopping. Reason out loud, because how you think counts.

Check yourself

Question 1

1. Which is immutable?

Question 2

2. What does def f(x, a=[]) do on repeat calls?

Summary

  • Understand why, not just what.
  • Run each example yourself.
  • Practise saying the answers out loud.

Finished reading?