Python Interview Questions
Python Interview Questions
1. What is Python?
Answer: Python is an interpreted, high-level, general-purpose programming
language. It is known for its simple syntax and readability, making it easy for
beginners to learn and use.
3. What is PEP 8?
Answer: PEP 8 is the Python Enhancement Proposal that outlines the style
guidelines for writing Python code to ensure readability and consistency across
Python projects.
13. What are the common operations you can perform on sets?
Answer:
- Union: set1 | set2
- Intersection: set1 & set2
- Difference: set1 - set2
- Symmetric difference: set1 ^ set2
18. What is the difference between a regular function and a lambda function?
Answer:
- Regular Function: Defined using def, can have multiple expressions.
- Lambda Function: Defined using lambda, has only one expression.
34. What is the difference between `__str__` and `__repr__` methods in Python?
Answer:
- __str__: Used for creating a readable string representation of an object.
- __repr__: Used for creating an official string representation, often used for
debugging.
37. What is the difference between `try`, `except`, and `finally` in exception
handling?
Answer:
- try: Code that may raise an exception.
- except: Code that handles the exception.
- finally: Code that runs no matter what, even if an exception is raised.
63. What is the difference between mutable and immutable objects in Python?
Answer:
- Mutable: Can be modified after creation (e.g., lists, dictionaries).
- Immutable: Cannot be modified after creation (e.g., tuples, strings).
75. What is the difference between deep learning and machine learning in Python?
Answer:
- Machine Learning: Algorithms that learn from data and make predictions.
- Deep Learning: A subset of machine learning that uses neural networks
with multiple layers for more complex tasks like image recognition.
@my_decorator
def say_hello():
print("Hello!")
say_hello()
95. What is the difference between a lambda function and a normal function in
Python?
Answer: A lambda function is defined in a single line and can have only one
expression. Normal functions are defined using def and can have multiple
expressions.