Skip to content

Standard Library and JSON

By the end of this lesson you'll be able to:

  • Create your own Python module and use the __name__ == "__main__" guard
  • Load and save data in JSON format using json.loads(), json.dumps(), json.load(), and json.dump()
  • Use key standard library modules: sys, os, collections, itertools, and datetime
  • Check whether a file exists and handle file paths with os.path

Python ships with a massive library of ready-to-use modules. This chapter explores the most useful ones and teaches you how to organize your own code into modules.

Welcome to Chapter 27!

Monty waving welcome The Python standard library is like a huge toolbox — it comes free with every Python installation. Today we'll explore the most useful tools and learn to work with JSON, the universal data format of the web. Let's code it together!

Creating Custom Modules

A module is just a .py file that contains functions and variables you want to reuse. Any Python file can be imported by another file in the same directory.

Suppose you create mymath.py:

1
2
3
4
5
6
7
8
# mymath.py
PI = 3.14159

def circle_area(r):
    return PI * r * r

def circle_circumference(r):
    return 2 * PI * r

Then in your main program:

1
2
import mymath
print(mymath.circle_area(5))

The __name__ == "__main__" Guard

When Python imports a module, it runs all the code at the top level. This can cause problems if your module contains test code you only want to run directly — not when imported.

The __name__ == "__main__" guard wraps test code so it only runs when the file is executed directly:

1
2
3
4
5
6
7
8
# mymath.py
def circle_area(r):
    return 3.14159 * r * r

if __name__ == "__main__":
    # This block only runs when you run mymath.py directly
    # It does NOT run when you do: import mymath
    print("Testing circle_area:", circle_area(5))

Always put test code and script logic inside this guard in module files.

The sys Module

The sys module gives you information about the Python interpreter itself.

Attribute/Function What it provides
sys.path List of directories Python searches for modules
sys.version Python version string
sys.argv Command-line arguments passed to the script
sys.exit() Exits the program immediately
sys.stdin, sys.stdout Standard input/output streams

  

The os Module and File Path Handling

The os module lets Python talk to the operating system — creating directories, listing files, working with file paths.

os.path is the sub-module for file path operations. Before we try them, here are the key functions:

Function Purpose
os.getcwd() Get current working directory
os.listdir(path) List files in a directory
os.path.exists(path) Check if a file/folder exists
os.path.join(a, b) Join path parts safely (works on Windows and Mac)
os.path.basename(path) Get just the filename from a path
os.path.dirname(path) Get just the directory part
1
2
3
4
5
import os

print(os.getcwd())                          # current directory
print(os.path.exists("notes.txt"))          # True or False
print(os.path.join("folder", "file.txt"))   # "folder/file.txt"

JSON — The Universal Data Format

JSON (JavaScript Object Notation) is a text format for storing structured data. It looks almost exactly like Python dictionaries and lists:

1
2
3
4
5
6
{
  "name": "Alice",
  "age": 14,
  "scores": [95, 87, 91],
  "active": true
}

JSON is used everywhere — web APIs, configuration files, databases, and data exchange between programs. Python's json module converts between JSON strings and Python objects.

Python type JSON type
dict object {}
list array []
str string ""
int, float number
True/False true/false
None null

json.loads() and json.dumps()

Two functions convert between JSON text and Python objects:

  • json.loads(text)loads JSON from a string into Python objects ("loads" = load string)
  • json.dumps(obj)dumps Python objects to a JSON string ("dumps" = dump string)

What Do You Think Will Happen?

Monty thinking The code below converts a JSON string to a Python dictionary, then back to JSON. Will the two JSON strings look identical? Are there any formatting differences? Make your guess — then run it!


  

indent=2 makes the JSON human-readable with 2-space indentation.

See It: The Type Mapper

JSON looks almost exactly like Python — which is exactly why it trips people up. In the mapper below, edit the JSON on the left and watch what json.loads() builds on the right. Before you touch it, predict: what do JSON's true and null become in Python?

Explore the JSON to Python Type Mapper MicroSim

Click The tricky trio preset to see all three famous differences at once, then try both Broken presets — single quotes and trailing commas are legal in Python but crash JSON parsing. Hover any colored value on the right to see its type names in both worlds.

Reading and Writing JSON from Files

json.load(file) reads JSON directly from an open file. json.dump(data, file) writes JSON directly to an open file.

These require a real Python environment (Thonny, VS Code) because they need actual disk access.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import json

# Write JSON to a file (run in Thonny or VS Code):
data = {"players": ["Alice", "Bob"], "high_score": 1500}
with open("game.json", "w") as f:
    json.dump(data, f, indent=2)

# Read JSON from a file:
with open("game.json") as f:
    loaded = json.load(f)

print(loaded["high_score"])   # 1500

In the Skulpt lab below we use json.dumps()json.loads() to demonstrate the same round-trip without needing a file on disk.


  

The collections Module

collections provides specialized container types:

  • Counter — count how many times each item appears
  • defaultdict — a dict that creates default values automatically
  • OrderedDict — a dict that remembers insertion order (less needed since Python 3.7)
  • deque — a double-ended queue that's faster than a list for adding/removing from both ends
1
2
3
4
5
6
7
from collections import Counter

words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
count = Counter(words)
print(count)             # Counter({'apple': 3, 'banana': 2, 'cherry': 1})
print(count["apple"])    # 3
print(count.most_common(2))  # [('apple', 3), ('banana', 2)]

The datetime Module

datetime provides date and time objects:

1
2
3
4
5
6
7
8
from datetime import datetime, date

now = datetime.now()
print(now.year, now.month, now.day)
print(now.strftime("%Y-%m-%d %H:%M:%S"))   # formatted string

today = date.today()
print(today)

The itertools Module

itertools provides efficient tools for working with iterators:

  • itertools.chain(a, b) — join two iterables end-to-end
  • itertools.combinations(seq, r) — all r-length combinations
  • itertools.permutations(seq, r) — all r-length permutations
  • itertools.cycle(seq) — repeat a sequence forever
1
2
3
4
5
from itertools import combinations

players = ["A", "B", "C", "D"]
pairs = list(combinations(players, 2))
print(pairs)   # [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]

Learning Check

Your Turn — Use Counter

Monty thinking The code below counts words in a sentence, but uses the manual dict.get() pattern from Chapter 21. Replace it with Counter from the collections module to count the same words in fewer lines!


  

Experiments

Try these changes. Predict what will happen first, then run it to check!

  1. Use json.dumps({"active": True, "count": None}, indent=2). Print the result and notice how Python's True and None become JSON's true and null. You'll know it worked when you see true and null in the output.

  2. Use Counter to find the most common character in the string "mississippi". You'll know it worked when you see "i" is the most common with count 4.

  3. Use json.loads() to parse '[1, 2, 3, 4, 5]' (a JSON array) into a Python list, then print its sum. You'll know it worked when you see 15.

  4. Use datetime.now().strftime("%A") to print today's day of the week. You'll know it worked when you see the current weekday name.

  5. Use itertools.combinations("ABCD", 2) to list all 6 two-letter combinations. You'll know it worked when you see 6 tuples.

Standard Library Pro!

Monty celebrating You've explored Python's vast standard library and mastered JSON — the language of the web! These tools will appear in virtually every real-world Python project you work on. You're thinking like a professional developer now. Let's keep coding!

Take the Chapter Review Quiz

See Annotated References