Useful Built-in Modules (math, random, datetime, os, sys) in Python
Python's standard library ships with hundreds of ready-to-use modules covering an enormous range of everyday needs, all installed automatically alongside Python itself. A handful come up constantly across almost any kind of program: math for numeric functions, random for randomness, datetime for dates and times, os for interacting with the operating system, and sys for interacting with the Python interpreter itself.
math β numeric functions and constants
Beyond the basic operators, like sqrt(), floor(), ceil(), and pi.
random β randomness
Generates pseudo-random numbers and makes random selections, useful for simulations, games, and sampling.
datetime β dates and times
Represents dates, times, and durations, with rich support for formatting and arithmetic on them.
os β the operating system
Environment variables and the file system operations covered in an earlier module.
sys β the interpreter itself
Command-line arguments, the exact Python version running, and other interpreter-level details.
import mathfrom datetime import date, timedeltaimport sysΒ print(math.floor(7.9))print(math.ceil(7.1))Β today = date(2024, 1, 1)next_week = today + timedelta(days=7)print(next_week)Β print(sys.version_info.major)Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself β building beats watching.