Python Intro

Notes 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!

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. Download the following Jupyter notebooks:

Python Practice

When you are ready to jump into some coding problems, download and complete these practice problems: w01-section-problems.ipynb. This notebook includes a few toy biological problems that will test your Python skills. Ask us questions as you work through it. If you get really stuck, here are some possible solutions: w01-section-solutions.ipynb.

Example Jupyter Notebooks

Before you start the pset next week, check out two example Jupyter Notebooks Sean has made available for some more Python example code and for some Jupyter inspiration:

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 without citing your source (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!

"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.

good_code