Standard Python Modules
A few of Python's built-in modules that ship with the language itself — no installation needed, just an import.
Math Module
Extra math functions beyond Python's built-in operators — import it first with import math.
import math print(math.sqrt(144)) print(math.ceil(4.1)) print(math.floor(4.9)) print(math.factorial(5)) print(math.pi)
sqrt() finds a square root, ceil()/floor() round up or down to the nearest whole number, factorial() computes n!, and pi is a ready-made constant for π.
Random Module
Generates pseudo-random numbers and choices — import it with import random.
import random print(random.random()) print(random.randint(1, 10)) print(random.choice(["a", "b", "c"])) nums = [1, 2, 3, 4] random.shuffle(nums) print(nums)
random() gives a float between 0 and 1, randint(a, b) gives a whole number between a and b inclusive, choice() picks one item from a sequence, and shuffle() rearranges a list in place. Since these are random, running the same code again will give different results.
Statistics Module
Common statistical calculations on a set of numbers — import it with import statistics.
import statistics data = [4, 8, 15, 16, 23, 42] print(statistics.mean(data)) print(statistics.median(data)) print(statistics.mode([1, 1, 2, 3]))
mean() gives the average, median() gives the middle value once sorted (averaging the two middle values when there's an even count), and mode() gives whichever value appears most often.