0% found this document useful (0 votes)
11 views

Python Interview Questions Updated

Uploaded by

hpatel63557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Interview Questions Updated

Uploaded by

hpatel63557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

### Python Concepts Interview Questions

#### 1. What is the difference between deep copy and shallow copy?

- **Shallow Copy**: Creates a new object but does not copy nested objects.

- **Deep Copy**: Creates a completely independent copy of the object.

#### 2. What is the difference between mutable and immutable objects?

- **Mutable**: Can be changed (e.g., `list`, `dict`).

- **Immutable**: Cannot be changed (e.g., `tuple`, `str`).

#### 3. What does the `id()` function do?

- Returns the memory address of an object.

#### 4. Can you tell me about `range()` with a real-time example?

- Generates a sequence of numbers.

#### 5. How does the membership operator (`in`) work?

- Checks if an element exists in a collection.

#### 6. What is list comprehension?

- A concise way to create a new list.

#### 7. What is a ternary operator?

- A one-line `if-else` statement.

#### 8. What is the `self` keyword in Python classes?


- Represents the current instance of the class.

#### 9. What is the purpose of the `__init__()` method in a class?

- Initializes an object when it is created.

#### 10. What is the `super()` keyword?

- Allows a child class to access methods and properties of its parent class.

#### 11. What is the difference between a list and a tuple?

- **List**: Mutable, uses `[]`.

- **Tuple**: Immutable, uses `()`.

#### 12. What are dictionaries, and how do you create an empty one?

- A collection of key-value pairs.

#### 13. How do you use the `zip()` function?

- Combines two or more iterables element-wise into tuples.

#### 14. Can you explain `max()` and `min()` with examples?

- `max()`: Returns the largest element.

- `min()`: Returns the smallest element.

#### 15. How do you create a reverse range?

- Use a negative step in `range()`.

#### 16. How do you use the `map()` function?

- Applies a function to every item in an iterable.


#### 17. What is the difference between `is` and `==` in Python?

- **`is`**: Checks if two objects have the same memory location (identity).

- **`==`**: Checks if two objects have the same value.

#### 18. How do you create an empty set in Python?

- You can create an empty set using `set()`.

#### 19. What are lambda functions in Python?

- A **lambda function** is an anonymous function defined using the `lambda` keyword.

#### 20. What is the purpose of the `filter()` function?

- The **`filter()`** function filters elements of an iterable based on a function that returns `True` or

`False`.

#### 21. How do you remove an element from a list in Python?

- You can use `remove()`, `pop()`, or `del` to remove elements.

#### 22. What is the `enumerate()` function and how is it used?

- The **`enumerate()`** function adds a counter to an iterable and returns it as an enumerate object.

#### 23. How do you merge two dictionaries in Python?

- You can use the `update()` method or the `**` unpacking operator.

#### 24. What are *args and **kwargs in Python?

- `*args` is used to pass a variable number of non-keyword arguments, and `**kwargs` is used to

pass keyword arguments.


#### 25. How do you handle exceptions in Python?

- You handle exceptions using `try`, `except`, and optionally `else` and `finally` blocks.

#### 26. What is the difference between `del` and `remove()` in Python?

- **`del`**: Deletes a variable or item from a collection by index.

- **`remove()`**: Removes the first occurrence of a value.

#### 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.

- **Deep Copy**: Creates a completely independent copy, including nested objects.

#### 31. How do you check if a key exists in a dictionary?

- You can use `in` or the `.get()` method.

#### 32. What is the difference between a list and a set in Python?

- **List**: Ordered, allows duplicates.


- **Set**: Unordered, does not allow duplicates.

#### 33. How do you sort a list 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.

#### 35. What are some common built-in modules in Python?

- **`math`**, **`random`**, **`datetime`**, **`os`**, **`sys`**, **`collections`**.

#### 36. What is the difference between `is` and `==` in Python?

- **`is`**: Checks if two objects have the same memory location (identity).

- **`==`**: Checks if two objects have the same value.

#### 37. How do you create an empty set in Python?

- You can create an empty set using `set()`.

#### 38. What are lambda functions in Python?

- A **lambda function** is an anonymous function defined using the `lambda` keyword.

#### 39. What is the purpose of the `filter()` function?

- The **`filter()`** function filters elements of an iterable based on a function that returns `True` or

`False`.

#### 40. How do you remove an element from a list in Python?


- You can use `remove()`, `pop()`, or `del` to remove elements.

#### 41. What is the `enumerate()` function and how is it used?

- The **`enumerate()`** function adds a counter to an iterable and returns it as an enumerate object.

#### 42. How do you merge two dictionaries in Python?

- You can use the `update()` method or the `**` unpacking operator.

#### 43. What are *args and **kwargs in Python?

- `*args` is used to pass a variable number of non-keyword arguments, and `**kwargs` is used to

pass keyword arguments.

#### 44. How do you handle exceptions in Python?

- You handle exceptions using `try`, `except`, and optionally `else` and `finally` blocks.

#### 45. What is the difference between `del` and `remove()` in Python?

- **`del`**: Deletes a variable or item from a collection by index.

- **`remove()`**: Removes the first occurrence of a value.

#### 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.

- **Deep Copy**: Creates a completely independent copy, including nested objects.

#### 50. How do you check if a key exists in a dictionary?

- You can use `in` or the `.get()` method.

You might also like