Python Modules

🐍 Python Modules β€” Full Guide

A module in Python is simply a file that contains Python code β€” such as variables, functions, classes, etc.

Modules help in:

βœ” Code reusability
βœ” Better organization
βœ” Avoiding duplication
βœ” Working with built-in tools


πŸ”Ή 1. Using Built-in Modules

Python comes with many modules like:
math, random, datetime, os, etc.

Example:

import math

print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793


πŸ”Ή 2. import Statement

There are multiple ways to import modules:

βœ” Import whole module

import random
print(random.randint(1, 10))

βœ” Import specific function

from math import sqrt
print(sqrt(25))

βœ” Import module with alias

import math as m
print(m.cos(0))

βœ” Import multiple functions

from math import sqrt, pow
print(sqrt(9))
print(pow(2, 3))

πŸ”Ή 3. Create Your Own Module

Create a file named: my_module.py

def greet(name):
return f"Hello {name}, welcome!"

pi = 3.14

Now import it in another file:

import my_module

print(my_module.greet("Vipul"))
print(my_module.pi)


πŸ”Ή 4. Using from module import *

This imports everything from a module:

from math import *
print(sin(0))
print(sqrt(16))

⚠ Not recommended for large projects because it becomes unclear what came from where.


πŸ”Ή 5. Built-in Useful Modules

Module Name Purpose
math Mathematical operations
random Generate random numbers
datetime Work with dates/time
os Operating system interaction
sys System-specific functions
statistics Mean, median, mode

Examples

πŸ“Œ Random Module

import random

print(random.choice(["Apple", "Banana", "Cherry"]))
print(random.randint(1, 100))

πŸ“Œ Datetime Module

import datetime

now = datetime.datetime.now()
print(now)
print(now.year)


πŸ”Ή 6. Check What’s Inside a Module

Use the dir() function:

import math
print(dir(math))

πŸ”Ή 7. Working with Packages

A package is a group of modules stored in a folder containing an __init__.py file.

Example:

myproject/
└── utilities/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ math_utils.py
└── string_utils.py

Usage:

from utilities.math_utils import add

πŸ”Ή 8. Install External Modules (PIP)

Use terminal command:

pip install numpy

Then import and use:

import numpy as np

arr = np.array([1, 2, 3])
print(arr)


πŸ“Œ Summary Table

Import Type Example
Import whole module import math
Import with alias import math as m
Import function from math import sqrt
Import all from math import *
Custom module import my_module

🧠 Practice Tasks

  1. Create a module named calculator.py with add, subtract, multiply, divide functions and import it.

  2. Use the random module to create a dice rolling program.

  3. Use datetime to print a user-friendly timestamp.

  4. Install an external package like pyjokes and print a random joke.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *