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(), andjson.dump() - Use key standard library modules:
sys,os,collections,itertools, anddatetime - 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!
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 | |
Then in your main program:
1 2 | |
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 | |
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 | |
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 | |
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?
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 | |
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 appearsdefaultdict— a dict that creates default values automaticallyOrderedDict— 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 | |
The datetime Module¶
datetime provides date and time objects:
1 2 3 4 5 6 7 8 | |
The itertools Module¶
itertools provides efficient tools for working with iterators:
itertools.chain(a, b)— join two iterables end-to-enditertools.combinations(seq, r)— all r-length combinationsitertools.permutations(seq, r)— all r-length permutationsitertools.cycle(seq)— repeat a sequence forever
1 2 3 4 5 | |
Learning Check¶
Your Turn — Use Counter
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!
-
Use
json.dumps({"active": True, "count": None}, indent=2). Print the result and notice how Python'sTrueandNonebecome JSON'strueandnull. You'll know it worked when you seetrueandnullin the output. -
Use
Counterto 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. -
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 see15. -
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. -
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!
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!