Course contentsShow
← Data Science
Lesson 2071 of 2,14547. Reproducible ResearchFree lesson

Modular Code: Functions and Scripts

Move reusable code from notebooks into Python modules and scripts for cleaner, testable analysis.

Modular Code: Functions and Scripts

You'll learn how to extract reusable logic from notebooks into Python modules and scripts to make your data science work cleaner, testable, and truly reproducible.

Why Move Code Out of Notebooks?

Jupyter notebooks are fantastic for exploration, but they become messy when analysis code gets copied across cells or projects. Think of it like cooking: you wouldn't write the recipe for making pasta sauce inside every dinner plan — you'd reference a tested sauce recipe you can reuse.

From Notebook Cells to Python Modules

When you spot code you're using repeatedly (data cleaning functions, custom plotting helpers, model evaluation utilities), extract it into a .py file. This Python module lives in your project directory and can be imported anywhere.

Example structure:

project/
├── notebooks/
│   └── analysis.ipynb
├── src/
│   ├── __init__.py
│   ├── preprocessing.py    # Your reusable functions
│   └── visualization.py
└── scripts/
    └── run_analysis.py      # End-to-end automation script

In your notebook, instead of 50 lines of data cleaning code cluttering your analysis, you write:

from src.preprocessing import clean_sensor_data

df_clean = clean_sensor_data(df_raw)

Scripts vs. Modules

Modules (.py files with functions) are libraries you import. Scripts are executable files that run a complete workflow — useful for automation and reproducibility. Someone can run your entire analysis pipeline with python scripts/run_analysis.py instead of clicking through notebook cells.

The Benefits

Clean notebooks become documentation of what you discovered, while modules contain how you processed data. Your functions become testable, shareable across projects, and version-controlled more cleanly than notebook JSON.

Key Takeaway: Modular code transforms notebooks from tangled execution logs into clear narratives, with the messy implementation details living in tested, reusable Python modules and scripts.