The Python Standard Library
Overview
Python's standard library includes built-in modules for everything from data compression to web requests. You'll explore how to discover available modules, read official documentation, and apply them to practical problems, unlocking powerful tools that come with Python.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What the standard library is: Understand Python's extensive built-in module collection.
- Exploring modules: Learn how to discover and explore available modules.
- Reading documentation: Understand how to use help() and official docs.
- Popular modules: Discover commonly used modules like datetime, json, csv, and os.
Why This Matters
Python comes with hundreds of modules already installed - you don't need to write code for common tasks! Whether you need to work with dates, parse JSON, read CSV files, or interact with the operating system, Python's standard library has you covered. Learning about the standard library saves you time and helps you write more powerful programs!
Step 1: What Is the Standard Library?
The Python standard library is a collection of modules that come with every Python installation. These modules are written in Python or C and provide ready-to-use functionality for common tasks:
# All these modules come with Python - no installation needed!
import math # Mathematical functions
import random # Random number generation
import datetime # Date and time handling
import json # JSON data parsing
import csv # CSV file handling
import os # Operating system interface
import sys # System-specific parameters
Pre-installed
Standard library modules come with Python - you don't need to install them separately. They're always available!
Well Documented
Every module has official documentation explaining what it does and how to use it. You can access it with help().
Reliable
These modules are maintained by Python's core developers and are thoroughly tested. They're production-ready!
Key Concept: The standard library is like a toolbox that comes with Python. Instead of building your own tools for common tasks, you can use the ones that are already included. This saves time and ensures your code is reliable!
Mini Practice #1: Exploring Standard Library
Try It YourselfSee what's available in the standard library:
What happened? Python's standard library includes hundreds of modules for tasks like math, random numbers, dates, file handling, and more. The sys module provides system-specific information like the Python version. All these modules are available without any installation!
Step 2: Popular Standard Library Modules
Here are some commonly used modules from the standard library:
datetime
Work with dates and times.
from datetime import datetime
now = datetime.now()
json
Parse and create JSON data.
import json
data = json.loads('{"key": "value"}')
csv
Read and write CSV files.
import csv
# Read CSV files easily
os
Interact with operating system.
import os
current_dir = os.getcwd()
collections
Specialized data structures.
from collections import Counter
counts = Counter([1, 2, 2, 3])
itertools
Functions for creating iterators.
import itertools
# Advanced iteration tools
Step 3: Discovering Modules
You can explore what modules are available using Python's built-in tools:
# List all available modules
import sys
print(sys.modules.keys())
# Get help on a module
import math
help(math)
# See what's in a module
import random
print(dir(random))
Online Documentation
The official Python documentation (docs.python.org) has complete reference for all standard library modules. Each module has:
- Purpose and introduction
- Complete API reference
- Usage examples
- Best practices
Mini Practice #2: Using datetime Module
Try It YourselfTry using the datetime module:
What happened? The datetime module provides powerful tools for working with dates and times. You can get the current time, format dates, calculate differences, and much more. This is all part of Python's standard library - no installation needed!
Step 4: Reading Documentation
You can access documentation right in Python:
# Get help on a module
import math
help(math)
# Get help on a specific function
help(math.sqrt)
# See module contents
print(dir(math))
Remember: The help() function shows documentation for modules, functions, and classes. If you're not sure how to use something, try help() first! For more detailed documentation, visit docs.python.org.
End-of-Lesson Exercises
Exercise 1: Use datetime Module
Import the datetime module and get the current date and time. Print it in a formatted string like "Current time: [date and time]".
Use from datetime import datetime, then datetime.now().
Exercise 2: Explore Module Contents
Import the random module and use dir() to see what functions it contains. Then use help() to get information about the randint function. Print both results.
Use import random, then dir(random) and help(random.randint).