python interwiew questions
python interwiew questions
Mutability: Lists are mutable, meaning their elements can be modified after creation,
while tuples are immutable, meaning their elements cannot be changed.
Syntax: Lists are defined using square brackets ([]), whereas tuples are defined using
parentheses (()).
Performance: Tuples are generally faster than lists due to their immutability.
Use Case: Lists are used when you need a collection of items that can change, while
tuples are preferred for fixed collections of items.
Methods: Lists have more built-in methods for manipulation (like append() and
remove()), while tuples have fewer methods.
Memory Usage: Tuples consume less memory compared to lists of the same size.
The Global Interpreter Lock (GIL) in Python is a mechanism that restricts the execution of
multiple threads in a single process to one thread at a time, even on multi-core systems. This
means that only one thread can execute Python bytecode at any given moment.
Key Points:
Purpose: The GIL is designed to simplify memory management in CPython (the default
Python implementation) by ensuring thread safety when interacting with Python objects.
Impact on I/O-Bound Tasks: The GIL is less of a concern for I/O-bound tasks (like file
operations or network requests) because threads can release the GIL while waiting for I/O
operations to complete.
Workarounds: For CPU-bound tasks requiring parallelism, developers often use the
multiprocessing module instead of threading, as it creates separate processes, each with its
own Python interpreter and GIL. Alternatively, using Python implementations like Jython or
IronPython, which do not have a GIL, can help.
No Explicit Compilation: Python code (.py files) is not explicitly compiled into machine code
by the programmer. Instead, the Python interpreter handles this process internally.
Bytecode Generation: When you run a Python script, the interpreter first converts the
source code into an intermediate format called bytecode. This bytecode is a low-level,
platform-independent representation of the code.
Execution by Python Virtual Machine (PVM): The bytecode is then executed by the Python
Virtual Machine (PVM), which interprets and runs it line-by-line.
Dynamic Execution: Because of this interpretation process, Python can execute code
dynamically, which means you can write and test small pieces of code interactively.
Portability: The interpreted nature of Python makes it cross-platform, as the same Python
code can run on any operating system with the appropriate Python interpreter installed.
8. What is the difference between shallow copy and deep copy in Python?
Intermediate-Level Questions
8. What are Python's generators, and how are they different from lists?
7. What is monkey patching in Python, and when would you use it?
9. Explain the difference between a list and a linked list in terms of implementation.