Python Workflows

Published

February 2026

In this course you’ll use two types of Python files: notebooks for analysis and scripts for data acquisition. Understanding when to use each is more important than which tool you use to open them.


1 Notebooks vs Scripts

1.1 Notebooks (.ipynb)

Notebooks combine code, output, and documentation in a single file. You write code in cells and run them one at a time, seeing results immediately. Use notebooks for:

  • Learning new concepts and experimenting
  • Exploratory data analysis
  • Curve fitting and making plots
  • Creating documented analysis workflows
  • Figures for lab reports

1.2 Scripts (.py)

Scripts are plain text files containing Python code that runs from start to finish. Use scripts for:

  • Real-time data acquisition
  • Automated measurements
  • Long-running experiments
  • Code that controls hardware

1.3 Why Scripts for Data Acquisition?

For data acquisition and instrument control, scripts are preferred over notebooks because:

  1. Reliability: Scripts run without the overhead of the Jupyter kernel
  2. Error handling: Easier to implement proper cleanup if something goes wrong
  3. Automation: Scripts can be run from the command line
  4. Version control: Plain text files are easier to track in Git

The provided automation scripts (like 04_beam_profiler.py) are designed to run as standalone scripts, not in notebooks.

1.4 Quick Reference

Task Use
Learning a new concept Notebook
Quick data exploration Notebook
Fitting and plotting data Notebook
Writing lab report figures Notebook
Real-time data acquisition Script
Automated measurements Script
Long-running experiments Script
Controlling hardware Script

3 File Organization

Here’s an example structure for organizing your lab work. Create a folder for each lab and keep your data, analysis notebooks, and scripts together:

phys4430/
├── gaussian-beams/
│   ├── data/              # Raw data files (CSV)
│   ├── beam_profiler.py   # Script for data acquisition
│   ├── analysis.ipynb     # Notebook for analysis
│   └── figures/           # Saved plots
├── lab-2/
│   ├── data/
│   ├── analysis.ipynb
│   └── ...
└── ...

Back to Python Resources