Python Interview Questions - Basic to Advanced
Basic Python Interview Questions
1. What is Python? Key Features:
- High-level, interpreted, dynamically typed.
- Features: Easy syntax, libraries, portability, open-source.
2. Python Data Types:
- int, float, bool, str, list, tuple, set, dict, NoneType
3. List vs Tuple:
- List is mutable, Tuple is immutable.
4. Memory Management:
- Reference counting, garbage collection.
5. *args and **kwargs:
def func(*args, **kwargs):
print(args)
print(kwargs)
6. is vs ==:
- 'is' checks identity, '==' checks value.
7. Conditional Statements: if, elif, else
Python Interview Questions - Basic to Advanced
8. List Comprehension: [x*x for x in range(5)]
9. Lambda Function: add = lambda x, y: x + y
10. Python 2 vs 3:
- print as function, integer division changes, unicode.
Intermediate Python Interview Questions
11. Decorators:
def deco(func):
def wrap():
print('Before')
func()
print('After')
return wrap
12. @staticmethod vs @classmethod vs instance method
13. Generator:
def gen():
yield 1
yield 2
14. Modules vs Packages:
Python Interview Questions - Basic to Advanced
- Module: .py file, Package: folder with __init__.py
15. Exception Handling:
try:
1/0
except ZeroDivisionError as e:
print(e)
16. Built-in functions: map(), filter(), zip(), enumerate(), sorted()
17. deepcopy vs copy:
- deepcopy copies nested objects.
18. Context Manager:
with open('file.txt') as f:
data = f.read()
19. Monkey Patching: Dynamic behavior change at runtime.
20. Handling Memory Leaks: Use gc, avoid circular refs.
Advanced Python Interview Questions
21. GIL:
- Prevents multiple threads from executing at once in CPython.
Python Interview Questions - Basic to Advanced
22. Metaclass:
- Class of a class, used to control class creation.
23. Garbage Collection:
- Reference counting + cyclic collector.
24. threading vs multiprocessing vs asyncio:
- threading for IO-bound, multiprocessing for CPU-bound, asyncio for async IO.
25. __new__ vs __init__:
- __new__ creates, __init__ initializes.
26. Descriptors:
- Define __get__, __set__, or __delete__.
27. Caching:
- Use functools.lru_cache, decorators.
28. Data Classes:
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
Python Interview Questions - Basic to Advanced
29. __slots__:
- Restricts attributes, saves memory.
30. Best Practices:
- Use generators, built-ins, comprehensions.