0% found this document useful (0 votes)
0 views3 pages

Practical 18

This document provides a step-by-step guide to create a user-defined Python package named 'calculator_package' that includes modules for addition and subtraction. It outlines the directory structure, the code for each module, and explains how to run the main program to perform basic arithmetic operations. The main.py file serves as the entry point, importing functions from the other modules to execute the calculations.

Uploaded by

dhanshripatil523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Practical 18

This document provides a step-by-step guide to create a user-defined Python package named 'calculator_package' that includes modules for addition and subtraction. It outlines the directory structure, the code for each module, and explains how to run the main program to perform basic arithmetic operations. The main.py file serves as the entry point, importing functions from the other modules to execute the calculations.

Uploaded by

dhanshripatil523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 18

*Write a python program to create and use a user defined package for
a given problem.

Step-by-Step Guide:
Create a directory structure: We will create a folder for our package and
include a couple of Python files (modules) for different functionalities.
Directory Structure:

calculator_package/
├── __init__.py
├── addition.py
├── subtraction.py
└── main.py

Create the addition.py module:


This module will contain a function for addition.

# calculator_package/addition.py

def add(a, b):


return a + b

Create the subtraction.py module:


This module will contain a function for subtraction.

# calculator_package/subtraction.py

def subtract(a, b):


return a - b
Create the __init__.py file:
This is an empty file or it can contain package-level initialization code. It's
necessary for Python to treat the directory as a package.

# calculator_package/__init__.py
# This file can be empty or include package-wide
imports

Create the main.py file:


This is the entry point of the program, where we will use our custom
package.

# calculator_package/main.py
from calculator_package.addition import add
from calculator_package.subtraction import subtract

def main():
x = 10
y = 5

print(f"The sum of {x} and {y} is: {add(x, y)}")


print(f"The difference between {x} and {y} is:
{subtract(x, y)}")

if __name__ == "__main__":
main()

How It Works:
● addition.py contains the add function that adds two numbers.
● subtraction.py contains the subtract function that subtracts
one number from another.
● main.py is where the functions are used. It imports them from the
package and performs basic operations.
● __init__.py marks the folder as a package and ensures it can be
imported.

Running the Program:

To run this program, you would execute the main.py file:

python calculator_package/main.py

Output:

The sum of 10 and 5 is: 15


The difference between 10 and 5 is: 5

You might also like