Python Interview Questions and Answers
1. What are Python's key features?
- Easy to learn and use
- Interpreted language
- Dynamically typed
- Extensive standard library
- Supports object-oriented, procedural, and functional programming
- Portable and cross-platform
- Huge community support
2. What is the difference between list and tuple in Python?
- Lists are mutable while tuples are immutable.
- Lists have variable length; tuples have fixed length.
- Lists consume more memory; tuples are memory efficient.
- Tuples are faster than lists.
3. Explain Python's memory management.
- Python uses private heap space to manage memory.
- It has an in-built garbage collector.
- Reference counting and cyclic garbage collector are used.
4. What are Python decorators?
- Decorators are functions that modify the behavior of another function.
- Commonly used in logging, access control, instrumentation, etc.
- Defined with the @decorator_name syntax.
5. What is the difference between 'is' and '=='' in Python?
- '==' checks value equality.
- 'is' checks reference (object identity) equality.
6. How does Python handle multithreading?
- Python supports multithreading using the threading module.
- Due to the Global Interpreter Lock (GIL), true parallelism is limited.
- Best suited for I/O-bound rather than CPU-bound tasks.
7. What are Python's data types?
- Numeric types: int, float, complex
- Sequence types: list, tuple, range
- Text type: str
- Set types: set, frozenset
- Mapping type: dict
- Boolean type: bool
- None type: NoneType
8. What is the difference between deep copy and shallow copy?
- Shallow copy creates a new object but references the original nested objects.
- Deep copy creates a new object and recursively copies all nested objects.
- Use copy.copy() for shallow copy and copy.deepcopy() for deep copy.
9. What are Python generators?
- Generators are iterators that yield items one at a time using the 'yield' keyword.
- More memory efficient for large datasets.
- Maintains state between iterations.
10. What are lambda functions?
- Anonymous, inline functions defined using the 'lambda' keyword.
- Syntax: lambda arguments: expression