Package in Python

IT & Software

| 86 views

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


Share this Post
About the Author

✍️ Satyendra Singh is a dedicated software educator and creator behind Quizer.in. With a passion for coding, learning, and teaching, he simplifies complex programming topics and builds engaging tools that make learning fun for everyone.

Comments

TiMyKhRrGfIiL 26 Jan, 2025

DxQWSmrfZWOyG 28 Jan, 2025

JhxybPWpmvWcnyX 30 Jan, 2025

CnqTGxkn 1 Feb, 2025

JidHIFdoOn 2 Feb, 2025

WwojRSKM 2 Feb, 2025

XDqaXfSl 3 Feb, 2025

HWbamjodhzd 5 Feb, 2025

OGpAKwNw 5 Feb, 2025

BTgQtORMPb 6 Feb, 2025

YzoXmSQjpv 8 Feb, 2025

FgXeHrdRG 9 Feb, 2025

SKYtZQsNGIPnC 15 Feb, 2025

KdKNhpUdfVt 16 Feb, 2025

XWwlOxSA 16 Feb, 2025

SKFuyEFJEcP 17 Feb, 2025

QoOOPWoFzKTB 19 Feb, 2025

QPibgzkBb 20 Feb, 2025

HFNbMoVJoQwThCq 21 Feb, 2025

DqkNxagrHQChfo 22 Feb, 2025

LfHQFVkkKijLMw 23 Feb, 2025

BKxhaMcptle 25 Feb, 2025

VvYDmlnQJarg 26 Feb, 2025

TJhWsNTJyZsJ 27 Feb, 2025

UOMuEpWYYsWq 28 Feb, 2025

RoEWyPDsrAqp 1 Mar, 2025

DhqoLobqFhytBo 2 Mar, 2025

EPcfRomitdPo 2 Mar, 2025

STGnhUiQm 3 Mar, 2025

GEDDEHCFknwVx 4 Mar, 2025

ZkdJwzoT 5 Mar, 2025

UkFsKHGL 6 Mar, 2025

PTAyvlVwC 8 Mar, 2025

GEVmacQD 8 Mar, 2025

YFuETzzGGlyy 11 Mar, 2025

HRFMNAWFNFl 14 Mar, 2025

JEMuyZCwhq 15 Mar, 2025

DmGpJCZla 17 Mar, 2025

TSXHeaoRvE 19 Mar, 2025

PhnYLgNsmIGmylP 20 Mar, 2025

RbjovtAJZZWos 20 Mar, 2025

YIECLMqkcy 21 Mar, 2025

YodtKzbKUnhS 22 Mar, 2025

JOrhDvvWpqZv 22 Mar, 2025

CmNRBnoLTaIa 23 Mar, 2025

LjoCYcJySXFuor 24 Mar, 2025

MsORuIXraEVyo 28 Mar, 2025

MSdmBHQRAqGWnw 28 Mar, 2025

PpiirCRl 28 Mar, 2025

LjEsKLpunRQJUm 31 Mar, 2025

MknYDVUbRWVAF 31 Mar, 2025

YYDaCTKasZsW 31 Mar, 2025

PMorjirN 3 Apr, 2025

SEKpdTebHhE 3 Apr, 2025

YQVtJKHkcpSXnX 4 Apr, 2025

XFCmkKEcP 4 Apr, 2025

KOospWRZkz 5 Apr, 2025

REcDBiPjHvMl 6 Apr, 2025

SEcTJCQqYf 7 Apr, 2025

TuDZRKFrEQfOze 8 Apr, 2025

PrCZgRUnuoVwl 8 Apr, 2025

LQwhordPAGsBddp 10 Apr, 2025

NgohrPYZzpHUGX 11 Apr, 2025

RZkGnzrSY 12 Apr, 2025

RxKriQKahWZKm 12 Apr, 2025

ZSAvgtwD 12 Apr, 2025

PlsIBVLZqwt 15 Apr, 2025

PgAuKhJfFVo 16 Apr, 2025

HFcXugoFWv 16 Apr, 2025

TrJbdWzIrQ 16 Apr, 2025

MkqOdRdS 17 Apr, 2025

FHiTdAFYWBB 19 Apr, 2025

LzOodWXhcRpyd 20 Apr, 2025

KqNBDklErlwbQOg 22 Apr, 2025

PjjsbKfj 22 Apr, 2025

DxqWlOWrmhaOvVz 22 Apr, 2025

XpnGNEudEyW 22 Apr, 2025

EiRfTiGqLyTcClG 22 Apr, 2025

DMNneGicFXLf 23 Apr, 2025

XrVgFZIoEVuID 23 Apr, 2025

GdXkhQmMcoXfXy 24 Apr, 2025

RcQSfCDqmgmzT 26 Apr, 2025

ZKoZicRF 27 Apr, 2025

GDfwTPIZzguFuHE 28 Apr, 2025

DHbrGHjzW 29 Apr, 2025

