Python Theory
Generators are functions that use the yield keyword to produce a sequence of values lazily, one at a time, instead of computing them all at once and sto...
What are generators in Python and how do they work?
Generators are functions that use the yield keyword to produce a sequence of values lazily, one at a time, instead of computing them all at once and storing them in memory. When a generator function is called, it returns a generator object that implements the iterator protocol ( iter () and next () ). Key characteristics: Lazy evaluation : Values are computed on demand, not upfront State preservation : The function pauses at each yield and resumes from where it left off Memory efficiency : Only
What is the purpose of the pass statement in Python?
The pass statement is a null operation that serves as a syntactic placeholder where Python requires a statement but no action is needed. It does nothing when executed. Common use cases: The pass statement is essential because Python uses indentation to define code blocks. Unlike languages with braces, Python cannot have an empty block, so pass fills that role. In production code, prefer adding a comment explaining why pass is used rather than leaving it unexplained.
How do you create a virtual environment in Python?
A virtual environment is an isolated Python environment that allows you to manage dependencies separately for each project, avoiding version conflicts between packages. Using the built in venv module (Python 3.3+): Alternative tools: conda : Manages both Python packages and system level dependencies, popular in data science poetry : Modern dependency management with lockfiles and project metadata pipenv : Combines pip and venv with a Pipfile.lock for deterministic builds Best practices: Always u
How does the zip() function work in Python?
The zip() function combines multiple iterables element wise into an iterator of tuples , pairing items at the same index. It stops at the shortest iterable by default. Advanced usage: zip() is commonly used in data engineering for transposing rows/columns, merging parallel lists, and iterating over multiple data streams simultaneously.
Explain Python's memory management model.
Python uses an automatic memory management system with three main components: a private heap, reference counting, and a cyclic garbage collector. Key components: Private heap : All Python objects are stored in a private heap managed by the Python Memory Manager . Programmers cannot access it directly. Reference counting : Each object tracks how many references point to it. When the count drops to zero, memory is freed immediately. Cyclic garbage collector : Handles reference cycles (e.g., object
Is Python a compiled language or an interpreted language?
Python is generally classified as an interpreted language , but the reality is more nuanced it uses a hybrid approach involving both compilation and interpretation. Python's execution process: 1. Source code ( .py ) is first compiled to platform independent bytecode ( .pyc files stored in pycache / ) 2. The bytecode is then interpreted by the Python Virtual Machine (PVM) , which executes instructions one by one This differs from: Purely compiled languages (C, C++, Rust): Source code is compiled
What is the Global Interpreter Lock (GIL) and how does it affect concurrency?
The GIL (Global Interpreter Lock) is a mutex in CPython that allows only one thread to execute Python bytecode at a time, even on multi core systems. It exists to protect CPython's memory management (reference counting) from race conditions. Impact on concurrency: CPU bound tasks : The GIL is a bottleneck because threads cannot run in parallel. Only one thread executes at a time, making multi threading no faster than single threaded execution. I/O bound tasks : The GIL is released during I/O ope
What are decorators in Python?
A decorator is a function that takes another function (or class) as input and extends or modifies its behavior without changing its source code. Decorators use the @ syntax and are a form of higher order function . Using functools.wraps to preserve metadata: Decorators with arguments: Common built in decorators: @staticmethod , @classmethod , @property , @functools.lru cache .