Python provides built-in modules to handle common tasks: math for mathematical operations and constants, random for generating randomness, and datetime for working with dates and times. Together, they are used in calculations, games, simulations, scheduling, logging, and more.
math module → precise mathematical functions like square root, factorial, trigonometry, rounding, and constants like math.pi.random module → random numbers, random choices from sequences, shuffling, and sampling.datetime module → working with dates, times, time differences, and formatting.import math, import random, import datetime).math.sqrt(x) → square root of x.math.factorial(n) → n! (product 1 to n).math.ceil(x) → smallest integer ≥ x.math.floor(x) → largest integer ≤ x.math.pi, math.e → common mathematical constants.random.random() → float in range [0.0, 1.0).random.randint(a, b) → integer between a and b (inclusive).random.choice(seq) → random element from a sequence.random.shuffle(list) → shuffle list in place.datetime.datetime.now() → current date and time.datetime.date.today() → current date only.timedelta (difference in days/seconds).strftime() → format dates (e.g., "%d-%m-%Y").The math module provides functions for square roots, factorials, rounding, logs, and trigonometry.
import math
# Basic mathematical operations
print("Square root of 25:", math.sqrt(25))
print("Factorial of 5:", math.factorial(5))
print("Ceil of 4.3:", math.ceil(4.3))
print("Floor of 4.7:", math.floor(4.7))
print("Value of pi:", math.pi)
# Trigonometric functions
print("sin(pi/2):", math.sin(math.pi/2))
print("cos(0):", math.cos(0))
print("tan(pi/4):", math.tan(math.pi/4))
# Logarithmic functions
print("log(100, 10):", math.log(100, 10)) # log base 10
print("Natural log of 2.718:", math.log(math.e))
The random module is useful in games, simulations, sampling, and any situation where you need unpredictability.
import random
# Random float between 0.0 and 1.0
print("Random float:", random.random())
# Random integer between 1 and 10
print("Random integer:", random.randint(1, 10))
# Random choice from a list
fruits = ['apple', 'banana', 'cherry']
print("Random fruit:", random.choice(fruits))
# Shuffle a list in-place
random.shuffle(fruits)
print("Shuffled fruits:", fruits)
# Random sample from a list
sample = random.sample(fruits, 2)
print("Random sample of 2 fruits:", sample)
The datetime module lets you get the current time, create specific dates, find differences between dates, and format them.
import datetime
# Current date and time
now = datetime.datetime.now()
print("Current date and time:", now)
# Current date only
today = datetime.date.today()
print("Today's date:", today)
# Create a specific date
birthday = datetime.date(2000, 5, 17)
print("Birthday:", birthday)
# Date difference
delta = today - birthday
print("Days since birthday:", delta.days)
# Formatting dates
print("Formatted date:", today.strftime("%d-%m-%Y")) # dd-mm-yyyy
print("Day of the week:", today.strftime("%A")) # e.g., Tuesday
math.random.datetime.random and datetime to generate random dates or timestamps for test data.Square root of 25: 5.0, factorial values like 120, ceiling/floor operations, and trigonometric values such as sin(pi/2): 1.0.date object.17-05-2000.Tuesday).If your output looks similar (even if the exact numbers are different for random and time-based values), your code is working correctly.
math for precise mathematical calculations and constants instead of writing your own formulas from scratch.random to add randomness for simulations, games, or random selections in your programs.datetime to handle dates, times, and differences between them in a clean, readable way.import math at the top of your file).random with datetime to create test data.random.randint().random.shuffle().datetime.date objects.math.ceil(), math.floor(), and math.factorial() in your own calculations."Weekday, Month Day, Year" format using strftime().