Interview Questions on Python.

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