Python Modules by Naresh I Technologies

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Python modules are files containing Python code (functions, classes, and variables) that can be imported and reused in other programs. They help keep code organized, reusable, and efficient.

1️⃣ Types of Python Modules

Python modules are categorized into three main types:

1. Built-in Modules (Pre-installed)

  • Python has several modules that come pre-installed.
  • These include os, sys, math, random, datetime, json, etc.
  • Example: Using the math module
import math
print(math.sqrt(16))  # Output: 4.0

2. User-Defined Modules (Custom modules)

  • You can create your own modules by saving Python functions in a .py file.
  • Example: Creating a custom module my_module.py
# my_module.py
def greet(name):
    return f"Hello, {name}!"

Import and use it in another script:

import my_module
print(my_module.greet("Alice"))  # Output: Hello, Alice!

3. External Modules (Third-party)

  • These modules are installed via pip and not included in Python by default.
  • Examples: numpy, pandas, requests, flask, etc.
  • Install & use an external module:
pip install requests
import requests
response = requests.get("https://api.github.com")
print(response.status_code)  # Output: 200

2️⃣ Useful Built-in Python Modules

Here are some commonly used built-in Python modules:

Module Purpose
os Interact with the operating system (files, directories, environment variables)
sys Access system-specific parameters & functions
math Perform mathematical operations
random Generate random numbers
datetime Work with dates and time
time Handle time-related functions
json Parse and handle JSON data
re Work with regular expressions
statistics Perform statistical operations
csv Read and write CSV files

3️⃣ Exploring Installed Modules

To see a list of available modules in Python:

help('modules')

4️⃣ Importing Modules in Different Ways

1. Import the whole module

import math
print(math.pi)  # Output: 3.141592653589793

2. Import specific functions

from math import sqrt, ceil
print(sqrt(25))  # Output: 5.0
print(ceil(4.3)) # Output: 5

3. Import with an alias

import datetime as dt
print(dt.datetime.now())  # Current date and time

4. Import everything using 

from math import *
print(factorial(5))  # Output: 120

5️⃣ Creating and Using Your Own Module

Step 1: Create a module file

Create a file my_module.py with some functions:

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

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

Step 2: Import and use it

import my_module

print(my_module.add(5, 3))  # Output: 8
print(my_module.subtract(10, 4))  # Output: 6

6️⃣ Finding Module Location

To check where Python imports a module from:

import math
print(math.__file__)

? Summary

Python modules make code reusable and organized.
✔ There are built-in, user-defined, and external modules.
✔ You can import modules in different ways using import, from module import, or aliases.
✔ External modules can be installed via pip install module_name.

Scope @ NareshIT:

  1. At NareshIT’s Python application Development program you will be able to get the extensive hands-on training in front-end, middleware, and back-end technology.

  2. It skilled you along with phase-end and capstone projects based on real business scenarios.

  3. Here you learn the concepts from leading industry experts with content structured to ensure industrial relevance.

  4. An end-to-end application with exciting features