Day 26 45 min beginner

The Standard Library Tour

Discover the powerful tools that come built-in with every Python installation.

Learning Objectives

  • Understand what the Standard Library is
  • Explore the 'sys' module for system tasks
  • Explore the 'statistics' module
  • Learn about the 'collections' module (Counter)

Batteries Included

Python is often described as having “batteries included”. This means that it comes with a large set of pre-written code called the Standard Library. You’ve already used math, random, and json. Today, we look at a few more.

The sys Module

Use this to interact with the Python runtime environment itself.

sys_example.py
1
2
3
4
5
6
7
import sys

# Get version of Python
print(f"Python Version: {sys.version}")

# Get command line arguments passed to the script
print(f"Arguments: {sys.argv}")

The statistics Module

Perfect for simple data analysis without needing complex external tools.

stats.py
1
2
3
4
5
6
import statistics

grades = [85, 90, 70, 95, 88]

print(f"Mean: {statistics.mean(grades)}")
print(f"Median: {statistics.median(grades)}")

The collections Module

Provides specialized “container” data types. Counter is one of the most useful.

counter.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from collections import Counter

inventory = ["apple", "banana", "apple", "cherry", "banana", "apple"]

# Count occurrences of items in a list
count = Counter(inventory)
print(count)
# Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})

print(count["apple"]) # 3

Why learn these?

Knowing what’s in the Standard Library prevents you from “reinventing the wheel”. Before writing a complex loop to count items or calculate an average, check if Python already has a battery for it!


Interactive Practice

Explore the time module. Can you use time.sleep(2) to make your program wait for 2 seconds before printing a message?

Module Battery For…
sys System paths and arguments
statistics Basic math analysis
collections Specialized lists/dicts
itertools Complex loops and iterations

Quiz

Complete this quiz with a minimum score of 80% to mark Day 26 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!