R
Rishtaara
All Interview Sets
Python

Python Interview Questions

10 questions with detailed answers

Q1. What is Python? What are its key features?

Answer: Python is a high-level, interpreted programming language known for simplicity, readability, dynamic typing, extensive libraries, and cross-platform support.

Q2. What is the difference between list and tuple?

Answer: Lists are mutable and use [], tuples are immutable and use (). Tuples are faster and can be used as dictionary keys.

Q3. What is a decorator in Python?

Answer: A decorator is a function that takes another function and extends its behavior without modifying it, using @syntax.

Q4. Explain GIL (Global Interpreter Lock).

Answer: GIL is a mutex that allows only one thread to execute Python bytecode at a time, affecting CPU-bound multithreading performance.

Q5. What is list comprehension?

Answer: A concise way to create lists: [expression for item in iterable if condition]. More readable and often faster than loops.

Q6. How do *args and **kwargs work?

Answer: *args collects extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary. They make function signatures flexible and are also used when forwarding arguments.

Q7. What is the difference between a shallow copy and a deep copy?

Answer: A shallow copy creates a new outer object but keeps references to nested objects. A deep copy recursively duplicates nested objects, so changes do not affect the original structure.

Q8. What is a Python generator?

Answer: A generator produces values lazily with yield instead of returning the entire result at once. It preserves execution state between iterations and is memory-efficient for large sequences.

Q9. How does exception handling work in Python?

Answer: Risky code goes in try, matching errors are handled by except, else runs when no exception occurs, and finally runs for cleanup whether an exception occurred or not.

Q10. What is a context manager?

Answer: A context manager manages setup and cleanup around a block, commonly through the with statement. Files use it to guarantee closure, and custom managers implement __enter__ and __exit__.