Modularity:
Modularity in Python is the design practice of breaking down a large software program into smaller, independent, and reusable sub-parts called modules.
Core Concepts of Python Modularity:
- Modules
- Packages
- Import System
Module:
- A Python module is a file containing Python definitions and statements (such as functions, classes, and variables) with a .py extension.
- It allows you to logically organize, reuse, and share your code across different programs.
Uses of modules:
- Code Reusability
- Maintainability
- Namespacing
Types of Modules
- built-in Modules: Pre-installed with Python (e.g., math, os, sys, random).
- User-defined Modules: Created by you to organize your own code.
- External Modules: Downloaded via packages managers like pip (e.g., requests, numpy).(TBD)
Create a module:
Save a file named function_demo.py
With the following content
def add(no1, no2):
return no1 + no2
def subtract(no1, no2):
return no1 - no2
def multiply(no1, no2):
return no1 * no2
def divide(no1, no2):
return no1 // no2
developer = 'nimal anand'
Import the module:
Use the import keyword in another Python file to use that code
import function_demo
result = function_demo.add(100,150)
print(result)
print(function_demo.developer)
print(type (function_demo))
Output:
250
nimal anand
class 'module'
The type() Function
Returns the exact class type of an object.
Example:
x = 10
print(type(x))
Output:
class 'int'
Common Import Variations (TBD)
- Import specific attributes: from mymodule import greet
- Import with an alias: import mymodule as mm
- Import all attributes: from mymodule import *
ModuleNotFoundError:
ModuleNotFoundError in Python means the interpreter cannot find the library or file you are trying to import.
pycache:
pycache is a directory created automatically by Python to store compiled bytecode (.pyc files).
Difference between file vs module:
duck typing language:
Duck typing is a programming style where an object's suitability is determined by the presence of specific methods and properties rather than its actual class or explicit type inheritance.
ASCII:
- ASCII stands for American Standard Code for Information Interchange.
- It is a system that gives a unique number from 0 to 127 to English letters, numbers, and symbols so computers can read text.
ISCII:
- ISCII stands for Indian Standard Code for Information Interchange.
- It maps 256 total characters, blending standard English text compatibility with native Indic alphabets.

Top comments (0)