0% found this document useful (0 votes)
2 views5 pages

Unit_3_Python_Modules_Packages

The document explains Python modules and packages, highlighting their importance in organizing code for larger projects. It describes built-in and user-defined modules, how to create and access them, and outlines the structure of Python packages for better code management. Additionally, it lists popular Python packages for various applications such as statistical analysis, data visualization, deep learning, and natural language processing.

Uploaded by

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

Unit_3_Python_Modules_Packages

The document explains Python modules and packages, highlighting their importance in organizing code for larger projects. It describes built-in and user-defined modules, how to create and access them, and outlines the structure of Python packages for better code management. Additionally, it lists popular Python packages for various applications such as statistical analysis, data visualization, deep learning, and natural language processing.

Uploaded by

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

Python Modules

As our program grows bigger, it may contain many lines of code. Instead of putting everything in
a single file, we can use modules to separate codes in separate files as per their functionality.

Module is a file that contains code to perform a specific task. A module may contain variables,
functions, classes etc.

A module is simply a Python file with a .py extension that can be imported inside another Python
program.

Types of Python Modules: There are two types of python modules:


1.Built-in python modules
2.User-defined python modules

1. Built-in python modules: Python built-in modules are a set of libraries that come pre-
installed with the Python installation. These modules provide a wide range of
functionalities, from file operations and system-related tasks to mathematical
computations and web services. Some examples of Python built-in modules include “os”,
“sys”, “math”, and “datetime”. The Python standard library contains well over 200
modules. We can import a module according to our needs.
a. Python - Math Module: Some of the most popular mathematical functions are
defined in the math module.

# import standard math module


import math
# use math.pi to get value of pi
print("The value of pi is", math.pi)

• Python from...import statement: We can import specific names from a module without
importing the module as a whole.
# import only pi from math module
from math import pi
print(pi)

• Import all names: In Python, we can import all names(definitions) from a module using
the following construct:
# import all names from the standard module math
from math import *
print("The value of pi is", pi)
b. Python - OS Module: It is possible to automatically perform many operating system
tasks. Python has a built-in os module with methods for interacting with the operating
system, like creating files and directories, management of files and directories, input,
output, environment variables, process management, etc.
import os
os.mkdir("d:\\tempdir")

c. Python - Random Module: Python has a built-in module that you can use to make
random numbers.
import random
print(random.random())

2. User-defined python modules: The user-defined module is written by the user at the
time of program writing.
• Creation a User-defined Module: To create a module just write a Python code in a file
with file extension as .py

Example:
# Python Module addition- example.py
def add(a, b):
result = a + b
return result

• Accessing a User-defined Module: Now we will access the module that we created
earlier, by using the import statement.
#accessing module- module_1.py
import example
print(example.add(4,5))
Python Packages
While working on big projects, we have to deal with a large amount of code, and writing
everything together in the same file will make our code look messy. We can separate our code
into multiple files by keeping the related code together in packages.

Python packages are a way to organize and structure code by grouping related modules into
directories. A package is essentially a folder that contains an __init__.py file and one or more
Python files (modules). This organization helps manage and reuse code effectively, especially in
larger projects.

Key Components of a Python Package:


Module: A single Python file containing reusable code (e.g., math.py).
Package: A directory containing modules and a special __init__.py file.
Sub-Packages: Packages nested within other packages for deeper organization.

How to create and access packages in python:


1. Create a Directory:Make a directory for your package. This will serve as the root folder.
2. Add Modules: Add Python files (modules) to the directory, each representing specific
functionality.
3. Include __init__.py: Add an __init__.py file to the directory to mark it as a package.
4. Add Sub packages (Optional): Create subdirectories with their own __init__.py files for
sub packages.
5. Import Modules:Use dot notation to import, e.g., from mypackage.module1 import greet

Example: In this example, we are creating a Math Operation Package to organize Python code
into a structured package with two sub-packages: basic (for addition and subtraction) and
advanced (for multiplication and division).
• math_operations/__init__.py

# Initialize the main package


from .basic import addition, subtract
from .advanced import multiply, divide

• math_operations/basic/__init__.py:
from .add import addition
from .sub import subtract

• math_operations/basic/add.py:

def addition(a, b):


return a + b

• math_operations/basic/sub.py:

def subtract(a, b):


return a – b

• math_operations/advanced/__init__.py:
from .mul import multiply
from .div import divide

• math_operations/advanced/mul.py:

def multiply(a, b):


return a * b
• math_operations/advanced/div.py:

def divide(a, b):


return a / b

• package_example.py
from math_operations import addition, subtract, multiply, divide
# Perform basic operations
print("Addition:", addition(5, 3))
print("Subtraction:", subtract(10, 4))
print("Multiplication:", multiply(4, 2))
print("Division", divide(20, 4))
Python Packages Examples

Python Packages for Statistical Analysis:


• NumPy
• Pandas
• SciPy
• XGBoost
• StatsModels
• Yellowbrick
• Arch
• Dask-ML

Python Packages for Data Visualization:


• Matplotlib
• Seaborn
• Plotly
• Bokeh
• Altair
• Pygal
• Plotnine
• Dash

Python Packages for Deep Learning:


• Scikit-learn
• TensorFlow
• torch
• Keras
• Keras-RL
• Lasagne
• Fastai

Python Packages for Natural Processing Language:


• NLTK
• spaCy
• FastText
• Transformers
• fastText
• AllenNLP
• TextBlob

You might also like