Monday, June 8, 2026

4.3 MODULE, PACKAGE AND LIBRARY IN PYTHON

                                          DOWNLOAD PDF

4.3 MODULE, PACKAGE AND LIBRARY IN PYTHON

Introduction

Python provides reusable code using modules, packages, and libraries. These help programmers write organized, reusable, and efficient programs.

 

Easy Memory Trick

Term

Meaning

Example

Module

Single Python file

math.py

Package

Folder containing modules

numpy.random

Library

Collection of packages and modules

NumPy, Pandas

 

Easy Analogy

Library = House
Package = Room
Module = Book

A house contains rooms, and rooms contain books.

Similarly:

Library → Packages → Modules

 

MODULE

Definition

A module is a single Python file (.py) containing reusable code such as functions, classes, variables, and statements.

Modules help reduce repetition and make programs organized.

 

Types of Modules

 

1. Built-in Module

A built-in module is a module already provided by Python that contains predefined functions, classes, and variables to perform specific tasks.

These modules are available automatically after Python installation.

 

Examples

·         math

·         random

·         os

·         sys

·         datetime

 

Example Program

import math
 
print(math.sqrt(25))

 

Output

5.0

 

2. User-defined Module

A user-defined module is a module created by the programmer to store reusable code such as functions, classes, and variables in a separate Python file.

Example

# calculator.py
 
def add(a, b):
    return a + b

Using the Module

import calculator
 
print(calculator.add(5, 3))

 

Output

8

 

Advantages of Modules

1.      Reuse code multiple times

2.      Reduce code duplication

3.      Make programs organized

4.      Simplify testing and debugging

5.      Improve readability

 

Methods of Importing Modules

 

1. Import Entire Module

import math

Example

print(math.factorial(5))

Output

120

 

2. Import Specific Function

from math import sqrt

Example

print(sqrt(49))

Output

7.0

 

3. Import Module Using Alias

An alias is an alternative short name given to a module during import using the as keyword.

Example

import math as m
 
print(m.sqrt(64))

Output

8.0

 

4. Import all functions from a module

Importing all functions from a module means importing every function, variable, and object from a module into the current program using the * symbol.

 

from module_name import *

 

Example:

 

from math import *

 

print(sqrt(25))

print(factorial(5))

 

Output

5.0
120

 

Important Point

After importing with *, functions can be used directly without writing the module name.

Example:

sqrt(25)

Instead of:

math.sqrt(25)

 

PACKAGE

Definition

A package is a collection of related Python modules organized inside a folder or directory.

Packages help organize large Python projects systematically.

 

Structure of a Package

calculator/
├── add.py
├── subtract.py
└── __init__.py

 

init.py File

__init__.py is a special file used to indicate that a directory should be treated as a Python package.

 

Advantages of Packages

1.      Groups related modules together

2.      Organizes large projects

3.      Improves code management

4.      Avoids naming conflicts

5.      Supports hierarchical structure

 

Example of Package Import

from calculator.add import add

 

Important Points About Packages

·         Contains one or more modules

·         Usually includes __init__.py

·         Can contain sub-packages

·         Provides hierarchical organization

 

 

 

LIBRARY

Definition

A library is a collection of modules and packages that provides reusable functionality for specific tasks.

Libraries help programmers perform complex tasks easily and quickly.


Types of Libraries

1. Standard Library

A standard library is a collection of built-in modules that are automatically available with Python installation.

Examples

·         math

·         random

·         os

·         datetime


2. External Library

An external library is a library that is not included with Python by default and must be installed separately using pip.

Examples

·         NumPy

·         Pandas

·         Matplotlib

·         TensorFlow

·         OpenCV

·         Django


Popular Python Libraries

Library

Main Use

NumPy

Numerical Computing

Pandas

Data Analysis

Matplotlib

Data Visualization

Requests

API Calls

TensorFlow

Machine Learning

OpenCV

Computer Vision

Django

Web Development


pip

Definition

pip is Python’s package manager used to install, update, and remove external packages and libraries.


Common pip Commands

Install Package

pip install numpy

Uninstall Package

pip uninstall numpy

Show Installed Packages

pip list

Check pip Version

pip --version

Advantages of pip

1.      Installs external libraries easily

2.      Updates packages

3.      Removes unwanted packages

4.      Manages dependencies

5.      Simplifies package management


📊 Difference Between Module, Package and Library

Feature

Module

Package

Library

Meaning

Single Python file

Collection of modules

Collection of packages and modules

Size

Smallest

Medium

Largest

Form

.py file

Folder/Directory

Complete collection

Purpose

Reuse code

Organize modules

Provide complete functionality

Example

math.py

numpy.random

NumPy


Common Exam Mistakes

❌ Confusing module and package
❌ Writing package as a single file
❌ Forgetting .py extension in module definition
❌ Writing pip as a library instead of package manager


Final Summary

A module is a single Python file containing reusable code. A package is a collection of related modules stored inside a folder. A library is a collection of modules and packages that provides reusable functionality for developers.


 

4.2 USER-DEFINED FUNCTIONS IN PYTHON

                                          DOWNLOAD PDF

4.2 USER-DEFINED FUNCTIONS IN PYTHON

What is a Function?

