Chapter 9 Assignment: Modules and Packages

Basic Level

  1. What is a module in Python?
  2. How do you import a module?
  3. What is the difference between `import math` and `from math import pi`?
  4. List any three built-in Python modules.
  5. What is the purpose of the `dir()` function when working with modules?
  6. Create a Python file named `greet.py` with a function `hello()` and import it in another file.
  7. What happens if you import a module that doesn’t exist?
  8. Write the syntax to import a module with an alias.
  9. What does `__name__ == "__main__"` do in a Python script?
  10. How can you reload a previously imported module without restarting the interpreter?

Intermediate Level

  1. Create a module `math_ops.py` with `add`, `sub`, `mul`, and `div` functions. Import and use them.
  2. Differentiate between `sys.path` and `sys.argv`.
  3. Explain the difference between absolute and relative imports with examples.
  4. How would you structure a package `calculator` with modules `basic.py` and `scientific.py`?
  5. Use the `random` module to generate a list of 5 random integers between 1 and 10.
  6. What is the significance of the `__init__.py` file?
  7. Create a package named `shapes` and write two modules: `circle.py` and `square.py`.
  8. How do you import only specific functions from a module?
  9. Write a script to list all functions available in the `math` module.
  10. Explain the difference between module-level and function-level namespaces.

Advanced Level

  1. Create a package `analytics` with sub-packages `stats` and `viz`. Organize modules accordingly.
  2. Simulate a namespace conflict using two modules and resolve it with aliases.
  3. Write a custom module that raises an exception if input is not a number.
  4. Use `importlib` to dynamically import a module at runtime.
  5. Explain how circular imports can cause problems and how to fix them.
  6. Create a script that tracks the number of times a module is imported.
  7. Use a Python package (like `pandas`) and write a short analysis on a CSV file.
  8. Write a program to explore all built-in exceptions from the `builtins` module.
  9. Demonstrate importing a private function `_hidden()` from another module.
  10. Create a plugin-based system using dynamic imports for command execution.
← Back to Modules