CodingNic

Modules, Packages, and Virtual Environments

Standard Library Modules

Modules, Packages, and Virtual Environments 30 min read

Standard Library Modules

Standard Library Modules

Python comes with many useful modules already installed.

You do not need to download them.

This collection is called the standard library.

What Is the Standard Library?

The standard library is a set of built-in modules that come with Python.

They help with:

  • math
  • dates
  • random values
  • files
  • operating system tasks
  • JSON
  • CSV
  • and much more

Why It Matters

Instead of writing everything yourself, you can use ready-made tools.

This saves time and effort.

Common Standard Library Modules

1. math

Used for math operations.

python
import math

print(math.sqrt(16))
print(math.pi)

Output

text
4.0
3.141592653589793

2. random

Used for random choices.

python
import random

print(random.randint(1, 10))

Example Output

text
7

(Output may be different.)

3. datetime

Used for dates and time.

python
from datetime import date

today = date.today()
print(today)

Example Output

text
2026-04-30

4. os

Used for folders and file paths.

python
import os

print(os.getcwd())

Example Output

text
/Users/student/project

5. json

Used for JSON files.

python
import json

data = {"name": "Tom"}

print(json.dumps(data))

Output

text
{"name": "Tom"}

6. csv

Used for CSV files.

python
import csv

How to Explore Modules

Use:

python
dir(math)

This shows items inside the module.

Common Beginner Tip

You do not need to memorize every module.

Learn how to search documentation and use what you need.

Code Along

Import random.

Print a random number from 1 to 5.

Mini Challenge

Use:

  • math
  • random

Tasks:

  • Print square root of 81
  • Print random number from 1 to 3

Expected output example:

text
9.0
2

Real World Use Case

Developers use the standard library daily for automation, dates, math, files, reports, and data processing.

Quiz

  1. What is the standard library?
  2. Which module helps with dates?
  3. Which module helps with random values?
  4. Which module helps with JSON?

Assignment

Use three different standard library modules in one small program.

Summary

You learned that Python includes many built-in modules that save time and solve common tasks.