←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Standard Modules in Python: os, datetime & math Modules

Study material and explanations for Unit 3

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.

Core Standard Modules in Unit 3
1. math Module
Mathematical functions (square root, trigonometry, logarithms, constants like pi and e).
2. datetime Module
Working with dates, times, timestamps, formatting (strftime), and date arithmetic.
3. os Module
Interacting with the Operating System (folders, file paths, environment variables).

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:

  1. datetime.date — Represents a calendar date (year, month, day).
  2. datetime.time — Represents time of day (hour, minute, second, microsecond).
  3. datetime.datetime — Combines both date and time into a single object.
  4. 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 (01 to 31)
  • %m → Month number (01 to 12)
  • %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("."))