Standard Modules in Python: math, datetime, and os
Python follows a “Batteries Included” philosophy — it comes pre-installed with rich standard modules so programmers don’t have to write common utilities from scratch. Three of the most important standard modules in the BCA syllabus are math, datetime, and os.
pi and e).strftime), and date arithmetic.1. The math Module
The math module provides fast mathematical functions and constants. To use it, write import math.
Important Constants in math:
math.pi→ Value of Pi (3.141592653589793)math.e→ Euler’s number (2.718281828459045)
Important Functions in math:
| Function | Description | Example | Output |
|---|---|---|---|
math.sqrt(x) |
Returns the square root of x as a float |
math.sqrt(49) |
7.0 |
math.pow(x, y) |
Returns x raised to power y as a float |
math.pow(2, 5) |
32.0 |
math.ceil(x) |
Rounds a float UP to the nearest integer | math.ceil(4.2) |
5 |
math.floor(x) |
Rounds a float DOWN to the nearest integer | math.floor(4.8) |
4 |
math.factorial(x) |
Returns the factorial of x (x!) |
math.factorial(5) |
120 |
math.gcd(a, b) |
Returns Greatest Common Divisor of a & b |
math.gcd(12, 18) |
6 |
math.fabs(x) |
Returns the absolute (positive) float value | math.fabs(-15.5) |
15.5 |
math.sin(x), cos(x) |
Trigonometric functions (x in radians) |
math.sin(math.radians(90)) |
1.0 |
math.log10(x) |
Base-10 logarithm of x |
math.log10(1000) |
3.0 |
import math
radius = 7
area = math.pi * math.pow(radius, 2)
print("Area of circle =", round(area, 2)) # Output: 153.94
print("Ceiling of 9.1 =", math.ceil(9.1)) # Output: 10
print("Floor of 9.9 =", math.floor(9.9)) # Output: 9
print("5! =", math.factorial(5))# Output: 120
2. The datetime Module
The datetime module provides classes for manipulating dates and times in both simple and complex ways.
Main Classes Inside datetime Module:
datetime.date— Represents a calendar date (year,month,day).datetime.time— Represents time of day (hour,minute,second,microsecond).datetime.datetime— Combines bothdateandtimeinto a single object.datetime.timedelta— Represents the difference (duration) between two dates or times (used for adding/subtracting days).
Practical Examples of datetime:
from datetime import datetime, date, timedelta
# 1. Get current date and time
now = datetime.now()
print("Current Date & Time:", now)
# 2. Get only today's date
today = date.today()
print("Year:", today.year, "Month:", today.month, "Day:", today.day)
# 3. Create a specific custom date (e.g., Independence Day)
ind_day = date(1947, 8, 15)
print("Custom Date:", ind_day)
# 4. Date Arithmetic using timedelta (Add 7 days to today)
next_week = today + timedelta(days=7)
print("Date after 7 days:", next_week)
Formatting Dates with strftime() (String Format Time):
The strftime() method converts a datetime object into a readable formatted string:
%d→ Day of month (01to31)%m→ Month number (01to12)%B→ Full month name (April,October)%Y→ 4-digit Year (2026)%A→ Full weekday name (Monday,Friday)%H:%M:%S→ 24-hour Time (14:30:05)
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%d-%B-%Y (%A) %H:%M:%S")
print("Formatted:", formatted)
# Example Output: 02-April-2026 (Thursday) 18:45:10
3. The os Module (Operating System Interface)
The os module allows your Python program to interact directly with the underlying Operating System (Windows, Linux, macOS) to manage directories (folders), files, paths, and environment variables.
Important Functions in os:
| Function | Description |
|---|---|
os.name |
Returns the OS kernel name ('nt' for Windows, 'posix' for Linux/Mac) |
os.getcwd() |
Returns the Current Working Directory (folder path) |
os.chdir(path) |
Changes the current working directory to path |
os.listdir(path) |
Returns a list of all files and folders inside path |
os.mkdir("folder") |
Creates a single new folder (directory) |
os.makedirs("a/b/c") |
Creates nested folders recursively |
os.rmdir("folder") |
Removes an empty directory |
os.remove("file.txt") |
Deletes a file from the disk |
os.rename(old, new) |
Renames a file or folder from old to new |
os.path.exists(path) |
Returns True if the file or folder exists, else False |
import os
# 1. Check current working directory
print("Current Folder:", os.getcwd())
# 2. Create a new folder if it doesn't exist
if not os.path.exists("BCA_Notes"):
os.mkdir("BCA_Notes")
print("Folder 'BCA_Notes' created successfully!")
# 3. List all files and folders in current directory
print("Files in current directory:", os.listdir("."))