What is Python's performance optimization techniques?
Why Interviewers Ask This
Interviewers ask this to evaluate whether you have the depth of knowledge needed to mentor others and lead technical decisions. The expected answer goes beyond definitions into practical implications and real-world consequences.
Answer
Python code optimization covers multiple levels. Algorithmic: choose correct data structures (set for membership O(1) vs list O(n), deque for O(1) head operations). Built-ins: built-in functions (implemented in C) are faster than Python equivalents — use sum(), map(), sorted(). List comprehensions are faster than equivalent for-loops. Generator expressions for memory efficiency. Local variables are faster than global/attribute access — cache frequently accessed attributes in local vars. String joining: use "".join(list) not += in loops. __slots__ for memory-heavy classes. lru_cache/cache for memoization. NumPy for array operations (vectorized C operations). Cython/mypyc: compile Python to C. PyPy: JIT-compiled Python for 5-10x faster execution. Profile before optimizing: cProfile, line_profiler, memory_profiler.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Python project, I used this when...' immediately makes your answer more credible and memorable.