Python Built in Modules

Reading Time: 6 minutes

Ready to test your knowledge? 🧠

After reading all the materials in this learning path, take the assessment to evaluate your skills and boost your exam readiness.

Take the Quiz Now →
📝 Python Built in Modules ( Topics )

What are Modules ?

A module is a file containing Python code (functions, variables, classes) that can be reused in other programs.

Example: math.py, random.py

  • Its functions are reusable.
  • Do no need writing code from scratch.
  • It has organized and modular code.
  • It supports faster development.

Importing Modules

To use a module, we need to import it. we can import it by using word import.

we will import math module and use its function sqrt( ). As you can see we do not need to write complete code to calculate square root of a number here. because we have used a simple function.

import math

print(math.sqrt(16))         # square root of 16
4.0

Import Specific Functions

we also import specific functions only by using From.

from math import sqrt

print(sqrt(25))
5.0

Import with Alias

we can also give and use alias to the module that we are importing. It will become easier for us to write module name, if we are using multiple functions of a module.

import math as m

print(m.factorial(5))
120

Built-in Modules

Now let’s study commonly used built-in modules.

math Module

It is used for mathematical operations. we will see some common functions in this learning path.

import math

print(math.sqrt(36))
print(math.pow(2, 3))
print(math.pi)
6.0
8.0
3.141592653589793

Important Functions

  • sqrt() → square root
  • pow() → power
  • factorial() → factorial
  • ceil() / floor() → rounding

random Module

It is used to generate random numbers.

import random

print(random.randint(1, 10))
print(random.choice([10, 20, 30]))
7
20

👉 Output may vary each time.

datetime Module

It is used for date and time operations.

import datetime

now = datetime.datetime.now()

print(now)
print(now.year)
print(now.month)
2026-04-20 10:30:45.123456
2026
4

os Module

It is used for interacting with the operating system.

import os

print(os.getcwd())   # Current directory
/home/user/project

time Module

It is used for time-related functions.

import time

print("Start")
time.sleep(2)
print("End")
Start
(wait 2 seconds)
End

statistics Module

It is used for statistical calculations.

import statistics

data = [10, 20, 30, 40]

print(statistics.mean(data))
print(statistics.median(data))
25
25.0

sys Module

It provides system-specific parameters.

import sys

print(sys.version)
3.12.0 (default, ...)

dir() and help() Functions

These help in exploring modules.

import math

print(dir(math))

Shows all functions inside the module.

help(math.sqrt)

👉 Gives documentation of a function.

Creating Your Own Module

You can create your own module.

Step 1: Create file mymodule.py and define any function / code that you use very often and save it.

def greet(name):
    return "Hello " + name

Step 2: Now open another file and import your module.

import mymodule

print(mymodule.greet("Rahul"))
Hello Rahul

Real-Life Use Cases

  • math → Scientific calculations
  • random → Games, simulations
  • datetime → Clocks, calendars
  • os → File handling
  • sys → System-level operations

Practice Programs

# Generate random number and find square root

import random
import math

num = random.randint(1, 100)

print("Random Number:", num)
print("Square Root:", math.sqrt(num))
Random Number: 49
Square Root: 7.0

1️⃣ Generate a random number between 1 and 50

import random

print(random.randint(1, 50))

2️⃣ Find factorial of a number

import math

print(math.factorial(5))
120

3️⃣ Print current date

import datetime

print(datetime.datetime.now().date())

Ready to test your knowledge? 🧠

After reading all the materials in this learning path, take the assessment to evaluate your skills and boost your exam readiness.

Take the Quiz Now →