COMPLETE BLOG ON MODULARIZATION AND PACKAGES IN PYTHON – 11

Packages And Modules In Python PACKAGES
0
0

OVERVIEW

In this blog, you will learn,

  • What are modules and packages?
  • How to create modules in python?
  • How to create packages in python?
  • what is the link between modules and packages?

WHAT ARE MODULES AND PACKAGES?

Python Modules: In python a module is a file that contains the python statements and definitions. In other words you can say that a python modules is a file that will contain only the functions or classes that may not perform anything in the same file on it’s execution, but it will execute the functionality of a class or a function when it will be called. The other advantage of having a module is that a function or a class that we have in a one module (say test1.py) can be called in other module or a script (say test2.py). To call any module import keyword is used.

Python Packages: In python, a package is a like a directory that holds multiple modules and subpackages. It’s like we can call any function or any class form any of the modules with respect to the package we’ll create. To call any module through package from keyword is used.

CREATING MODULES IN PYTHON

Now , let’s understand how to create a module in python and to further use it. To create a module let’s take an example to create a module to perform the arithmetic operations.

For more clarification also check this video:

Code

def addition(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def divide(num1, num2):
    return num1 / num2

def multiply(num1, num2):
    return num1 * num2

def modulus(num1, num2):
    return num1 % num2
from arithmetic import *

num1=float(input("Enter the value for number 1: "))
num2=float(input("Enter the value for number 2: "))

print("Addition of num1 and num2 is", addition(num1, num2))
print("Subtraction of num1 and num2 is", subtract(num1, num2))
print("Divide of num1 and num2 is", divide(num1, num2))
print("Multiply of num1 and num2 is", multiply(num1, num2))
print("Modulus of num1 and num2 is", modulus(num1, num2))

Output for the above code will be,

Enter the value for number 1:  5
Enter the value for number 2:  4
Addition of num1 and num2 is 9.0
Subtraction of num1 and num2 is 1.0
Divide of num1 and num2 is 1.25
Multiply of num1 and num2 is 20.0
Modulus of num1 and num2 is 1.0
Output for modules in python

From the above code you can understand that we have created two scripts one with the name arithmetic.py and the second one with the name calculator.py. Here arithmetic.py is treated as a module as it only contains the functions and nothing else. calculator.py is a main script that will execute our code. This will call the functions from the module that we created and will generate the output.

CREATING PACKAGES IN PYTHON

So this is how the module is created. Now let’s understand how we can convert these modules to package. To create a package in python, the simple way is to create a new directory and name it. Then, create an empty script with the name __init__.py, now this directory will act as a package and you can create or copy one or more modules to this package. Below is a structure of a package named “checking” which contains two modules arithmetic.py and Adam.py. So in this way we’ll be converting a directory to a package.

checking__init__.py
arithmetic.py
Adam.py

Here is the code for finding the adam number,

Adam Number

def adam_number(num):
    out=int(str(int(int(str(num**2)[::-1])**0.5))[::-1])
    if num==out:
        return True
    else:
        return False
from checking.Adam import adam_number
from checking.arithmetic import *

num=int(input("Enter the number to find the Adam number: "))
if adam_number(num)==True:
    print(f"{num} is an Adam number.")
else:
    print(f"{num} is not an Adam number.")

num1=float(input("Enter the value for number 1: "))
num2=float(input("Enter the value for number 2: "))

print("Addition of num1 and num2 is", addition(num1, num2))
print("Subtraction of num1 and num2 is", subtract(num1, num2))
print("Divide of num1 and num2 is", divide(num1, num2))
print("Multiply of num1 and num2 is", multiply(num1, num2))
print("Modulus of num1 and num2 is", modulus(num1, num2))

Output for the above code with respect to the packages is as shown here:

Enter the number to find the Adam number: 12
12 is an Adam number.
Enter the value for number 2:  4
Addition of num1 and num2 is 9.0
Subtraction of num1 and num2 is 1.0
Divide of num1 and num2 is 1.25
Multiply of num1 and num2 is 20.0
Modulus of num1 and num2 is 1.0
Output for packages in python

LINK BETWEEN MODULE AND PACKAGE

From the above examples now I think you have got an idea that what is the link between module and a package. In simple words we can say that a module is a single unit and package is collection of these single units.

Time to wrap up now. Hope you liked our content on FUNCTIONS IN PYTHON. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python contents.

Also check out our other playlist Rasa ChatbotInternet of thingsDockerPython Programming, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning.

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

Leave a Reply