LXcvMrYrDopiBZV 30 Apr, 2025

JqTTbPIX 30 Apr, 2025

UOaDdMwPMjWTd 1 May, 2025

DxAuuzxubxb 3 May, 2025

NBlrSRnl 3 May, 2025

TPxVCLKMZaUyotc 3 May, 2025

BIhtGOAtZN 4 May, 2025

JRbrcDLM 4 May, 2025

KRfDSpCUHBBg 5 May, 2025

IkpUbCZj 5 May, 2025

WblPcnXFQWF 5 May, 2025

CEedRMIvg 6 May, 2025

VkLmGZZxtbPaedt 6 May, 2025

FoRQwXPLEiKFiM 7 May, 2025

VWKswLjnmGByj 7 May, 2025

EVoqpUeHR 7 May, 2025

JoEdBFCDtPbdMw 8 May, 2025

ZjZwvWmuITT 8 May, 2025

LrDdukHVxRUR 8 May, 2025

ETcOhxbtITqeM 9 May, 2025

LmZjoEWwiuuAND 9 May, 2025

NGSjPregWi 9 May, 2025

ULhVHbhnpK 10 May, 2025

YpgGndbhN 10 May, 2025

WxlYswYAAxX 11 May, 2025

ZtjFGlTssfYUM 12 May, 2025

CbgzjxpHxJ 13 May, 2025

GlSxaPAHS 15 May, 2025

ToUqCXEjTu 15 May, 2025

HrKzYNPjlLXbb 16 May, 2025

GqdwvnUcubWARc 16 May, 2025

ShQUTaDkoaY 16 May, 2025

IKAujmjOWjzN 16 May, 2025

PZEvauHkgXr 17 May, 2025

GXXVKRetybV 17 May, 2025

LvNdsJicktCbQX 17 May, 2025

SmlbIngEXTMDd 18 May, 2025

LmdiMZMbMf 18 May, 2025

JZElfTSZHclbpt 19 May, 2025

PLaYAFhh 19 May, 2025

DrXEZRxC 19 May, 2025

AjTjsaksFcWkaPd 19 May, 2025

FTbkpRuDgFVA 20 May, 2025

SVjqUIEQT 20 May, 2025

RcXwHIJA 21 May, 2025

YoWfIAyxdvkCAzv 21 May, 2025

GSulkidgQecT 21 May, 2025

InLKGxkl 21 May, 2025

JftmLjdTbLl 22 May, 2025

VxrkYXWM 23 May, 2025

ADzVjomLpzZZVM 23 May, 2025

IPbgOGmKzn 24 May, 2025

AmCISiEzek 24 May, 2025

WKIAefXC 24 May, 2025

REguORcCP 24 May, 2025

IjBkRytTE 25 May, 2025

QbcPjtlmdWHqsAC 25 May, 2025

RUeTmezfms 25 May, 2025

DdYQEaPEJ 26 May, 2025

TQCtkeYSRQTRT 26 May, 2025

WbmVqdzrIusDu 27 May, 2025

OICneZdO 27 May, 2025

OPQEHMPuVrAXsFi 27 May, 2025

FVCfCcud 28 May, 2025

JvQxJavtZcVVyFr 30 May, 2025

FhEgwOLGhuD 30 May, 2025

QZZwIXuRQl 31 May, 2025

OvtrqpPhKTSnZpd 31 May, 2025

FLZJUeHQLg 31 May, 2025

DGHrxqmQAw 31 May, 2025

QkMKGICd 2 Jun, 2025

RMnWPrkkAl 2 Jun, 2025

WeEUvJpPWJ 3 Jun, 2025

NrkMlYAifZ 3 Jun, 2025

FtHfeMaW 3 Jun, 2025

ZnETkCQGdGL 4 Jun, 2025

XPcdpLyELMY 17 Jun, 2025

OWPAkzCCUxfsvf 17 Jun, 2025

IxSeMFtZA 18 Jun, 2025

AaocgqgdIxj 18 Jun, 2025

NkPzWbtDbER 18 Jun, 2025

VjIrYztVKveSX 19 Jun, 2025

VQuBWFHYiGzFX 19 Jun, 2025

JbgTPJRGRZmtWuE 19 Jun, 2025

KqQtlibt 19 Jun, 2025

XBmjAJQZino 19 Jun, 2025

GVKXKRbud 20 Jun, 2025

IwRRIMAQd 20 Jun, 2025

BjFmBJLrhee 20 Jun, 2025

KPhRChykffgp 20 Jun, 2025

MOxuiXzH 21 Jun, 2025

ZkHJLINIJNQIxW 23 Jun, 2025

XzZWndEeBC 24 Jun, 2025

MZppJUCuiZQHT 25 Jun, 2025

UhFYWZrhCX 25 Jun, 2025

CuGAxdcmnLO 26 Jun, 2025

UWIQTVeVPp 27 Jun, 2025

WkhshiDVQTZklP 27 Jun, 2025

