1. What are Python’s key features? #

Answer:

  • Easy to learn and read.
  • Dynamically typed.
  • Object-oriented and functional programming support.
  • Extensive standard library.
  • Interpreted and platform-independent.
  • Used for scripting, web development, data science, and more.

2. What are Python’s data types? #

Answer:

  • Numeric: int, float, complex
  • Sequence: list, tuple, range
  • Text: str
  • Mapping: dict
  • Set: set, frozenset
  • Boolean: bool
  • Binary: bytes, bytearray, memoryview

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

Answer:

  • List: Mutable, dynamic, allows changes.
  • Tuple: Immutable, faster, used for fixed collections.

4. What is Python's GIL? #

Answer:
The Global Interpreter Lock (GIL) is a mutex in CPython that ensures only one thread executes Python bytecode at a time, limiting true parallelism in multi-threaded programs.


5. What are Python decorators? #

Answer:
Decorators are functions that modify the behavior of other functions or methods. They are applied using the @ symbol.
Example:

def decorator(func):
    def wrapper():
        print("Before function call")
        func()
        print("After function call")
    return wrapper

@decorator
def greet():
    print("Hello!")

greet()

6. What is the difference between deepcopy and copy? #

Answer:

  • copy: Creates a shallow copy, references nested objects.
  • deepcopy: Creates a deep copy, recursively copying all objects.
    Example:
import copy
a = [[1, 2], [3, 4]]
b = copy.copy(a)  # Shallow copy
c = copy.deepcopy(a)  # Deep copy

7. What are Python’s built-in data structures? #

Answer:

  • List: Ordered, mutable.
  • Tuple: Ordered, immutable.
  • Dictionary: Key-value pairs.
  • Set: Unordered, unique elements.

8. What are Python’s namespaces? #

Answer:
Namespaces are containers that hold a mapping between names and objects. Types:

  • Local: Inside a function.
  • Global: At the module level.
  • Built-in: Provided by Python (e.g., len, print).

9. How is Python different from Java? #

Answer:

  • Python is dynamically typed, while Java is statically typed.
  • Python uses indentation for blocks, Java uses braces {}.
  • Python is interpreted; Java is compiled to bytecode.

10. What are Python generators? #

Answer:
Generators produce values lazily using the yield keyword. They are memory-efficient for large data.
Example:

def generate_numbers():
    for i in range(5):
        yield i

for num in generate_numbers():
    print(num)

11. What is Python’s with statement? #

Answer:
The with statement ensures proper resource management, such as closing files.
Example:

with open('file.txt', 'r') as f:
    data = f.read()

12. What is the difference between is and ==? #

Answer:

  • is: Checks identity (whether two objects are the same).
  • ==: Checks equality of values.
    Example:
a = [1, 2]
b = [1, 2]
print(a == b)  # True
print(a is b)  # False

13. What are Python’s comprehensions? #

Answer:
Comprehensions provide a concise way to create lists, dictionaries, and sets.
Example:

# List comprehension
squares = [x**2 for x in range(5)]
# Dictionary comprehension
squared_dict = {x: x**2 for x in range(5)}

14. What is the difference between @staticmethod and @classmethod? #

Answer:

  • @staticmethod: Does not access the class or instance; behaves like a regular function inside a class.
  • @classmethod: Accesses the class as the first parameter (cls).
    Example:
class Example:
    @staticmethod
    def static_method():
        print("Static method")

    @classmethod
    def class_method(cls):
        print(f"Class method: {cls}")

Example.static_method()
Example.class_method()

15. What is Python’s self? #

Answer:
self refers to the instance of a class. It is used to access instance variables and methods.


16. What is Python’s __init__ method? #

Answer:
__init__ is the constructor method in Python, automatically called when an object is instantiated.
Example:

class Example:
    def __init__(self, name):
        self.name = name

17. What are Python’s dunder methods? #

Answer:
Dunder (double underscore) methods provide special functionality for objects. Examples:

  • __str__: String representation.
  • __repr__: Debug representation.
  • __add__: Operator overloading for +.

18. How does Python handle memory management? #

Answer:
Python uses automatic memory management with Garbage Collection (GC) to reclaim unused memory. The reference counting mechanism tracks object usage.


19. What is the difference between args and kwargs? #

Answer:

  • *args: Captures variable-length positional arguments.
  • **kwargs: Captures variable-length keyword arguments.
    Example:
def example(*args, **kwargs):
    print(args, kwargs)

example(1, 2, name="Alice", age=25)

20. What is Python’s Global Interpreter Lock (GIL)? #

Answer:
The GIL ensures that only one thread executes Python bytecode at a time, even on multi-core processors. This simplifies memory management but can be a bottleneck for multi-threading.