Python Modules, pip and Virtual Environments (venv) Explained for Beginners
P10.2 Useful Built-in Python Modules: math, datetime, random, statistics
Why. These come with Python — no install needed — and they cover many daily data tasks.
| Module | Use it for | Example |
|---|---|---|
| math | rounding up/down, roots, logs | math.ceil(4.2) → 5 |
| datetime | dates and times | date(2026, 3, 1) |
| random | random samples, shuffles | random.choice(list) |
| statistics | mean, median, mode | statistics.median(x) |
How to read this table: each row is one built-in module, what it is for and a tiny example.
How.
Use a built-in module
- Import it.
- Call the function you need.
- Print and check the result.
Worked example A — Round up boxes with math.
Step 1 — What we do. A warehouse needs boxes for 53 items, 12 per box. A half-full box still counts.
Step 2 — Type and run this code.
import math
print(math.ceil(53 / 12))
Step 3 — Output you should see.
5
Step 4 — What it means. ceil rounds up: 5 boxes. (// from the variables chapter gave 4 full boxes.)
Worked example B — Days between dates.
Step 1 — What we do. A loan in Singapore started on 1 Jan 2026; how many days until 15 Mar 2026?
Step 2 — Type and run this code.
from datetime import date
start = date(2026, 1, 1)
end = date(2026, 3, 15)
print((end - start).days)
Step 3 — Output you should see.
73
Step 4 — What it means. Subtracting dates gives a time difference; .days is the number of days.
Worked example C — Median vs mean.
Step 1 — What we do. Salaries in a small IT team, with one very high value.
Step 2 — Type and run this code.
import statistics
salaries = [52000, 55000, 58000, 61000, 250000]
print(statistics.mean(salaries))
print(statistics.median(salaries))
Step 3 — Output you should see.
95200
58000
Step 4 — What it means. One big salary pulls the mean up. The median (58000) shows the typical salary better.
Worked example D — A repeatable random sample.
Step 1 — What we do. We pick 2 stores to audit, and want the same pick every time.
Step 2 — Type and run this code.
import random
random.seed(42)
stores = ["Pune", "Mumbai", "Nagpur", "Nashik", "Aurangabad"]
print(random.sample(stores, 2))
Step 3 — Output you should see.
['Pune', 'Aurangabad']
Step 4 — What it means. seed(42) makes random results repeatable, so a teammate gets the same sample.
Ravindra Bagale's Tip
Use random.seed() whenever you sample data for a report. Then anyone can reproduce your numbers.
Ravindra Bagale's Tip – मराठी
Report साठी data sample करताना नेहमी random.seed() वापरा. मग कोणीही तुमचे आकडे परत मिळवू शकतो.
Ravindra Bagale's Tip – हिंदी
Report के लिए data sample करते समय हमेशा random.seed() इस्तेमाल करो. फिर कोई भी आपके आँकड़े दोबारा पा सकता है.