Python Interview Questions Updated
Python Interview Questions Updated
#### 1. What is the difference between deep copy and shallow copy?
- **Shallow Copy**: Creates a new object but does not copy nested objects.
- Allows a child class to access methods and properties of its parent class.
#### 12. What are dictionaries, and how do you create an empty one?
#### 14. Can you explain `max()` and `min()` with examples?
- **`is`**: Checks if two objects have the same memory location (identity).
- The **`filter()`** function filters elements of an iterable based on a function that returns `True` or
`False`.
- The **`enumerate()`** function adds a counter to an iterable and returns it as an enumerate object.
- You can use the `update()` method or the `**` unpacking operator.
- `*args` is used to pass a variable number of non-keyword arguments, and `**kwargs` is used to
- You handle exceptions using `try`, `except`, and optionally `else` and `finally` blocks.
#### 26. What is the difference between `del` and `remove()` in Python?
#### 27. What is a generator in Python, and how do you use it?
- A **generator** is a function that returns an iterable set of items, one at a time, using `yield`.
#### 28. What is the purpose of the `finally` block in exception handling?
- The **`finally`** block is used to execute code that must run regardless of whether an exception
occurred.
#### 29. What are Python decorators, and how do they work?
- **Decorators** are functions that modify the behavior of other functions or methods.
#### 30. What is the difference between a shallow copy and a deep copy of a list?
- **Shallow Copy**: Creates a new object, but nested objects are still references to the originals.
#### 32. What is the difference between a list and a set in Python?
- You can use `sort()` for in-place sorting or `sorted()` for a new sorted list.
#### 34. What is the difference between a local variable and a global variable in Python?
- **Local variable**: Defined inside a function, accessible only within that function.
- **Global variable**: Defined outside any function, accessible throughout the program.
#### 36. What is the difference between `is` and `==` in Python?
- **`is`**: Checks if two objects have the same memory location (identity).
- The **`filter()`** function filters elements of an iterable based on a function that returns `True` or
`False`.
- The **`enumerate()`** function adds a counter to an iterable and returns it as an enumerate object.
- You can use the `update()` method or the `**` unpacking operator.
- `*args` is used to pass a variable number of non-keyword arguments, and `**kwargs` is used to
- You handle exceptions using `try`, `except`, and optionally `else` and `finally` blocks.
#### 45. What is the difference between `del` and `remove()` in Python?
#### 46. What is a generator in Python, and how do you use it?
- A **generator** is a function that returns an iterable set of items, one at a time, using `yield`.
#### 47. What is the purpose of the `finally` block in exception handling?
- The **`finally`** block is used to execute code that must run regardless of whether an exception
occurred.
#### 48. What are Python decorators, and how do they work?
- **Decorators** are functions that modify the behavior of other functions or methods.
#### 49. What is the difference between a shallow copy and a deep copy of a list?
- **Shallow Copy**: Creates a new object, but nested objects are still references to the originals.