Python Built-in Modules

Python comes with many built-in modules that provide useful functions and features. These modules can be used directly by importing them into your program. math Module The math module provides mathematical functions. Example Output: random Module The random module is used to generate random values. Example Output: Output may vary each time. datetime Module The […]

2 mins read Lesson 5 of 44 11% through track

Python comes with many built-in modules that provide useful functions and features. These modules can be used directly by importing them into your program.

math Module

The math module provides mathematical functions.

Example

import math

print(math.sqrt(25))
print(math.ceil(4.2))
print(math.floor(4.8))

Output:

5.0
5
4

random Module

The random module is used to generate random values.

Example

import random

print(random.randint(1, 10))

Output:

7

Output may vary each time.

datetime Module

The datetime module is used to work with dates and times.

Example

import datetime

current_date = datetime.datetime.now()

print(current_date)

Output:

2026-05-31 10:30:15.123456

Output will show the current date and time.

os Module

The os module provides functions for interacting with the operating system.

Example

import os

print(os.getcwd())

Output:

/home/user/project

Output will vary depending on your system.

sys Module

The sys module provides access to Python runtime information.

Example

import sys

print(sys.version)

Output:

3.13.0

Output may vary depending on the Python version installed.

statistics Module

The statistics module provides statistical functions.

Example

import statistics

numbers = [10, 20, 30, 40]

print(statistics.mean(numbers))

Output:

25

calendar Module

The calendar module is used to display calendars.

Example

import calendar

print(calendar.month(2026, 5))

Output:

      May 2026
Mo Tu We Th Fr Sa Su
...

collections Module

The collections module provides specialized container data types.

Example

from collections import Counter

data = ["apple", "banana", "apple"]

print(Counter(data))

Output:

Counter({'apple': 2, 'banana': 1})

Summary

  • Built-in modules come with Python.
  • math provides mathematical functions.
  • random generates random values.
  • datetime works with dates and times.
  • os interacts with the operating system.
  • sys provides Python runtime information.
  • statistics performs statistical calculations.
  • calendar displays calendar data.
  • collections provides advanced data structures.