A function is a block of organized and reusable code designed to perform a specific task.

Functions help reduce repetition and make programs easier to manage.

 

Advantages of Functions

v  Reuse code multiple times.

v  Reduce code duplication.

v  Improve program readability.

v  Make debugging easier.

v  Support modular programming.

 

Modularity

Modularity means dividing a large program into smaller and manageable parts called functions or modules.

 

Types of Functions in Python

There are two types of functions:

v  Built-in Functions

v  User-defined Functions

 

1. Built-in Functions (Library Functions)

Functions already provided by Python are called built-in functions.

These functions are automatically available.

 

Common Built-in Functions

Function

Purpose

Examples

print( )

Displays output

print("Hello")

input( )

Takes input

 

len( )

Returns length

len("Python")

int( )

Converts into integer

int("5")

sum( )

Calculates total

sum([1,2,3])

 

2. User-defined Functions

A function created by the programmer using the def keyword is called a user-defined function.

Example

def greet( ):
    print("Hello")

 

Difference Between Built-in and User-defined Functions

Feature

Built-in Function

User-defined Function

Creator

Python Developers

Programmer

Availability

Available by default

Must be created

Loading

Loaded automatically

Loaded when defined

Example

print( )

add( )

Creating a User-defined Function

Syntax

def function_name(parameters):
    statements
    return value

 

Example Program

def add_numbers(x, y):
    total = x + y
    return total

print(add_numbers(5, 6))

Output

11

 

Important Rules of Functions

v  Function definition starts with def

v  Function header ends with colon :

v  Indentation is compulsory

v  Function name should be meaningful

v  Parameters are optional

v  Return statement is optional

 

Return Statement

A statement used to send a value back from a function.

Syntax

return value

 

Function Returning Value

Example

def area(length, width):
    return length * width

result = area(5, 8)

print(result)

Output

40

 

Parameters and Arguments

 

Parameter

A variable written inside the function definition is called a parameter.

Example

def area(radius):

radius is a parameter.

 

 

 

Argument

The actual value passed during the function call is called an argument.

Example

area(5)

5 is an argument.

 

Difference Between Parameter and Argument

Parameter

Argument

Placeholder

Actual value

Used in definition

Used in function call

Variable

Value

 

Example

def add(x, y):
    return x + y

add(5, 3)

Item

Type

x, y

Parameters

5, 3

Arguments

 

Scope of Variables

Scope determines where a variable can be accessed.

Types of Scope

v  Local Scope

v  Global Scope

 

Local Scope

Variables declared inside a function and accessible only within that function.

Example

def test():
    num = 5
    print(num)

test( )

 

Global Scope

Variables declared outside functions and accessible throughout the program.

Example

num = 10

def show( ):
    print(num)

show( )

 

 

 

Passing Arguments

Python supports three types of arguments.

v  Positional Arguments

v  Default Arguments

v  Keyword Arguments

 

1. Positional Arguments

Arguments are passed in the same order as parameters.

Example

def check(a, b, c):
    print(a, b, c)

check(2, 5, 7)

Output

2 5 7

 

2. Default Arguments

Parameters are assigned default values.

Example

def power(base, exp=2):
    return base ** exp

print(power(4))
print(power(2,3))

Output

16
8

 

Important Rule for Default Arguments

✅ Required parameters first
✅ Default parameters last

 

Legal Function

def interest(prin, time, rate=0.10):

Illegal Function

def interest(prin, time=2, rate):

❌ Error occurs because default arguments must come after required arguments.

 

3. Keyword Arguments

Arguments are passed using parameter names.

Order does not matter.

Example

def interest(prin, time, rate):
    pass

interest(rate=0.10, prin=2000, time=2)

 

 

Types of Functions Based on Return Value

v  Void Function

v  Non-Void Function

 

Void Function

A function that does not return any value.

Example

def greet( ):
    print("Hello")

Python automatically returns None.

Example

def greet():
    print("Hello")

a = greet( )

print(a)

Output

Hello
None

 

Non-Void Function

A function that returns a value.

Example

def add(x, y):
    return x + y

result = add(5,3)

 

Returning Multiple Values

Python can return multiple values using one return statement.

Example

def squared(x, y, z):
    return x*x, y*y, z*z

print(squared(2,5,7))

Output

(4, 25, 49)

 

Example with Variables

def squared(x, y, z):
    return x*x, y*y, z*z

a, b, c = squared(2,3,4)

print(a, b, c)

Output

4 9 16

 

Common Exam Mistakes ❌

v  Forgetting colon : after function definition

v  Wrong indentation

v  Confusing parameter and argument

v  Writing default arguments before required arguments

v  Forgetting to call the function

 

Most Important Programs for SEE Exam

Program 1: Add Two Numbers

def add(a, b):
    return a + b

print(add(5, 3))

 

Program 2: Find Area of Rectangle

def area(length, width):
    return length * width

print(area(5, 8))

 

Program 3: Default Argument

def power(base, exp=2):
    return base ** exp

print(power(4))

 

Program 4: Keyword Argument

def student(name, age):
    print(name, age)

student(age=15, name="Ram")

 

Program 5: Local and Global Variable

x = 10

def show( ):
    y = 5
    print(x)
    print(y)

show( )