Python Built-in Modules

A module is just a code library. Discover what's available with help('modules'), then take a hands-on tour of math, random, time, and os.

Overview

This topic covers Python modules โ€” what they are, how to discover which ones are available, and how to use some of the most common built-in modules (math, random, time, os) through hands-on examples in Jupyter Notebook.

Key Concepts

Detailed Explanation

What are Modules?

"Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application."

Examples of Python modules covered in this lesson:

Listing All Available Modules

You can see every module installed/available in your Python environment with:

help('modules')

What happens:

๐Ÿ’ก Tip: help('modules') is a great way to explore what's available in your environment, but the output can be long โ€” scroll through it or search for a keyword.

Importing a Module

Before using any function from a module, you must import it:

import math
import random
import os
import time

Important Point: If you misspell a module name or import something that isn't a real module, Python raises a ModuleNotFoundError.

Example of an error:

import mathrhe
ModuleNotFoundError: No module named 'mathrhe'

C vs Python โ€” importing a library:

CPython
Bring in a library#include <stdio.h>import math
Syntax#include <filename.h>import module_name
When it happensPreprocessor step (before compilation)Runtime (when the line executes)

Same purpose in both โ€” reuse existing code โ€” just a different keyword and mechanism.

Things to Remember:

The math Module

Used for mathematical operations.

FunctionExampleResultDescription
math.pimath.pi3.141592653589793Value of ฯ€
math.emath.e2.718281828459045Euler's number
math.factorial(n)math.factorial(5)120Factorial of n (5! = 120)
math.ceil(x)math.ceil(6.3)7Rounds up to the nearest integer
math.floor(x)math.floor(6.9)6Rounds down to the nearest integer
math.sqrt(x)math.sqrt(100)10.0Square root (always returns a float)

Key takeaway: ceil() always rounds up, floor() always rounds down โ€” regardless of the decimal value (e.g., 6.1 would still floor to 6 and ceil to 7).

The random Module

Used for generating random numbers and shuffling data.

import random

random.randint(1, 100)   # Returns a random integer between 1 and 100 (inclusive)

Example output: 54

a = [1, 2, 3, 4, 5]
random.shuffle(a)        # Shuffles the list IN PLACE (no return value needed)
a

Example output: [1, 4, 5, 2, 3] (order will vary each run)

Important Points:

The time Module

Used for working with time-related values.

import time

time.time()     # Returns current time as a Unix timestamp (seconds since epoch)

Example output: 1625459281.405317

time.ctime()    # Converts current time into a human-readable string

Example output: 'Mon Jul 5 09:58:08 2021'

time.sleep(1)   # Pauses program execution for the given number of seconds

Example demonstrating time.sleep():

print("Hello")
time.sleep(1)
print("World")

Output:

Hello
World

("Hello" prints, then execution pauses for 1 second, then "World" prints.)

Important Points:

The os Module

Used to interact with the operating system (files, directories, paths).

import os

os.getcwd()     # Returns the current working directory

Example output:

'C:\\Users\\91842\\100-days-of-python\\day9-builtins'
os.listdir()    # Lists all files and folders in the current directory

Example output:

['.ipynb_checkpoints', 'built-in-functions.ipynb', 'built-in-modules.ipynb']

Important Points:

Commands / Syntax Summary

# Discover all available modules
help('modules')

# Import modules
import math
import random
import os
import time

# math module
math.pi
math.e
math.factorial(5)
math.ceil(6.3)
math.floor(6.9)
math.sqrt(100)

# random module
random.randint(1, 100)
random.shuffle(my_list)

# time module
time.time()
time.ctime()
time.sleep(seconds)

# os module
os.getcwd()
os.listdir()

Things to Remember

Quick Revision

Python modules are reusable code libraries. Use help('modules') to see all available ones, and import <module_name> to bring one into your program. Four commonly used built-in modules:

If you import something that doesn't exist, Python throws ModuleNotFoundError: No module named '<name>' โ€” always double-check spelling.