Skip to content

Python Standard LibrariesΒΆ

Python comes with a "batteries included" philosophy, meaning it has a rich set of built-in modules.

1. The math ModuleΒΆ

Provides mathematical functions.

import math
print(math.sqrt(64)) # 8.0
print(math.pi)       # 3.1415...

2. The os ModuleΒΆ

Used for interacting with the operating system (files, folders, paths).

import os
print(os.getcwd())   # Current working directory
os.mkdir("new_folder")

3. The random ModuleΒΆ

Used for generating random numbers and choices.

import random
print(random.randint(1, 10))
print(random.choice(["Red", "Blue", "Green"]))

4. The datetime ModuleΒΆ

Used for working with dates and times.

import datetime
print(datetime.datetime.now())

5. The sys ModuleΒΆ

Provides access to variables used or maintained by the interpreter.

import sys
print(sys.version)
print(sys.argv) # Command line arguments

6. The statistics ModuleΒΆ

Mathematical statistics functions.

import statistics
data = [1, 2, 3, 4, 5, 5]
print(statistics.mean(data))   # 3.33
print(statistics.mode(data))   # 5

Searching for Modules

The official Python Module Index is the best place to find details on every built-in library.