Python_Interview_Questions_with_Answers
Python_Interview_Questions_with_Answers
Answer: A shallow copy creates a new object but inserts references to the original objects. A deep copy
creates a completely independent copy of the original object, including all nested objects.
Answer: Python uses automatic memory management with garbage collection. It keeps track of object
references and removes unused objects to free memory.
Answer: PEP 8 is Python's style guide that provides best practices for writing clean and readable code.
Following PEP 8 improves code consistency and maintainability.
Answer: Python has several built-in data types including int, float, str, list, tuple, set, dict, and bool.
Answer: Lists are mutable (can be changed), while tuples are immutable (cannot be changed). Tuples are
faster than lists due to their immutability.
Answer: Python 3 introduced several improvements over Python 2, including better Unicode support, new
syntax (e.g., print() as a function), and improved integer division (// operator).
Answer: Python manages memory using private heap space, which contains all Python objects and data
structures. The memory is handled by Python's built-in garbage collector.
Answer: Mutable types (e.g., lists, dictionaries) can be changed after creation, whereas immutable types
(e.g., strings, tuples) cannot be modified after they are created.
11. What is the difference between a shallow copy and a deep copy?
Answer: A shallow copy only copies references to objects, whereas a deep copy creates new objects
recursively, ensuring no references are shared.
Answer: Python uses automatic garbage collection, primarily using reference counting and a cyclic garbage
collector to remove objects that are no longer needed.
Answer: Some built-in functions in Python include len(), type(), id(), sorted(), sum(), min(), max(), and abs().
Answer: Exceptions are handled using try-except blocks. Optionally, you can use finally to execute cleanup
code and else to run code if no exception occurs.
Answer: A module is a single Python file, while a package is a collection of modules organized in a directory
with an __init__.py file.
Answer: Python provides threading using the threading module, but due to the Global Interpreter Lock (GIL),
true parallel execution is better achieved with multiprocessing.
Answer: A lambda function is an anonymous function defined using the lambda keyword. Example: lambda x:
x * 2.
Answer: List comprehensions provide a concise way to create lists. Example: [x for x in range(10) if x % 2 ==
0].
Answer: is checks for object identity (whether two variables reference the same object), while == checks for
equality (whether the values are the same).
Answer: Using the open() function with 'r' mode: with open('file.txt', 'r') as file: data = file.read().