Python Intro
Jakob Heinz (2024)
Adapted from notes by Mary Richardson (2021)
Now that you have Anaconda installed and are able to run a Jupyter Notebook, we're ready to start coding in Python!
Downloading Jupyter Notebooks
To download Jupyter Notebooks, here are a few strategies. You'll need this each week to download example notebooks in section notes and to view solutions for the problem sets.
Option 1: From the website on a Mac
1. Click on the link (http://mcb112.org/w01/w01-python.ipynb) to load the source code of the notebook
2. Right click and select Save As...
and choose the directory where you want to download the notebook
3. Change the filename to end in .ipynb
(not .txt
) and be sure the Format
is All Files
Option 2: On the command line on a Mac or PC
1. Right click and select Copy Link Address
to copy the notebook link from the website
2. Navigate to the directory where you want to download the notebook with cd
2. Use the wget
command on the command line to download the notebook to this directory
wget http://mcb112.org/w01/w01-python.ipynb
Learning Python
Let's start with the basics of coding in Python along with a quick introduction to NumPy and MatPlotLib.
Download the following Jupyter notebook to follow along in class:
Solutions and indepth explanations can be found here. Try to avoid looking at them until after class:
Troubleshooting
What's the best way to troubleshoot when you're stuck?
-
Python Documentation: Often you will find yourself needing to look up the syntax for a specific function. I do this about 20x a day when I'm coding (really and truly)! This is where documentation is helpful. I'd recommend just googling something like "numpy sum documentation" to find the documentation page for a specific function. But if you want sources, here are links to the documentation for all the packages we'll use in MCB112:
- Python Docs: base python
- Numpy: mathematical functions ranging from basic +,-,*,/ functions to representing data as arrays and matrices
- Pandas: data analysis and visualization functions and data structures
- Matplotlib: basic plotting functions (scatterplots, histograms etc.)
- Seaborn: more complex plotting functions
- Scipy: optimization, machine learning, and other complex computing functions
-
Google: Many coding questions have already been answered online and can be found with a quick google search. A popular site to troubleshoot coding questions is Stack Overflow. You should not just copy/paste code from answers (this usually will not work for your specific problem anyway). But sites like Stack Overflow are helpful for figuring out little quirks in Python functions and also for seeing ways that other people have solved different problems.
-
Class Resources: These include the Ed Discussion for directly asking questions, as well as lecture, section, and office hours. Working together with classmates can also be super helpful. You are expected to submit your own work for each pset (your code and text should not look like your friend's submission), but we encourage you to talk to each other about ideas and help each other figure out problems along the way. That's part of science!
-
LLMs: Can be a useful tool for debugging. There is no guarantee it will be correct! It can also explain code to you and be used as a learning tool. In the end, you must figure out how much to rely on it, and for this class you should understand every line of code you turn in!
"I can't do this!"
Don't get overwhelmed trying to master all of this if you're totally new to coding (I know, I know: easier said than done)! Focus on learning to use base python to solve problems for now. We don't expect you to write beautiful, concise, modular code right now (though we hope you are inspired to, eventually). Your code just needs to work and be well explained in your notebook.