🐍 Python Interview Questions
Master Python interviews with questions on core Python, OOP, decorators, async programming, and frameworks
Data structures, decorators, generators, async/await, OOP, and Python fundamentals
15-Minute Python Cheatsheet
Quick reference for last-minute interview preparation
📦 Data Structures
list - Mutable, ordered, O(1) append, O(n) inserttuple - Immutable, ordered, hashableset - Unique items, O(1) lookupdict - Key-value pairs, O(1) accessdeque - O(1) append/pop both ends🔑 Key Concepts
def f(x=[])copy() vs deepcopy()🎨 Decorators & Generators
@functools.wraps(func) - Preserve metadatayield(x for x in range(n)) - Generator expression🏗️ OOP Essentials
@property - Getter/setter pattern@classmethod - Access class, not instance@staticmethod - No access to class/instance__slots__ - Memory optimization🔐 Context Managers
with statement ensures cleanup__enter__ and __exit__ methods@contextmanager decorator shortcut__exit__⚡ Async & Concurrency
async/await - Non-blocking I/Oasyncio.gather() - Concurrent tasksthreading - I/O-bound tasksmultiprocessing - CPU-bound (bypass GIL)concurrent.futures - High-level API⚠️ Common Interview Gotchas
range() is a lazy sequence, not a list/ returns float, // returns int*args is tuple, **kwargs is dictPython has four main built-in data structures, each with different characteristics and use cases.
- List: Ordered, mutable, allows duplicates. Use for sequences that change.
- Tuple: Ordered, immutable, allows duplicates. Use for fixed data.
- Set: Unordered, mutable, no duplicates. Use for unique items and set operations.
- Dictionary: Key-value pairs, mutable, keys must be unique. Use for lookups.
Decorators are a powerful feature that allows you to modify the behavior of functions or classes. They are functions that take another function as input and extend its behavior without explicitly modifying it.
Generators are a simple way to create iterators. They use yield instead of returnand automatically implement the iterator protocol, making them memory-efficient for large datasets.
*args allows a function to accept any number of positional arguments as a tuple.**kwargs allows a function to accept any number of keyword arguments as a dictionary.
Context managers allow you to allocate and release resources precisely when you want to. The with statement ensures proper acquisition and release of resources.
Magic methods (also called dunder methods) are special methods with double underscores that allow you to define how objects behave with built-in operations. They enable operator overloading and customize object behavior.
Async/await enables asynchronous programming, allowing you to write concurrent code that can handle multiple I/O operations efficiently without blocking. It's particularly useful for network requests, file I/O, and database operations.
The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecode simultaneously. This makes CPU-bound multithreaded programs slower but doesn't affect I/O-bound programs.
List comprehensions create a complete list in memory, while generator expressions create an iterator that generates values on demand. Generator expressions are more memory-efficient for large datasets.
Python uses automatic memory management with reference counting and a cyclic garbage collector. Understanding this helps write efficient code and avoid memory leaks.
Interview Tips for Python
- ✓ Understand the differences between Python 2 and Python 3
- ✓ Know when to use lists vs tuples vs sets vs dictionaries
- ✓ Master decorators and context managers
- ✓ Understand the GIL and its implications
- ✓ Be familiar with async/await for concurrent programming
- ✓ Know popular frameworks (Django, Flask, FastAPI)
- ✓ Understand memory management and garbage collection
- ✓ Practice with generators and iterators