Python core and data structures
Python's object model, collections and reliable control flow.
Use dictionaries for keyed records and generators for streaming large datasets.
Interview questions
Choose a question to reveal its answer.
01EasyCoreWhen should you use a list instead of a tuple?
Answer
When should you use a list instead of a tuple?
Mutable ordered collection supporting indexing and duplicates.
Append a new training example without recreating the collection.
from collections import Counter
words = ['ai', 'ml', 'ai']
counts = Counter(words)
print(counts['ai']) # 2Counter is a dictionary-like frequency collection.
02EasyCoreCan a tuple contain mutable objects?
Answer
Can a tuple contain mutable objects?
Immutable ordered collection; contained objects may still be mutable.
Use a pair of coordinates as a dictionary key when its contents are hashable.
03EasyCoreHow does a Python dictionary achieve efficient lookup?
Answer
How does a Python dictionary achieve efficient lookup?
Mapping from unique hashable keys to values; lookup is typically constant time.
Count tokens by using each token as a key and incrementing its count.
04MediumHigh priorityHow do you remove duplicates efficiently in Python?
Answer
How do you remove duplicates efficiently in Python?
Unordered collection of distinct hashable elements.
Remove repeated labels before creating a vocabulary.
05MediumHigh priorityWhy use a generator instead of returning a list?
Answer
Why use a generator instead of returning a list?
Lazy iterator that yields values on demand rather than storing all of them.
Stream batches from a large file with yield.
06MediumWhat happens when an exception occurs inside a with block?
Answer
What happens when an exception occurs inside a with block?
Protocol that runs setup and cleanup around a with block.
Close a dataset file even if parsing raises an exception.
07MediumHow do try, except and finally work together?
Answer
How do try, except and finally work together?
try and except handle selected exceptions; finally runs cleanup.
Catch a ValueError from invalid numeric input.
08MediumWhen is a list comprehension preferable to a loop?
Answer
When is a list comprehension preferable to a loop?
Compact expression that constructs a list from an iterable with optional filtering.
Square only positive values in a list.
09HardHow do decorators work in Python?
Answer
How do decorators work in Python?
Callable that wraps or transforms a function or class at definition time.
Add timing around a model inference function.
10HardDoes async Python make CPU-bound work faster?
Answer
Does async Python make CPU-bound work faster?
Cooperative concurrency that lets tasks yield while awaiting I/O; it does not itself parallelize CPU work.
Await multiple network requests without blocking the event loop.
11MediumHigh priorityWhy are mutable default arguments dangerous?
Answer
Why are mutable default arguments dangerous?
Default expressions are evaluated once at definition time, so a mutable default can retain changes across calls.
Use None as a sentinel and create a fresh list inside a function.
12MediumHigh priorityHow does a shallow copy differ from a deep copy?
Answer
How does a shallow copy differ from a deep copy?
Copies the outer collection but shares references to nested objects.
Copy a list of lists and observe nested mutations in both copies.
13MediumHigh priorityHow does the GIL affect CPU-bound Python threads?
Answer
How does the GIL affect CPU-bound Python threads?
In conventional CPython builds, the GIL restricts execution of Python bytecode to one thread at a time; free-threaded builds differ.
Use processes or native libraries for CPU-bound work in a conventional CPython runtime.
14MediumHigh priorityWhat is the difference between an iterable and an iterator?
Answer
What is the difference between an iterable and an iterator?
An iterator yields successive items through __next__ and raises StopIteration when exhausted.
Iterate through a dataset without knowing its length beforehand.
15MediumHigh priorityWhat is the difference between is and ==?
Answer
What is the difference between is and ==?
The is operator tests object identity, while == tests equality according to object semantics.
Use is None to test for the None singleton.
Priority and difficulty are editorial study recommendations.
Technical reference: Documentation / paper ↗
Try a few practice questions
Explore the questions before creating a free account.
1. What is printed after a = [1, 2]; b = a; b.append(3); print(a)?
2. Which structure is suitable for counting occurrences of hashable tokens?
3. Which approach avoids storing every transformed row at once?
Python OOP and software design
Continue to the next set of interview questions.