🐍 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) insert
tuple - Immutable, ordered, hashable
set - Unique items, O(1) lookup
dict - Key-value pairs, O(1) access
deque - O(1) append/pop both ends

🔑 Key Concepts

GIL: Only 1 thread runs Python at a time
Mutable default args: Avoid def f(x=[])
is vs ==: Identity vs equality
Pass by assignment: Not by value/reference
Shallow vs Deep copy: copy() vs deepcopy()

🎨 Decorators & Generators

Decorator: Function that wraps another function
@functools.wraps(func) - Preserve metadata
Generator: Lazy evaluation with yield
(x for x in range(n)) - Generator expression
Memory efficient for large datasets

🏗️ OOP Essentials

@property - Getter/setter pattern
@classmethod - Access class, not instance
@staticmethod - No access to class/instance
__slots__ - Memory optimization
MRO: Method Resolution Order (C3 linearization)

🔐 Context Managers

with statement ensures cleanup
__enter__ and __exit__ methods
@contextmanager decorator shortcut
Use for files, locks, DB connections
Exception handling in __exit__

⚡ Async & Concurrency

async/await - Non-blocking I/O
asyncio.gather() - Concurrent tasks
threading - I/O-bound tasks
multiprocessing - CPU-bound (bypass GIL)
concurrent.futures - High-level API

⚠️ Common Interview Gotchas

• Mutable default arguments persist across calls
• List comprehensions create new scope (Python 3)
range() is a lazy sequence, not a list
• Division: / returns float, // returns int
*args is tuple, **kwargs is dict
• Late binding in closures (loop variable issue)

Python 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.
Python

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.

Python

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.

Python

*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.

Python

Context managers allow you to allocate and release resources precisely when you want to. The with statement ensures proper acquisition and release of resources.

Python

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.

Python

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.

Python

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.

Python

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

Python uses automatic memory management with reference counting and a cyclic garbage collector. Understanding this helps write efficient code and avoid memory leaks.

Python

Interview Tips for Python