Working with Python: functions and modules

Presenters

  • Anne
  • Mukarram

Aims

This course will cover concepts and strategies for working more effectively with Python with the aim of:

  • Writing reusable code, using functions and libraries
  • Acquiring a working knowledge of key concepts which are prerequisites for advanced programming in Python e.g. writing modules and classes
  • During this course you will learn about:
    • Writing functions
    • Best practices to write reusable code
    • Structuring code in a custom module
    • Using Python libraries
    • Drawing plots with Matplotlib and working with biological data using BioPython

Learning objectives

  • Recall the basic syntax, how to print and define variables
  • List the most common data structures in Python
  • Explain how to write conditions and loops in Python
  • Practice reading and writing files with Python
  • Explain how to write user-defined functions and modules in Python
  • Use existing in-built as well as third-party Python libraries
  • Solve more complex exercises using these concepts

Course schedule

  • 09:30-10:00: [0h30] Introduction
  • 10:00-11:00: [1h00] Session 1 - Introduction to Python
  • 11:00-11:15: break
  • 11:15-13:00: [1h45] Session 2 - Functions
  • 13:00-14:00: lunch break
  • 14:00-15:15: [1h15] Session 3 - Modules
  • 15:15-15:30: break
  • 15:30-16:30: [1h00] Session 4 - Matplotlib & BioPython

Course materials

  • There are two course webpages with links to the materials, example solutions to the exercises etc.:
  • 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 that you think could be attempted using Python, we are happy to (try to) help during the exercises. Just let us know!

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 academic areas and in industry too
  • Python is available on all popular operating systems
    • Mac
    • Windows
    • Linux

The Python programming language

  • Python is considered to come with "batteries included" and the standard library (some of which we will see in this course) provides built-in support for lots of common tasks:

    • numerical & mathematical functions
    • interacting with files and the operating system
    • ...
  • There is also a wide range of external libraries for areas not covered in the standard library, such as Matplotlib the Python plotting library and the BioPython Library which provides tools for bioinformatics - we look at this later

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?

On a Mac or Linux machine you should start a terminal and then just type the command python3.

How to run Python code from a file?

Running Python code is as simple as opening a Terminal window and typing the command python3 followed by the name of the script.

python3 scripts/hello.py

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?

  • We recommend using a virtual environment after having installed Python 3.

    python3 -m venv venv
    source venv/bin/activate # activate your virtual environment
    pip install jupyter
    
  • 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?