Educational Articles

Package in Python

Read the important current affairs of 15 July 2026 for SSC, Banking, UPSC, Railway and all competitive exams.

17 Oct 2024 5 Min Read Quizer Team 143 Views

17

October 2024
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


Why This Content Matters

This content is prepared to help students and competitive-exam aspirants understand important topics, revise key information, and strengthen their exam preparation.

  • Quickly revise important facts and concepts
  • Improve General Knowledge and exam awareness
  • Support preparation for competitive and government exams
  • Build a consistent and effective study routine
Quizer Team
About the Author

Quizer Team

The Quizer Team creates exam-focused educational content, current affairs, general knowledge, study notes, and preparation resources to help students and competitive-exam aspirants learn, practice, and stay updated.

Educational Content Exam Focused Learning Resources

📢 Join Our WhatsApp Channel

Get Daily GK, Current Affairs, Amazing Facts & Quiz Updates.

🚀 Join Now

Join Monthly Challenge

Compete, Learn & Win exciting prizes.

  • Cash Rewards
  • E-Certificates
  • Leaderboard Ranking
  • Daily Practice
Join Now

Related Post

Educational Articles
17 Oct 2024 146
Read More
Educational Articles
17 Oct 2024 201
Read More
Educational Articles
16 Oct 2024 186
Read More