PHYS 4430
Physics Undergraduate Labs
Python Workflows
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:
- Reliability: Scripts run without the overhead of the Jupyter kernel
- Error handling: Easier to implement proper cleanup if something goes wrong
- Automation: Scripts can be run from the command line
- 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 |
2 Recommended Tools
2.1 VS Code (Recommended)
VS Code handles both notebooks and scripts, making it our recommended environment. It’s already installed on the lab computers. If you want to install it on your personal computer, download it from code.visualstudio.com.
To run a notebook: Open any .ipynb file and run cells with Shift+Enter.
To run a script: Open a .py file and click the play button (top right) or press F5.
2.1.1 Required Extensions
Install these extensions (click the Extensions icon in the left sidebar or press Ctrl+Shift+X):
| Extension | Publisher | Purpose |
|---|---|---|
| Python | Microsoft | Python language support |
| Pylance | Microsoft | Fast, feature-rich language server |
| Jupyter | Microsoft | Run notebooks in VS Code |
2.1.2 Useful Shortcuts
- Run cell:
Shift+Enter - Select Python interpreter: Click the Python version in the bottom status bar
- Open terminal:
Ctrl+` - Command palette:
Ctrl+Shift+P - Go to definition:
F12orCtrl+Click
2.2 JupyterLab (Alternative)
JupyterLab is a browser-based environment for running notebooks. It’s a good alternative if you prefer the classic notebook experience.
To start JupyterLab:
py -m jupyter labThis opens a browser window where you can create and run notebooks.
2.2.1 Useful Shortcuts
- Run cell:
Shift+Enter - Add cell above/below:
A/B(in command mode) - Delete cell:
D D(in command mode) - Switch to Markdown:
M(in command mode)
JupyterLab only runs notebooks. For scripts, use VS Code.
2.3 Google Colab (Alternative)
Google Colab runs notebooks in your browser with no software installation required. It’s a good option if you’re already familiar with it or want to work from a computer without Python installed.
To use Colab:
- Go to colab.research.google.com
- Sign in with your Google account
- Create a new notebook or upload an existing
.ipynbfile
Colab runs on Google’s servers, so it cannot communicate with lab hardware. Use Colab only for data analysis—you’ll need to collect data using the lab computers and then upload your data files to Colab for analysis.
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
│ └── ...
└── ...