Python Docs

Python Built-in Modules — Full Cheat Sheet

Python comes with many powerful built-in modules that give you extra features without installing anything. Below are some of the most commonly used ones with simple examples.

1. math Module

The math module provides mathematical functions like square root, factorial, π, trigonometry, etc.

import math

print(math.sqrt(25))
print(math.pi)
print(math.factorial(5))

2. random Module

Used for generating random numbers, choosing random items, shuffling lists, etc.

import random

print(random.randint(1, 10))
print(random.choice(["apple", "banana", "cherry"]))

3. datetime Module

Used to work with dates and time (current time, formatting, etc.).

from datetime import datetime

now = datetime.now()
print(now)
print(now.year)
print(now.strftime("%d/%m/%Y"))

4. os Module

Used to interact with the operating system.

  • Get current working directory
  • List files and folders
  • Work with paths and environment variables
import os

print(os.getcwd())
print(os.listdir("."))

5. sys Module

Gives access to system-level information like Python version and import paths.

import sys

print(sys.version)
print(sys.path)

6. platform Module

Useful for detecting OS type and machine/CPU details. Good for writing cross-platform code.

import platform

print(platform.system())
print(platform.machine())

7. statistics Module

Helpful for basic data analysis like mean, median, variance, etc.

import statistics

nums = [10, 20, 30, 40, 50]
print(statistics.mean(nums))
print(statistics.median(nums))
print(statistics.variance(nums))

8. json Module

Used to work with JSON data, very common in APIs and web applications.

import json

data = {"name": "Emil", "age": 25}
json_string = json.dumps(data)
print(json_string)

9. re Module

The re module deals with regular expressions — powerful pattern matching for text searching and validation.

import re

text = "I love Python"
x = re.search("Python", text)
print(x)

Summary

Python's built-in modules save you time by providing ready-made functionality for maths, randomness, OS control, date/time, system info, statistics, JSON handling, and text searching. Learn these well and your scripts become much more powerful with very little code.