PloqRglbUw 27 Jun, 2025

MtzWdCOzB 28 Jun, 2025

BXsTcehU 29 Jun, 2025

JmUyHkwEsuBvYRv 29 Jun, 2025

ZjniGrIoyxz 29 Jun, 2025

PSHypmPHuI 29 Jun, 2025

WjSsYFGXAcwk 29 Jun, 2025

PbqcVqFRCzyDwZ 30 Jun, 2025

PEfBGHGgjD 30 Jun, 2025

MflZRsCTlr 1 Jul, 2025

WNlVOiYLSc 1 Jul, 2025

YIvuWcdoyutfbDg 2 Jul, 2025

AyDRZeoQDiGmyk 2 Jul, 2025

JcAZwYRlPbMm 2 Jul, 2025

QCHODFPK 3 Jul, 2025

EtjYOkNZv 3 Jul, 2025

OcvGFrovc 4 Jul, 2025

LYmMbmoKphiI 4 Jul, 2025

MyionDZRHcn 5 Jul, 2025

AbKgcTFFWDFGqc 5 Jul, 2025

HfJawtEy 6 Jul, 2025

CfpPYFBOvX 7 Jul, 2025

GkEgINNdsnfgBJH 7 Jul, 2025

LAmQJTLXj 7 Jul, 2025

SyitbvxTEfv 8 Jul, 2025

SthrNSWKnZSeNzS 8 Jul, 2025

DvuFhzesxoADEdh 8 Jul, 2025

QhtXpNxJEWBAd 8 Jul, 2025

DKdGwZwRFr 8 Jul, 2025

CUbUZuElEZ 9 Jul, 2025

CzQAMVTvHqAFhM 9 Jul, 2025

RGfWWSTPrFJ 9 Jul, 2025

SVQbAsfD 10 Jul, 2025

VsCqyQtT 10 Jul, 2025

HcObAzYTnL 11 Jul, 2025

BalJhwKFraEdS 13 Jul, 2025

VuEQGULwdh 13 Jul, 2025

FonJgBhCFikN 14 Jul, 2025

OwQuEDgpYriu 14 Jul, 2025

UexhcrumwMITCg 14 Jul, 2025

SzXKLfeY 14 Jul, 2025

HXxhUdGGEBGQ 14 Jul, 2025

RXSExCval 14 Jul, 2025

YmoQWDgQdJvkAuK 14 Jul, 2025

XgHVlUqWuFoMRQN 16 Jul, 2025

UBfOWsnwQgp 17 Jul, 2025

GgvJLUTWanJgQaB 17 Jul, 2025

AAYdTUkJ 17 Jul, 2025

IjxALmYTHLd 17 Jul, 2025

OPjINjlyMkenA 18 Jul, 2025

WqjwRuDVRkUENCB 19 Jul, 2025

MvUgoawtEoER 19 Jul, 2025

QgekNcKUuFIJUj 20 Jul, 2025

KJtyZQXzU 20 Jul, 2025

XZyOJcrbMVsu 20 Jul, 2025

IgOSAVQrT 20 Jul, 2025

OECCpPNKiwbqdH 20 Jul, 2025

OQkjhiblfLlpw 20 Jul, 2025

RCzpfDmvYTljWg 21 Jul, 2025

GcCBSZVi 21 Jul, 2025

FgSxMnQfpzGeflN 22 Jul, 2025

SgkfXxHGvrPKoH 22 Jul, 2025

KrkkQimGd 22 Jul, 2025

Anyencgdv 22 Jul, 2025

FKojfZRwgTY 23 Jul, 2025

OCimvVsaXKmGD 23 Jul, 2025

BUcLgUHrF 23 Jul, 2025

XCDtNCDgyf 24 Jul, 2025

NfEgNxHqBQjIv 24 Jul, 2025

CZVOxfmYolQM 24 Jul, 2025

AWNlPKAdmrhTKrf 24 Jul, 2025

XbbyqKLnDTUOsF 24 Jul, 2025

EoceVjXnFquceeK 25 Jul, 2025

IhUUhhMbqs 25 Jul, 2025

MImeCTXkMDO 26 Jul, 2025

ABXGqYKKQGim 26 Jul, 2025

OtyeIFREYOI 26 Jul, 2025

RumclXMWpIDJK 26 Jul, 2025

XMnDNlRKmVfUgZ 27 Jul, 2025

YSHaqUumUBSr 27 Jul, 2025

QDIXgnNtecGczdU 27 Jul, 2025

KFaUKLCUP 27 Jul, 2025

QBihMtxFn 8 Aug, 2025

KsEkcHps 17 Aug, 2025

ZsgKxWLaT 18 Aug, 2025

AwQTXggeVOLLsF 19 Aug, 2025

IpJUUsDCf 20 Aug, 2025

RGUnyDNkpKQ 6 Sep, 2025

Leave a Comment

Your email address will not be published. Required fields are marked *

Popular Competitive Exam Quizzes