Module 16: Practice & Interview Q&A
Test your understanding of modules, packages, and virtual environments with these hands-on exercises and placement preparation questions.
16.4.1 Practice Exercises
- Custom Math Module:
Create a file named
shapes.pywith functions to calculate the area of a circle (circle_area(radius)) and a square (square_area(side)). Import this module inside a separateapp.pyfile, call both functions with custom inputs, and print the results. - Local Environment Isolation:
Create a new directory named
project-demo. Open a terminal inside it, create a virtual environment namedvenv-test, activate it, installrequests, and export the details to arequirements.txtfile. Finally, deactivate the environment.
16.4.2 Placement Q&A (Interview Prep)
Click on the questions below to reveal the answers:
Q1. What is the difference between a Module and a Package in Python?
- Module: A single Python file (
.pyfile) containing variables, functions, and classes that can be imported. - Package: A folder directory that contains multiple modules, along with a special
__init__.pyfile (which can be empty) indicating to Python that the folder should be treated as an importable package.
Q2. What is PyPI, and how does it relate to pip?
- PyPI (Python Package Index): The official cloud-based public repository where Python developers publish their open-source libraries.
- pip: The CLI package manager tool that downloads and installs those libraries from the PyPI repository onto your local computer.
Q3. Why are virtual environments critical in professional Python development?
Virtual environments prevent dependency conflicts by creating isolated package installations for each project. For instance, if Project A requires an older version of a package and Project B requires a newer version, virtual environments allow both versions to coexist on the same computer without interfering with each other.
Q4. What is the role of the requirements.txt file?
A requirements.txt file acts as a manifest list of all external dependencies and versions used in a project. Running pip freeze > requirements.txt saves this list, and other developers can run pip install -r requirements.txt to instantly install the exact same packages, ensuring environment reproducibility across machines.