Skip to Content
Lab 1

Modules, Packages, and PIP

Overview and Objectives

In this lab you will explore Python’s modular capabilities. You will:

  • Learn to import and use built‐in modules (such as math, random, and platform) to perform common tasks.
  • Create your own module (with functions like greet and factorial), organize it into a package, and import it into another script.
  • Use PIP to install an external package (e.g., colorama or requests) and write a small program that demonstrates its functionality.

By the end of this lab, you will be comfortable with module imports, package organization, and external library usage.

Instructions and Tasks

Task 1: Using Built-in Modules

  1. Write a Python script (e.g., builtin_usage.py) that:
    • Imports the math, random, and platform modules.
    • Generates a random integer between 1 and 100 using the random module.
    • Calculates the square root of that number using math.sqrt and rounds it down (using math.floor or by converting to an integer).
    • Retrieves and prints the system’s OS name and Python version using functions from the platform module.
  2. Your program should print descriptive messages for each output (e.g., “Random Number:”, “Square Root (floored):”, “Operating System:”, “Python Version:”).

Task 2: Creating and Importing a Custom Module

  1. Create a new Python file named utilities.py that defines at least the following functions:
    • greet(name): Returns a greeting string for the given name.
    • factorial(n): Returns the factorial of n (you may use functions from the math module if needed).
  2. Organize utilities.py into a package:
    • Create a folder named mypackage and move utilities.py inside it.
    • Include an empty __init__.py file in the mypackage folder.
  3. Write another script (e.g., use_utilities.py) that:
    • Imports your custom module using the package name (e.g., from mypackage import utilities).
    • Calls both greet and factorial functions and prints their outputs.

Task 3: Using PIP and an External Package

  1. Choose a third-party package (for example, colorama or requests).
  2. Using PIP, install the selected package.
  3. Write a script (e.g., external_package.py) that:
    • Imports the package.
    • Demonstrates a simple usage of the package. For instance, if using colorama, print a colored text message; if using requests, fetch a URL and print the HTTP status code.
  4. Include a comment at the top of your script explaining that the package must be installed (e.g., # Requires colorama; install with: pip install colorama).

Submission

Submit the following files via a link to a single GitHub repository containing all the files.:

  • builtin_usage.py
  • mypackage/utilities.py (with an accompanying empty __init__.py)
  • use_utilities.py
  • external_package.py

Include example outputs (screenshots or text logs) demonstrating that each script works as intended.

Grading Rubric

CriteriaPointsDescription
Use of Built-in Modules10 ptsProgram correctly imports and utilizes math, random, and platform modules. Outputs include a random integer, its floored square root, and system info.
Custom Module Creation and Usage15 ptsA utilities.py module is created with greet and factorial functions. The main script imports it using the package structure and calls both functions correctly.
Package Organization5 ptsThe custom module is correctly organized in a package (folder with __init__.py), and imported using the package name.
PIP Package Installation and Usage10 ptsAn external package is installed and successfully used in a script (e.g., printing colored text or performing an HTTP request) with correct and clear output.
Code Quality and Clarity5 ptsCode is well-organized, properly commented, and outputs are clearly labeled.

Total Points: 40 points

Note

Partial credit will be awarded for submissions that demonstrate a correct understanding of the concepts and correct implementation of some, but not all, required elements. If a solution is incomplete, contains minor errors, or omits one or more components of a task, evaluators may award partial points proportional to the work correctly completed. Points may be deducted for errors in logic, formatting, or missing features, but credit will be given where the underlying approach is sound and the code clearly shows an attempt to meet the rubric criteria.