Stop 15 · Portfolio & Interview PrepTopics · 3/9

Core Python Interview Questions

Part of the Python Roadmap

In short

Python interviews love short concept questions: list vs tuple, shallow vs deep copy, what a decorator is. A crisp answer with a two-line example beats a memorised textbook definition every time.

Interviews test concepts like mutable versus immutable types, list versus tuple, shallow versus deep copy, *args and **kwargs, decorators, generators, the GIL and how dicts work. Clear explanations with small examples matter more than memorised definitions.

The jaadu carefully reverses a small car between orange cones on a driving test ground while an inspector with a clipboard watches and nods.

Think of it like this

The driving licence test

The RTO inspector doesn't ask you to recite the motor vehicles act. They ask you to reverse into a spot and explain what you'd do at a blinking signal. Interviewers do the same: they check you can use and explain the concept, ideally with a tiny example you could type right away.

Watch it happen

Shallow copy vs deep copy

copy.copy(x)

  • Makes a new outer container
  • Inner objects are shared with the original
  • Changing an inner list affects both

copy.deepcopy(x)

  • Copies the container and everything inside it
  • Nothing is shared
  • Safer for nested data, but slower

Flat list of numbers? A shallow copy is enough. Nested lists or dicts you plan to change? Use deepcopy.

The big ideas

What you need to know

Mutable vs immutable

Lists, dicts and sets can change in place. int, str and tuple can't. Only immutable (hashable) values can be dict keys.

*args and **kwargs

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

Decorators

A function that takes a function and returns a new one that wraps it, used for logging, timing, caching or login checks.

Generators

Functions with yield produce values one at a time and keep their state, so they can handle huge or endless data with little memory.

See it in code

Walk through the code

python
1import copy2import time3from functools import wraps4 5# 1. Shallow vs deep copy6marks = [[90, 80], [70, 60]]7shallow = copy.copy(marks)8deep = copy.deepcopy(marks)9marks[0][0] = 010print(shallow[0][0], deep[0][0])   # 0 9011 12# 2. *args and **kwargs13def order(*items, **options):14    print(items, options)15 16order("dosa", "chai", table=4, parcel=False)17# ('dosa', 'chai') {'table': 4, 'parcel': False}18 19# 3. A decorator20def timer(func):21    @wraps(func)22    def wrapper(*args, **kwargs):23        start = time.perf_counter()24        result = func(*args, **kwargs)25        print(f"{func.__name__} took {time.perf_counter() - start:.4f}s")26        return result27    return wrapper28 29@timer30def total(n):31    return sum(range(n))32 33# 4. A generator34def squares(n):35    for i in range(n):36        yield i * i37 38print(total(1_000_000), list(squares(5)))   # timing line, then 499999500000 [0, 1, 4, 9, 16]

1/5The shallow copy shares the inner lists, so it sees the change. The deep copy doesn't.

Four classic questions answered with code you could type on a whiteboard in under a minute each.

Save yourself the pain

Common mistakes

Oops

Answering "a tuple is an immutable list" and stopping there.

Do this instead

Add why it matters: tuples can be dict keys, signal fixed records like (lat, lng) and are slightly lighter. Then give a one-line example.

Oops

Memorising answers without running them.

Do this instead

Type every example in a Python shell. Interviewers often follow up with "what would this print?"

Oops

Saying "Python is pass by reference".

Do this instead

Python passes references to objects (often called pass by object reference). Rebinding a parameter doesn't affect the caller, but mutating a list does.

Your mission

Twenty questions drill

~45 min

Quick check

Which of these can be used as a dictionary key?

Go deeper

  1. 1Make a list of 30 common Python questions and answer each in your own words.
  2. 2Back every answer with a two-line code example.
  3. 3Practise explaining the mutable default argument bug.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge