๐น What is a Python Module?
A module in Python is a file containing Python functions, classes, and variables that can be imported and reused in other programs.
- It helps organize code logically.
- Reduces redundancy and improves code reusability.
- A module is simply a Python file (
.py
) that can be imported into another script.
โ
Example of a module (mymodule.py
):
# mymodule.py
def greet(name):
return f"Hello, {name}!"
pi = 3.14159
โ
Using the module (main.py
):
import mymodule
print(mymodule.greet("Alice")) # Output: Hello, Alice!
print(mymodule.pi) # Output: 3.14159
๐น Types of Python Modules
Python modules are categorized into three types:
1๏ธโฃ Built-in Modules (Pre-installed)
2๏ธโฃ User-Defined Modules (Custom-created)
3๏ธโฃ External Modules (Installed via pip
)
1๏ธโฃ Built-in Modules (Standard Library)
Python comes with many built-in modules that provide useful functions.
โ Examples of Built-in Modules
Module | Purpose |
---|---|
math |
Mathematical functions |
random |
Generate random numbers |
os |
Operating system interaction |
sys |
System-specific parameters and functions |
time |
Time-related functions |
datetime |
Work with dates and times |
json |
Work with JSON data |
re |
Regular expressions |
๐น Example 1: Using the math
module
import math
print(math.sqrt(16)) # Output: 4.0
print(math.factorial(5)) # Output: 120
๐น Example 2: Using the random
module
import random
print(random.randint(1, 10)) # Output: Random number between 1 and 10
print(random.choice(['apple', 'banana', 'cherry'])) # Output: Random choice
2๏ธโฃ User-Defined Modules
These are modules created by users to organize code efficiently.
โ
Creating a module (greetings.py
)
# greetings.py
def say_hello(name):
return f"Hello, {name}!"
def say_goodbye(name):
return f"Goodbye, {name}!"
โ
Using the module (main.py
)
import greetings
print(greetings.say_hello("Alice")) # Output: Hello, Alice!
print(greetings.say_goodbye("Alice")) # Output: Goodbye, Alice!
โ Importing specific functions
from greetings import say_hello
print(say_hello("Bob")) # Output: Hello, Bob!
3๏ธโฃ External Modules (Third-Party)
These modules are not included in Python by default but can be installed using pip
.
โ Examples of External Modules
Module | Purpose |
---|---|
numpy |
Numerical computing |
pandas |
Data analysis |
requests |
HTTP requests |
flask |
Web development |
matplotlib |
Data visualization |
scipy |
Scientific computing |
โ
Installing an external module (requests
)
pip install requests
โ Using the installed module
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # Output: 200
๐น Importing Modules in Python
Python provides several ways to import modules.
1๏ธโฃ Importing the Entire Module
import math
print(math.sqrt(25)) # Output: 5.0
2๏ธโฃ Importing a Specific Function
from math import sqrt
print(sqrt(25)) # Output: 5.0
3๏ธโฃ Importing with an Alias
import math as m
print(m.sqrt(25)) # Output: 5.0
4๏ธโฃ Importing All Functions (Not Recommended)
from math import *
print(sqrt(25)) # Output: 5.0
โ ๏ธ Caution: This can cause name conflicts if multiple modules have functions with the same name.
๐น Finding Module Location
If you want to check where a module is installed:
import os
print(os.__file__) # Outputs the path of the os module
๐น Checking Available Functions in a Module
import math
print(dir(math)) # Lists all functions in the math module
๐น Creating and Using a Package
A package is a collection of modules organized in folders.
โ Folder structure:
my_package/
โโโ __init__.py
โโโ module1.py
โโโ module2.py
โ module1.py
def greet(name):
return f"Hello, {name}!"
โ Using the package
from my_package import module1
print(module1.greet("Alice")) # Output: Hello, Alice!
๐น Summary Table
Type of Module | Description | Example |
---|---|---|
Built-in Modules | Pre-installed with Python | math , random , os |
User-Defined Modules | Custom-created modules | mymodule.py |
External Modules | Installed via pip |
numpy , pandas , requests |
๐ฏ Final Thoughts
โ
Modules help in organizing and reusing code efficiently.
โ
Python has built-in, user-defined, and external modules.
โ
Using pip
, we can install third-party modules.
Would you like me to explain modules in depth with real-world projects? ๐
Leave a Reply