What is a Module?
A module is a single Python file (.py) that contains functions, variables, or classes which can be reused in other programs.
Example:
If you create a file named utils.py, it becomes a module.
Why modules are important:
- Promote code reusability
- Help in organizing large codebases
- Reduce redundancy
What is a Package?
A package is a folder that contains multiple modules.
Structure example:
my_package/
├── module1.py
├── module2.py
└── __init__.py
Packages allow you to structure your project logically and scale your code efficiently.
Importing Modules
There are multiple ways to import modules:
1. Import Entire Module
import math
print(math.sqrt(16))
2. Import Specific Functions
from math import sqrt, ceil
print(sqrt(16))
print(ceil(4.2))
3. Using Module Alias
import math as m
print(m.factorial(5))
Built-in Modules in Python
Python provides many built-in modules that simplify development.
Math Module
Used for mathematical operations.
import math
print(math.sqrt(16))
print(math.ceil(4.2))
print(math.floor(4.9))
print(math.pow(2, 3))
print(math.factorial(5))
Common functions:
- sqrt()
- ceil()
- floor()
- pow()
- factorial()
Random Module
Used for generating random values.
import random
print(random.random())
print(random.randint(1, 10))
print(random.choice([1, 2, 3, 4]))
Shuffling example:
lst = [1, 2, 3, 4]
random.shuffle(lst)
print(lst)
Other function:
- uniform(a, b) → random float between a and b
Datetime Module
Used for working with dates and time.
from datetime import datetime, date, timedelta
print(datetime.now())
print(date.today())
d = datetime.now()
print(d.strftime("%Y-%m-%d"))
new_date = d + timedelta(days=5)
print(new_date)
Key functionalities:
- Current date and time
- Formatting dates
- Date arithmetic
OS Module
Used for interacting with the operating system.
import os
print(os.getcwd())
print(os.listdir())
Common operations:
- Get current directory
- List files
- Create/delete folders
- Rename files
Key Takeaways
- Modules help reuse code
- Packages help organize large projects
- Built-in modules save development time
- Proper imports improve code readability
Assignment Questions
Basic Level
- Import the math module and find square root of a number
- Import specific functions from math and use ceil() and floor()
- Generate a random number between 1 and 50
- Print current date using datetime
- Print current working directory using os
Intermediate Level
- Generate a list of 5 random numbers using random.randint()
- Shuffle a list of numbers using random.shuffle()
- Format current date as DD-MM-YYYY
- Create a program to add 7 days to current date
- List all files in your current directory
Advanced Level
- Create your own module with functions (e.g., add, subtract) and import it
- Create a package with at least 2 modules and use them
- Build a mini project using math and random (e.g., number guessing game)
- Write a script to organize files in a folder using os
- Combine datetime and os to log file creation time
Summary
Modules and packages are fundamental for writing scalable Python applications.
- Modules allow code reuse
- Packages provide structure
- Built-in modules handle complex operations easily
Understanding this concept is essential before moving into:
- Large projects
- Frameworks
- Real-world software development
.png)

0 Comments:
Post a Comment