Interview Questions on Python.
- What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability.
- What are Python's key features?
Python is dynamically typed, interpreted, object-oriented, easy to learn, and has extensive libraries.
- What is PEP 8?
PEP 8 is a style guide for Python that provides coding conventions to improve code readability.
- What are Python's built-in data types?
Common data types include int, float, str, list, tuple, set, and dict.
- What is a Python list?
A list is a mutable, ordered collection that can store different data types.
- What is a tuple?
A tuple is an immutable, ordered collection in Python.
- What is the difference between list and tuple?
Lists are mutable, while tuples are immutable.
- What is a dictionary in Python?
A dictionary is an unordered collection of key-value pairs.
- What are Python sets?
Sets are unordered collections of unique elements.
- What is the difference between deep copy and shallow copy?
A shallow copy copies references, while a deep copy creates independent objects.
- What are Python functions?
Functions are reusable blocks of code that perform a specific task.
- What is lambda in Python?
Lambda is an anonymous function that can have multiple arguments but only one expression.
- What is the difference between *args and **kwargs?
*args passes variable-length positional arguments, **kwargs passes variable-length keyword arguments.
- What is a Python module?
A module is a file containing Python code that can be reused.
- What is a Python package?
A package is a collection of related Python modules.
- How does Python handle memory management?
Python has automatic memory management, including garbage collection.
- What is the difference between is and ==?
is checks object identity, while == checks value equality.
- What is an iterator?
An iterator is an object that implements the __iter__() and __next__() methods.
- What is a generator?
A generator is an iterator that yields values using the yield keyword.
- What is the difference between a generator and an iterator?
Generators are created using functions and yield, while iterators use __iter__() and __next__().
- What is the difference between append() and extend()?
append() adds an element, while extend() merges two lists.
- What is list comprehension?
List comprehension is a concise way to create lists using a single line of code.
- What is a decorator?
A decorator is a function that modifies another function's behavior.
- What is monkey patching?
Monkey patching dynamically modifies a module or class at runtime.
- What is the difference between static, class, and instance methods?
Static methods don’t access instance or class attributes, class methods access class attributes, and instance methods access instance attributes.
- What is the difference between mutable and immutable types?
Mutable types can be modified (lists, dicts), while immutable types cannot (tuples, strings).
- What are Python exceptions?
Exceptions are errors detected during execution.
- How do you handle exceptions?
Using try-except blocks.
- What is the difference between throw and raise?
Python uses raise to trigger exceptions, not throw.
- What are Python file handling modes?
r (read), w (write), a (append), rb (read binary), wb (write binary).
- What is a context manager?
It manages resources using the with statement.
- What is __init__.py?
It indicates a directory is a Python package.
- What are Python metaclasses?
Metaclasses define class behavior.
- What is multithreading in Python?
Multithreading allows concurrent execution using threads.
- What is GIL (Global Interpreter Lock)?
GIL ensures only one thread executes Python bytecode at a time.
- What is a lambda function?
A lambda function is an anonymous function defined with the lambda keyword.
- What is the difference between map(), filter(), and reduce()?
map() applies a function, filter() filters elements, and reduce() reduces values using a function.
- How do you perform unit testing in Python?
Using the unittest module.
- What is logging in Python?
Logging helps track events and errors.
- What is the difference between Python 2 and Python 3?
Python 3 has improved syntax and better Unicode support.