Python datetime, math, and random Module
Python provides built-in modules to simplify tasks such as date and time manipulation, mathematical operations, and generating random values. These modules are:
- datetime – for date and time handling.
- math – for mathematical functions and constants.
- random – for random number generation.
Python datetime Module – Work with Dates and Times
What is the datetime module in Python?
The datetime module in Python provides classes for manipulating dates and times. It supports both date arithmetic and formatting.
Key Classes in datetime:
- datetime.datetime
- datetime.date
- datetime.time
- datetime.timedelta
Example 1: Get Current Date and Time
import datetime
current_dt = datetime.datetime.now()
print("Current Date and Time:", current_dt)
Output:
Current Date and Time: 2025-05-10 14:32:12.123456
Example 2: Format Date
today = datetime.date.today()
formatted_date = today.strftime("%d-%m-%Y")
print("Formatted Date:", formatted_date)
Output:
Formatted Date: 10-05-2025
Example 3: Calculate Future Date Using timedelta
from datetime import timedelta, date
future_date = date.today() + timedelta(days=10)
print("Date after 10 days:", future_date)
Output:
Date after 10 days: 2025-05-20
Python math Module – Perform Advanced Mathematical Operations
What is the math module in Python?
The math module provides access to mathematical functions like square root, power, trigonometric functions, logarithms, and constants like pi and e.
Commonly Used math Functions:
- math.sqrt(x)
- math.pow(x, y)
- math.sin(x)
- math.log(x, base)
- math.pi, math.e
Example 1: Calculate Square Root
import math
result = math.sqrt(25)
print("Square Root of 25 is:", result)
Output:
Square Root of 25 is: 5.0
Example 2: Use of Pi Constant
radius = 7
area = math.pi * math.pow(radius, 2)
print("Area of Circle:", area)
Output:
Area of Circle: 153.93804002589985
Example 3: Trigonometric Functions
angle = math.radians(30)
sin_val = math.sin(angle)
print("Sine of 30 degrees:", sin_val)
Output:
Sine of 30 degrees: 0.49999999999999994
Python random Module – Generate Random Numbers and Values
What is the random module in Python?
The random module is used for generating pseudo-random numbers. It's widely used in simulations, games, data sampling, and testing.
Common random Functions:
- random.random() – float between 0.0 and 1.0
- random.randint(a, b) – random integer
- random.choice(sequence) – random element
- random.shuffle(list) – shuffle a list
Example 1: Generate a Random Float
import random
value = random.random()
print("Random float between 0 and 1:", value)
Output:
Random float between 0 and 1: 0.73423923
Example 2: Generate Random Integer
num = random.randint(1, 100)
print("Random Integer between 1 and 100:", num)
Output:
Random Integer between 1 and 100: 57
Example 3: Random Choice from List
colors = ['Red', 'Green', 'Blue', 'Yellow']
picked = random.choice(colors)
print("Randomly Picked Color:", picked)
Output:
Randomly Picked Color: Blue
Example 4: Shuffle a List
deck = [1, 2, 3, 4, 5]
random.shuffle(deck)
print("Shuffled Deck:", deck)
Output:
Shuffled Deck: [3, 1, 5, 4, 2]