Module 3 Lesson 9: Python Standard Library
Explore the 'Batteries Included' philosophy of Python. Learn about the most useful built-in modules for everything from math to data networking.
Module 3 Lesson 9: Python Standard Library
When people say Python is "Batteries Included," they mean that it comes pre-installed with a massive library of modules ready to use. You don't need to download anything extra to perform complex math, handle dates, or even download files from the internet. In this lesson, we will tour the most useful parts of the Python Standard Library.
Lesson Overview
In this lesson, we will cover:
datetime: Working with time, dates, and time zones.osandpathlib: Navigating your computer’s file system.statistics: Performing basic data analysis.json: Reading and writing the web's most popular data format.shutil: Copying, moving, and deleting files.
1. Dating with datetime
Dealing with leap years, time zones, and formats is a nightmare. The datetime module makes it easy.
from datetime import datetime
now = datetime.now()
print(f"Today is: {now.strftime('%B %d, %Y')}")
# Calculate how many days until your birthday
birthday = datetime(2025, 12, 31)
countdown = birthday - now
print(f"Days left: {countdown.days}")
2. Working with your OS (os and pathlib)
Need to know what's in a folder or move a file? Python handles this safely across Windows, Mac, and Linux.
import os
# List all files in the current folder
print(os.listdir("."))
# Get the path to your current working directory
print(os.getcwd())
3. Math and Statistics
Beyond the math module, the statistics module is perfect for quick data insights.
import statistics
grades = [88, 92, 75, 95, 80]
print(f"Mean (Average): {statistics.mean(grades)}")
print(f"Median: {statistics.median(grades)}")
4. The json Module
JSON is the language of the web. If you want to save a dictionary into a file that other apps can read, you use JSON.
import json
user = {"name": "Alex", "id": 12345}
# Convert dictionary to a JSON string
user_json = json.dumps(user)
print(user_json) # Output: {"name": "Alex", "id": 12345}
Practice Exercise: The File & Folder Tool
Create a file named sys_tool.py.
- Import
os,datetime, andstatistics. - Print the names of all files in your current directory.
- Print the current time in the format:
HH:MM:SS. - Create a list of 10 random numbers and print their
meanandstandard deviation. (Hint: userandom.randintfor the numbers).
Quick Knowledge Check
- What does the phrase "Batteries Included" mean in Python?
- Which module would you use to find the current date?
- Why is the
statisticsmodule useful for beginners? - What type of data format is the
jsonmodule used for?
Key Takeaways
- The Standard Library is a huge collection of built-in modules.
datetimeis the go-to for time-based calculations.osallows your Python script to interact with your computer.statisticsandjsonare essential for data-driven applications.
What’s Next?
We’ve learned everything we need to build complex systems. But as you add more functions and modules, your code can get messy. In our final lesson of this module, Lesson 10, we’ll learn Refactoring Code—the art of cleaning up your work!