Python Intro

Boyan Li (2026) Adapted from notes by Mary Richardson (2021) and Jakob Heinz (2024)

Now that you have Anaconda installed and are able to run a Jupyter Notebook, we're ready to start coding in Python!

funny cartoon of 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/w00/w00-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/w00/w00-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: Python has terrific online documentation for all of its functions. Python packages, which we'll talk about next week, also (usually) have their own documentation pages. These pages include descriptions and examples.

  • Google: Many coding questions have already been answered online and can be found with a quick search. A popular site to troubleshoot coding questions is Stack Overflow. You should not just copy/paste code (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 Piazza page 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.

cartoon flowchart joke of good_code