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.
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:
Scripts are plain text files containing Python code that runs from start to finish. Use scripts for:
For data acquisition and instrument control, scripts are preferred over notebooks because:
The provided automation scripts (like 04_beam_profiler.py) are designed to run as standalone scripts, not in notebooks.
| 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 |
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.
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 |
Shift+EnterCtrl+`Ctrl+Shift+PF12 or Ctrl+ClickJupyterLab 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 lab
This opens a browser window where you can create and run notebooks.
Shift+EnterA / B (in command mode)D D (in command mode)M (in command mode)Note: JupyterLab only runs notebooks. For scripts, use VS Code.
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:
.ipynb fileLimitation: 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.
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
│ └── ...
└── ...