Modulas and packages in python

 


Here is a clear and easy explanation of Modules in Python, perfect for beginners:


📌 Modules in Python

A module in Python is simply a file that contains Python code — functions, variables, classes, etc.
It helps you:

✔️ Organize code
✔️ Reuse code
✔️ Keep programs clean and manageable


🧩 Types of Modules

1. Built-in Modules

These come pre-installed with Python.
Example: math, random, os, datetime

import math

print(math.sqrt(25))  # Output: 5.0

2. User-Defined Modules

Modules created by you.

Example:
Create a file my_module.py

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

pi = 3.14

Now import it in another file:

import my_module
print(my_module.add(5, 3))  # Output: 8

3. Third-Party Modules

Installed using pip.

Example:

pip install numpy

Then use it:

import numpy as np
print(np.array([1, 2, 3]))

📥 Importing a Module

1. import module_name

import math
print(math.factorial(5))

2. from module import function

from math import sqrt
print(sqrt(16))

3. from module import * (not recommended)

from math import *
print(sin(1))

4. import module as alias

import math as m
print(m.pi)

📚 Why Use Modules?

✔ Avoid repeating code
✔ Easier debugging
✔ Better organization
✔ Use powerful libraries like NumPy, Pandas, Matplotlib


🔍 Checking Available Modules

help('modules')

📦 Creating and Using Your Own Module

Step 1: Create a file named calculator.py

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

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

Step 2: Import it


import calculator


print(calculator.add(10, 20))




If you want, I can also give:

📘 Notes (PDF style)


📚 Examples with output
🧩 Practice questions


Just tell me!

Here is a very clear and simple explanation of Modules and Packages in Python—perfect for beginners.


Modules and Packages in Python

📌 1. What is a Module?

A module is a single Python file (.py) that contains functions, variables, classes, or code.

✔ Example: Creating a module

Create a file named calculator.py:

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

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

Use the module in another file:

import calculator

print(calculator.add(5, 3))  # Output: 8

📦 2. What is a Package?

A package is a collection of modules inside a folder.
The folder must contain a special file named:

__init__.py

This file makes Python treat the folder as a package.

📁 Example package structure:

my_package/
    __init__.py
    calculator.py
    greetings.py

✔ Using a module inside a package:

from my_package import calculator
print(calculator.add(10, 5))

Or:

from my_package.calculator import add
print(add(10, 5))

🔍 Difference Between Module and Package

Feature Module Package
Definition Single Python file Folder containing modules
File module.py Folder with __init__.py + modules
Purpose Organize code Organize multiple modules
Example math.py numpy package

🧩 Real-Life Examples

Built-in Modules

  • math, random, os, time

Built-in Packages

  • email, xml, asyncio

Third-party Packages (installed via pip)

  • numpy
  • pandas
  • matplotlib

🔧 How to Create Your Own Package

Step 1: Create a folder

my_tools/

Step 2: Add __init__.py (can be empty)

my_tools/
    __init__.py

Step 3: Add modules

my_tools/
    __init__.py
    math_ops.py
    string_ops.py

Step 4: Use it

from my_tools.math_ops import add
print(add(4, 2))

⭐ Summary

  • Module = A single Python file.
  • Package = A folder containing multiple modules.
  • Packages help organize large programs.

If you'd like, I can also provide:

📘 Notes (PDF style)
🐍 Python programs for module & package
📝 Practice questions
📦 Real project example with packages

Just tell me!


Post a Comment

0 Comments