Learn Python
>>> modules_packages.builtin_modules

Useful Built-in Modules (math, random, datetime, os, sys) in Python

#concept

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.

#how it works
1

math β€” numeric functions and constants

Beyond the basic operators, like sqrt(), floor(), ceil(), and pi.

2

random β€” randomness

Generates pseudo-random numbers and makes random selections, useful for simulations, games, and sampling.

3

datetime β€” dates and times

Represents dates, times, and durations, with rich support for formatting and arithmetic on them.

4

os β€” the operating system

Environment variables and the file system operations covered in an earlier module.

5

sys β€” the interpreter itself

Command-line arguments, the exact Python version running, and other interpreter-level details.

#example
import math
from datetime import date, timedelta
import 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)
Output
7
8
2024-01-08
3
1math.floor(7.9) rounds down to 7; math.ceil(7.1) rounds up to 8 β€” both from the math module.
2date(2024, 1, 1) creates a specific date, and adding a timedelta(days=7) computes a date exactly one week later, directly through arithmetic.
3sys.version_info.major reveals the major version number of the Python interpreter actually running the code β€” 3, for any modern Python.
#use cases
Doing calculations beyond basic arithmetic with math, like square roots, logarithms, or trigonometry
Simulating dice rolls, shuffling a deck of cards, or sampling data randomly with random
Calculating durations, formatting dates for display, or scheduling with datetime
Reading environment variables or command-line arguments with os and sys, common in scripts and command-line tools

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself β€” building beats watching.