Package in Python

Write steps to create a package. Apply these steps to create a package named volume and create 3 modules in it named cube, cuboid and sphere, having function to calculate volume of the cube, cuboid and sphere respectively. Import the modules defined in the package and use the defined functions for calculation of volume respectively.


Steps to Create a Package

  1. Create a Directory for the Package: Create a new directory named volume.
  2. Create __init__.py File: This file makes Python treat the directory as a package.
  3. Create Modules: Inside the volume directory, create three Python files: cube.py, cuboid.py, and sphere.py.
  4. Define Functions in Each Module: Write functions to calculate the volume of a cube, cuboid, and sphere in their respective modules.
  5. Import the Package and Use the Functions: Create a separate script to import the modules and use the functions.

Directory Structure

volume/

├── __init__.py
├── cube.py
├── cuboid.py
└── sphere.py


Step 1: Create the Directory for the Package

You can create the directory structure manually or use a terminal command.

Step 2: Create __init__.py File

Create an empty file named __init__.py in the volume directory.

Step 3: Create Modules and Define Functions

cube.py

def volume_of_cube(side):
    """Calculate the volume of a cube."""
    return side ** 3

cuboid.py

def volume_of_cuboid(length, width, height):
    """Calculate the volume of a cuboid."""
    return length * width * height

sphere.py

import math

def volume_of_sphere(radius):
    """Calculate the volume of a sphere."""
    return (4/3) * math.pi * (radius ** 3)


Step 4: Import the Package and Use the Functions

Create a separate Python file, for example, main.py, to import and use the functions:


# main.py

# Importing the modules from the volume package
from volume.cube import volume_of_cube
from volume.cuboid import volume_of_cuboid
from volume.sphere import volume_of_sphere

# Example calculations
if __name__ == "__main__":
    # Volume of a cube with side length 3
    cube_side = 3
    print(f"Volume of the cube: {volume_of_cube(cube_side)}")

    # Volume of a cuboid with dimensions 2, 3, 4
    cuboid_length = 2
    cuboid_width = 3
    cuboid_height = 4
    print(f"Volume of the cuboid: {volume_of_cuboid(cuboid_length, cuboid_width, cuboid_height)}")

    # Volume of a sphere with radius 5
    sphere_radius = 5
    print(f"Volume of the sphere: {volume_of_sphere(sphere_radius)}")

Explanation of the Code

  1. Functions in Modules: Each module defines a function that takes necessary parameters and calculates the volume of the corresponding shape.
  2. Import Statements: In main.py, the functions are imported from their respective modules.
  3. Calculating Volumes: The main.py file demonstrates how to use the functions to calculate and print the volumes of a cube, cuboid, and sphere.

Running the Code

To run the program, execute main.py:

bash
python main.py

Expected Output

Volume of the cube: 27
Volume of the cuboid: 24
Volume of the sphere: 523.5987755982989


  • To Share this Blog, Choose your plateform


Write your Testimonial

Your review is very precious for us.


Rating: