Python Interview Questions & Answers
1. Basics
- Python's Key Features: Easy syntax, interpreted, dynamic typing, OOP support, standard library.
- Data Types: int, float, str, bool, list, tuple, set, dict, None, bytes.
- 'is' vs '==': 'is' checks identity; '==' checks value.
- Mutable vs Immutable: Mutable - list, dict, set. Immutable - int, float, str, tuple.
- Memory Management: Automatic GC, reference counting.
2. Data Structures
- List vs Tuple vs Set vs Dict:
List: ordered, mutable.
Tuple: ordered, immutable.
Set: unordered, unique items.
Dict: key-value pairs.
- Use Tuple: For fixed data.
- Iterate Dict: for k,v in [Link]()
- Remove Duplicates: list(set(my_list))
3. Functions
- *args/**kwargs: *args for positional, **kwargs for keyword args.
- Lambda: Anonymous function (e.g. lambda x: x*x)
- Deep vs Shallow Copy: Deep copies all levels, shallow copies top level only.
- Decorator: Wraps another function to modify behavior.
4. OOP
- Class/Object: Class is blueprint; object is instance.
- Inheritance Types: Single, Multiple, Multilevel, Hierarchical, Hybrid.
- @staticmethod/@classmethod: static - no self/cls; classmethod - uses cls.
- OOP Concepts: Encapsulation (data hiding), Polymorphism (many forms), Abstraction (hide
complexity).
5. Modules & Packages
- Module: Python file. Package: folder with __init__.py.
- Import: import math / from math import sqrt
- __init__.py: Marks directory as package.
6. Exception Handling
- try/except: Handle errors.
- try/finally: Code in finally always runs.
- Custom Exception: class MyError(Exception): pass
7. File Handling
- Read/Write: with open("[Link]", "r") as f: data = [Link]()
- Modes: r, w, a, rb, etc.
8. Advanced
- Generator: Uses yield; lazy eval.
- Coroutine: yield + send()
- GIL: Global Interpreter Lock, restricts threads.
- Metaclass: Class of a class.
9. Libraries & Tools
- Libraries: NumPy, Pandas, requests, Flask, Django.
- virtualenv: Isolated Python environments.