DEV Community

Cover image for MODULE IN PYTHON
G Gokul
G Gokul

Posted on

MODULE IN PYTHON

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:

  1. Modules
  2. Packages
  3. 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:

  1. Code Reusability
  2. Maintainability
  3. Namespacing

Types of Modules

  1. built-in Modules: Pre-installed with Python (e.g., math, os, sys, random).
  2. User-defined Modules: Created by you to organize your own code.
  3. 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'

Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output:

250
nimal anand
class 'module'

The type() Function

Returns the exact class type of an object.
Example:

x = 10
print(type(x))
Enter fullscreen mode Exit fullscreen mode

Output:
class 'int'

Common Import Variations (TBD)

  1. Import specific attributes: from mymodule import greet
  2. Import with an alias: import mymodule as mm
  3. 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:

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)