Tooling

Tooling#

This appendix is a tooling reference for the whole book. You do not need to master everything here before moving on — read it once for orientation, then return to specific sections as you need them in later chapters.

What this appendix covers

Notebook

Topic

When you’ll need it

5.1 — Files

Reading/writing files; Path()

Anytime you load or save data

5.2 — Modules

Writing and importing your own .py modules

When your code outgrows a single notebook

5.3 — Packaging

Package structure; pip; requirements.txt; virtual envs

When sharing or deploying a project

5.4 — Coding Tooling

List comprehensions; lambda; docstrings; type hints; enumerate/zip; try/except; f-string formatting

Throughout — these are everyday Python idioms

How to use this appendix

  • Browse 5.1–5.3 early so you know what’s available.

  • Treat 5.4 as a recipe book: look up a pattern when you encounter it in later chapters.

  • Treat this appendix as a reference: use it when a later chapter expects one of these practical patterns.

What is a Module?

Python organizes reusable code in three levels:

function        → one reusable operation             def square(x): ...
module          → one .py file of related functions  math_tools.py
package         → a folder of related modules        my_package/

The standard library ships hundreds of modules. You have already used several:

Module

What you used it for

math

math.sqrt(), math.pi

random

random.choice(), random.randint()

pathlib

Path() for file paths

time

time.perf_counter() for benchmarking

In 5.2 you will write your own module. In 5.3 you will learn how to install modules others have written.

Importing Patterns

import math                        # import the whole module; access via math.sqrt()
from math import sqrt              # import one name; access as sqrt()
from math import sqrt, pi         # import multiple names
from math import sqrt as sq        # import with an alias
import math as m                   # rename the module itself

Prefer import module over from module import * — the star import pollutes the namespace and makes it hard to tell where a name came from.