Python Interview Questions
-
What makes Python useful?
Python offers readable syntax, dynamic typing, automatic memory management and a broad library ecosystem.
-
List vs tuple vs dictionary?
Lists are mutable sequences, tuples are immutable sequences, and dictionaries map hashable keys to values while preserving insertion order in Python 3.7+.
-
How are arguments passed?
Python uses object sharing. Mutating a passed object can affect the caller, but rebinding a parameter does not.
-
What are decorators?
Callables that transform or wrap functions or classes.
-
What are generators?
Iterators that suspend execution at
yieldand produce values on demand. -
How does memory management work?
CPython primarily uses reference counting with cyclic garbage collection.
-
How do you improve performance?
Profile first, choose suitable algorithms and data structures, and avoid unnecessary work.
-
What is the difference between shallow and deep copying?
A shallow copy shares nested objects; a deep copy recursively copies them.
-
How do you handle exceptions?
Use
try,except,else,finallyandraiseas appropriate. -
How do you test and debug?
Use
unittest,pytest,breakpoint()andcProfile.
Example
def squares(n):
for i in range(n):
yield i * i
print(list(squares(5)))Reference
Top 50 Python Interview Questions and Answers.