Python's built-in libraries
Compute a factorial, find today's date, shuffle a list, read JSON — none of this needs anything installed. It already ships with Python out of the box: dozens of ready-made modules available right after an import, with no pip install. This is the standard (built-in) library.
What are built-in libraries?
The main built-in libraries
Let's go through several of the most useful built-in libraries in Python.
math: mathematical functions
The math module provides access to mathematical functions defined in the C language standard:
Python 3.13import math # Constants print(f"Number π: {math.pi}")Number π: 3.141592653589793print(f"Number e: {math.e}")Number e: 2.718281828459045# Trigonometric functions angle = math.pi / 4 # 45 degrees in radians print(f"Sine of 45°: {math.sin(angle):.4f}")Sine of 45°: 0.7071print(f"Cosine of 45°: {math.cos(angle):.4f}")Cosine of 45°: 0.7071# Other functions print(f"Factorial of 5: {math.factorial(5)}")Factorial of 5: 120print(f"Greatest common divisor of 12 and 18: {math.gcd(12, 18)}")Greatest common divisor of 12 and 18: 6
random: generating random numbers
The random module provides functions for generating random numbers and picking random elements:
Python 3.13import random # Generating a random integer in a range print(f"Random number between 1 and 10: {random.randint(1, 10)}")Random number between 1 and 10: 7# Random float between 0 and 1 print(f"Random number between 0 and 1: {random.random():.4f}")Random number between 0 and 1: 0.3528# Picking a random element from a sequence fruits = ["apple", "banana", "orange", "pear"] print(f"Random fruit: {random.choice(fruits)}")Random fruit: orange# Shuffling a sequence numbers = [1, 2, 3, 4, 5] random.shuffle(numbers) print(f"Shuffled numbers: {numbers}")Shuffled numbers: [3, 1, 5, 2, 4]
datetime: dates and times
The datetime module can parse dates from strings, add intervals to them, and format them back. That set is enough for typical date operations:
Python 3.13from datetime import datetime, timedelta # parse a string into a date using the "day.month.year" pattern d = datetime.strptime("31.12.2022", "%d.%m.%Y") # add an interval new_d = d + timedelta(days=5) # format the date back into a string print(new_d.strftime("%d.%m.%Y"))05.01.2023
This is just the tip of the iceberg — datetime can do a lot more, and it has its own chapter, "Working with dates and times", later in the module.
Two more big standard-library topics have chapters of their own, so here we'll just name the modules:
But collections didn't get its own chapter, so let's look at it right now.
collections: specialised data types
The collections module provides several convenient data structures on top of the built-in ones. One of the most useful is Counter for counting elements:
Python 3.13from collections import Counter orders = ["apple", "banana", "apple", "cherry", "apple", "banana"] counts = Counter(orders) # most_common() sorts by count, highest first print(counts.most_common())[('apple', 3), ('banana', 2), ('cherry', 1)]
The module also has defaultdict, namedtuple, deque and others, advanced tools that will come in handy later.
Understanding check
Which library is best suited for working with dates in Python?
In the next lesson we'll look at third-party libraries, the ones installed via pip install that extend Python beyond the standard distribution.
