An introduction to solving biological problems with Python

Today's Presenters

  • Anne
  • Cristian

Learning objectives

  • Recall how to print, create variables and save Python code in files
  • List the most common data types in Python
  • Explain how to use different type of collections
  • Use and compare these concepts in different code examples
  • Propose and create solutions using these concepts in different exercises

Course schedule - day one

  • 09:30-10:00: [0h30] Introduction
  • 10:00-10:30: [0h30] Session 1.1 - Print and Variables
  • 10:30-10:45: break
  • 10:45-12:15: [1h30] Session 1.2 - Simple data types, Arithmetic and Saving code in files
  • 12:15-13:30: lunch break
  • 13:30-14:30: [1h00] Session 1.3 - Collections: Lists and String
  • 14:30-14:45: break
  • 14:45-15:45: [1h00] Session 1.4 - Collections: Sets and Dictionaries
  • 15:45-16:00: break
  • 16:00-17:00: [1h00] Wrap-up

Course schedule - day two

  • Conditions, Loops and Files

Course materials

  • There is a course webpage with links to the materials, example solutions to the exercises etc.:
  • All course materiel is available on GitHub in our python-basic repo
  • We’d like you to follow along with the example code as we go through the material, and attempt the exercises to practice what you’ve learned
  • Questions are welcome at any point!
  • If you have specific projects/problems you like to use Python for we are happy to (try to) help during the exercises

What is Python?

  • Python is a dynamic, interpreted general purpose programming language initially created by Guido van Rossum in 1991
  • It is a powerful language that supports several popular programming paradigms:
    • procedural
    • object-oriented
    • functional
  • Python is widely used in bioinformatics and scientific computing, as well as many other fields and in industry
  • Python is available on all popular operating systems
    • Macs
    • Windows
    • Linux

The Python programming language

  • Python is considered to come with "batteries included" and the standard library provides built-in support for lots of common tasks:
    • numerical & mathematical functions
    • interacting with files and the operating system
    • ...

Getting started

  • Python is an interpreted language, this means that your computer does not run Python code natively, but instead we run our code using the Python interpreter
  • There are three ways in which you can run Python code:
    • Directly typing commands into the interpreter: Good for experimenting with the language, and for some interactive work
    • Using a Jupyter notebook: Great for experimenting with the language, and for sharing and learning
    • Typing code into a file and then telling the interpreter to run the code from this file: Good for larger programs, and when you want to run the same code repeatedly

How to start the Python interpreter?

  • How you start the interpreter will depend on which operating system you are using, but on a Mac or Linux machine you should start a terminal and then just type the command python3
  • This will print out some information about your installation of python and then leave you with a command prompt which looks like >>>
  • You can then type commands and press Enter when you're done. Python will run the code you typed, and might display some output on the line below, before leaving you with another prompt.
  • If you want to exit the interactive interpreter you can type the command quit() or type Ctrl-D

The terminal

We will see later how to save code in a file and run it.

The shell command lines you may need

  • ls: to list directory contents
  • pwd: to return working directory name
  • cd to/this/directory/: to change directory
  • cat hello.py: to print the content of a text file

What is a Jupyter notebook?

  • The Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text.

  • Jupyter provides a rich architecture for interactive data science and scientific computing with:

    • Over 40 programming languages such as Python, R, Julia and Scala.
    • A browser-based notebook with support for code, rich text, math expressions, plots and other rich media.
    • Support for interactive data visualization.
    • Easy to use tools for parallel computing.

How to install Jupyter on your own computer?

  • See Installing Jupyter Notebook

  • For new users, we recommend installing Anaconda. Anaconda conveniently installs Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science.

  • Start the notebook server from the command line:

    jupyter notebook
  • You should see the notebook home page open in your web browser.

How to run python in a Jupyter notebook?