Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Creating Your Own Modules and Packages

Lesson 41 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Creating Your Own Modules and Packages

Creating a Custom Module

Any .py file is automatically a module. Save the following as calculator.py:

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

def subtract(a, b):
    return a - b

PI = 3.14159

Now use it from another file in the same folder:

# main.py
import calculator

print(calculator.add(5, 3))         # 8
print(calculator.subtract(10, 4))    # 6
print(calculator.PI)                  # 3.14159

# or import specific names
from calculator import add
print(add(2, 2))   # 4

name == 'main' inside a module

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

if __name__ == '__main__':
    # runs ONLY when calculator.py is executed directly,
    # NOT when it is imported by another file
    print("Testing:", add(2, 3))

---

Concept of Packages

A package is a folder containing multiple related modules, along with a special init.py file that marks it as a package.

mypackage/
    __init__.py
    shapes.py
    strings_util.py
# mypackage/shapes.py
def area_circle(r):
    return 3.14159 * r * r
# using the package from outside
from mypackage import shapes
print(shapes.area_circle(5))    # 78.53975

# or
from mypackage.shapes import area_circle
print(area_circle(5))

Why init.py?

  • Marks a directory as a Python package so it can be imported.
  • Can be empty, or can control what gets exposed when the package is imported (via all).

Module vs Package

ModulePackage
A single .py fileA folder of related modules
Imported as import module_nameImported as import package_name.module_name
Example: math, randomExample: numpy, os.path

Real-world example — organizing a project

project/
    main.py
    utils/
        __init__.py
        math_utils.py
        string_utils.py
# main.py
from utils.math_utils import add
from utils.string_utils import reverse_string