</>
Vizly

Python Modules, File I/O & JSON

July 3, 20263 min
PythonFundamentalsPracticeBeginner

Chapter 9 of Python Fundamentals. 14 practice drills on imports, the __main__ guard, file modes, pathlib, and JSON round trips. Predict the output before you run anything.

The rules, again

Same game as chapter 1: read the question, commit to a prediction, run it, and only then open the answer. Wrong prediction means the drill goes on your redo list for three days from now.

This chapter is about the boundaries: your code meeting other files, the disk, and other systems. The traps here are quiet and expensive. import runs code you did not ask for, mode "w" deletes before you write, and a JSON round trip hands back data that is almost, but not exactly, what you saved.

Learn the concepts first

Rusty on the basics? Work through Modules and Packages on learnpython.org first, then come back and drill.


One idea before you start

Most import surprises stop being surprising once you know what a module actually is.

Definition

A module is just a .py file. Importing it executes the whole file once, top to bottom, and caches the resulting module object in sys.modules; every later import gets the cached one. There is no separate declaration language: def and class are ordinary statements that happen to run at import time.


The drills

Click a question to open it. The answer stays hidden until you ask for it. Predict first.

14 drills. Predict first, then open the answer

Want more drills?

When these stop surprising you, let an AI generate fresh ones. Copy this prompt into ChatGPT, Claude, or any assistant you like:

You are my Python drill coach. Quiz me on: imports and module caching,
the __main__ guard, from-import binding semantics, file modes and
truncation, reading files (cursor, line iteration), pathlib, and JSON
serialization (dumps/loads, round-trip type changes).
 
Rules:
- Ask ONE question at a time. Show a short code snippet and ask me to
  predict the output (or the error) before running it.
- Wait for my answer. Do not reveal anything until I reply.
- If I am right, say so in one line and raise the difficulty slightly.
- If I am wrong, explain the why in 2-3 sentences, then give me a
  similar question later to check I really got it.
- Prefer tricky edge cases over textbook questions: importing twice,
  naming a script random.py, mode "w" truncating at open, f.read()
  twice, int dict keys after a JSON round trip, datetime in json.dumps,
  platform-default encodings.
- Keep score. After 10 questions, list the concepts I missed so I can
  add them to my redo list.
 
Start with a medium question.

The predict-first rule matters more than the tool. Whatever generates the questions, always commit to an answer before you look.


Done?

Score yourself honestly and queue up your misses. The JSON round-trip drill (number 11) is the one to internalize before your next API project: int keys become strings, tuples become lists, silently, every time. And if drill 4 (the file named random.py) made you smile in recognition, you have debugged that afternoon already.

Next chapter: the standard library's power tools, where collections, itertools, and re replace half the helper functions you have been writing by hand.

Edit this page on GitHubโ†—