PYTHON
PYTHON
Answer: A generator is a function that returns an iterator and produces items lazily using the
yield keyword.
Answer: It ensures that code runs only when the file is executed directly, not when imported.
Answer: A decorator is a function that modifies the behavior of another function or class.
Answer: It simplifies resource management like file handling by ensuring automatic cleanup.
Answer: It applies a function to every item in an iterable and returns a map object.
Answer: It is a concise way to create lists using [expression for item in iterable].
Answer: raise manually throws an exception, while assert checks conditions and raises
AssertionError.
Answer: GIL ensures only one thread runs Python bytecode at a time.
Answer: int, float, str, bool, list, tuple, dict, set, and NoneType.
Answer: copy() creates a shallow copy, while deepcopy() creates a deep copy.
Answer: It returns the hash value of an object (used in sets and dictionaries).
Answer: Mutable objects can be modified after creation (e.g., lists), while immutable objects
cannot (e.g., tuples, strings)
Answer:
A decorator is a function that modifies the behavior of another function or method. It allows
us to wrap another function to add functionality without modifying the original function’s
code.
Answer:
Python's garbage collector automatically reclaims memory occupied by unused objects. It
uses reference counting and a cyclic garbage collector to manage memory.
3. What is the Global Interpreter Lock (GIL), and how does it affect multithreading?
Answer:
The Global Interpreter Lock (GIL) is a mutex in CPython that allows only one thread to
execute Python bytecode at a time, even in multi-core systems.
The GIL ensures thread-safety but prevents true parallel execution of threads.
In CPU-bound tasks, the GIL can be a bottleneck, as threads cannot execute
simultaneously.
Answer:
The with statement simplifies resource management, such as opening and closing files. It
ensures resources are properly cleaned up, even if an exception occurs.
5. What are Python generators, and how are they different from lists?
Answer:
A generator is a function that produces values lazily, using the yield keyword, instead of
returning a list.
Key Differences:
1. Memory Efficient: Generators don’t store all values in memory, unlike lists.
2. On-Demand: Generators produce values one at a time.
Answer:
Metaprogramming is writing code that manipulates other code, such as classes or functions,
at runtime.
Examples of metaprogramming:
7. What is the difference between a class method, static method, and instance method?
Answer:
1. Instance Method:
o Default method in a class.
o Uses self to access instance variables.
2. Class Method:
o Uses @classmethod decorator.
o Uses cls to access class variables.
3. Static Method:
o Uses @staticmethod decorator.
o Does not access instance or class variables.
Answer:
The subprocess module is used to execute external programs or commands from Python
scripts.
Answer:
A context manager simplifies resource management. It uses __enter__ and __exit__ methods.
Answer:
The multiprocessing module enables parallel execution of code by creating separate
processes.