Chapter 9 Assignment: Modules and Packages
Basic Level
- What is a module in Python?
- How do you import a module?
- What is the difference between `import math` and `from math import pi`?
- List any three built-in Python modules.
- What is the purpose of the `dir()` function when working with modules?
- Create a Python file named `greet.py` with a function `hello()` and import it in another file.
- What happens if you import a module that doesn’t exist?
- Write the syntax to import a module with an alias.
- What does `__name__ == "__main__"` do in a Python script?
- How can you reload a previously imported module without restarting the interpreter?
Intermediate Level
- Create a module `math_ops.py` with `add`, `sub`, `mul`, and `div` functions. Import and use them.
- Differentiate between `sys.path` and `sys.argv`.
- Explain the difference between absolute and relative imports with examples.
- How would you structure a package `calculator` with modules `basic.py` and `scientific.py`?
- Use the `random` module to generate a list of 5 random integers between 1 and 10.
- What is the significance of the `__init__.py` file?
- Create a package named `shapes` and write two modules: `circle.py` and `square.py`.
- How do you import only specific functions from a module?
- Write a script to list all functions available in the `math` module.
- Explain the difference between module-level and function-level namespaces.
Advanced Level
- Create a package `analytics` with sub-packages `stats` and `viz`. Organize modules accordingly.
- Simulate a namespace conflict using two modules and resolve it with aliases.
- Write a custom module that raises an exception if input is not a number.
- Use `importlib` to dynamically import a module at runtime.
- Explain how circular imports can cause problems and how to fix them.
- Create a script that tracks the number of times a module is imported.
- Use a Python package (like `pandas`) and write a short analysis on a CSV file.
- Write a program to explore all built-in exceptions from the `builtins` module.
- Demonstrate importing a private function `_hidden()` from another module.
- Create a plugin-based system using dynamic imports for command execution.
← Back to Modules