A Quick Tutorial on the Programming Language Python (with NumPy)

September 20, 2011

Art Lee

This is a short tutorial that I prepared on Python for the Bio 133 students at the Claremont Colleges in the Fall of 2011. If you find it useful, feel free to read it.

P Y T H O N

"Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for rapid application development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed." (from the Python Official Website)

1. Installing Python on your machine

I assume that you have access to an installation of Python, which would be the case if you are a Bio 133 student.

Note that:

2. Learning the language Python

There is an excellent book available free online that will teach you the basics of Python and some important concepts in computer science at the same time. It is: Think Python: How to Think Like a Computer Scientist by Allen B. Downey, which I will refer to as [Downey] in the remainder of this tutorial.

Think Python is a new book based on the original book: How to Think Like a Computer Scientist: Learning with Python, 2nd Edition, Jeffrey Elkner, Allen Downey, and Chris Meyers, which I will refer to as [DEM] as I make some references to some parts of the original one in the remainder of this tutorial. You may find the original book useful as well.

I also found an online tutorial on Python, which may be a good supplement.

You can find other interesting information (e.g., NumPy, SciPy, etc.) on scientific computing here

3. Several different ways to run a Python interpreter

  1. Interactive mode whether you are using python or ipython. You first start a python session using a terminal window. Then, read this for detail.
  2. Script mode. You would prepare a file containing a Python program and execute (or run) the program in the script file. Read this.
  3. Whether you are using an interactive mode or a script mode, you can import other modules into your python program. See the "Modules" section later for detail.

4. Basic data type values that programs manipulate

Note: I am not adding very much explanation on these simple concepts since I am assuming that you would already be familiar with them. If not, the book by Downey [Downey] is a good source to find more information about them. In fact, I would strongly recommend that you keep a copy of [Downey] next to you as you read my tutorial. For those students who will come to my lectures, I will describe them in more detail.

5. Comments

Any line starting with a pound sign (#) in your Python program is considered a comment and will be ignored by the Python interpreter. I will often add my explanation as a comment in a program file as you will see in types.py next.

6. Basic types of values we use in Python (or programming in general)

Read types.py first.

Types such as int, float, boolean are called primitive data types. Later we will see some compound data types such as string, list, tuples, etc. Actually we already saw strings.

7. Use of the 'print' command in a script file

Read print.py.

8. Variables

9. Operating on values: operators and operands

Combining simple data to form more complex ones. Read operators.py.

10. Order of precedence among operators

11. Input from keyboard

There are two built-in functions for getting keyboard input, and they are:

  1. raw_input
  2. input
Read input.py for detail.

12. Functions

Read functions.py.

13. Conditionals: doing it conditionally

Read conditionals.py

14. Loops: doing it repeatedly

Read loops.py

15. Compound data types: strings, lists, tuples

So far we have mainly dealt with primitive data type values (and a little bit about strings). That is, we have seen the following four data types: int, float, bool, and string.

Strings are compound data whereas the other three are atomic. That is, a string is made up of smaller pieces, characters. To be more precise, a string is a sequence of characters.

Our main focus so far has been learning the basic language constructs and programming elements such as operators, conditionals, loops, functions, input, etc. using simple data types.

Using the basic language constructs that we have already learned, we can enrich the data types. We do so by learning compound data types. The compound data types we will learn include strings (in more detail), lists, and tuples.

To write interesting programs, we will need to know these compound data types as well.

Let us do it.

16. Strings

Read strings.py

17. Lists

Read lists.py

18. Tuples and Dictionaries

These are also compound data types and we will study them, but let us study modules first before we continue with them.

19. Modules

  1. Read modules.py
  2. Read seqtools.py
  3. Read init.py

20. Tuples

Read tuples.py

21. Dictionaries

Read dictionaries.py

22. Files

We have now seen enough basic data structures that you will need in Python to write some interesting programs.

We will now study how to deal with data in a file.

Read files.py

23. Exception handling

Read exceptions.py

24. Classes and objects

25. Arrays

The array module defines a new object type, array, that works almost exactly like other sequence types, except that its contents are constrained to a single type. That is, only one type of elements can be included in an array.

The type of an array is determined at the time of creation.

This module is used to create large lists in a storage-efficient manner. The resulting arrays are not suitable for serious numeric computation due to its inefficient implementation though.

To create storage- and calculation-efficient arrays, I suggest you use NumPy arrays instead, which I will present below. You will see arrays.txt when we discuss NumPy arrays below.

Another key difference: the Standard Python class array is only for one-dimensional arrays, whereas the multidimensional array class called ndarray in NumPy is obviously multidimensional.

26. Python, NumPy, and SciPy: Why All These?

The Python language provides an array package, but it supports only one-dimensional arrays. This package is also rather inefficient for numeric computation.

NumPy provides among other things an n-dimensional array package (ndarray) that is convenient and fast. It basically relies on an efficient implementation done in another language. Python uses that efficient implementation inside Python. If we were to classify programming languages into 'fast' ones and 'slow' ones, Python would be considered as one of the 'slow' ones. When you deal with computations that require speed in Python, Python relies on the help of other 'fast' languages such as C, C++, Fortran. NumPy is one such example.

Later, you will see SciPy which is open-source software for mathematics, science, and engineering. SciPy is built using a library which relies on NumPy arrays and provides many user-friendly and efficient numerical routines such as numerical integration and optimization.

27. NumPy

There is a document: Guide to Numpy by Travis E. Oliphant.

A piece of warning first from the document:

    page 38 No. 4 Default data type in NumPy states:

    The default data-type in NumPy is float unlike in Numeric (and
    numpy.oldnumeric) where it was int.  There are several functions
    affected by this so that if your code was relying on the default
    data-type, then it must be changed explicitly by adding
    dtype = int.

Read arrays.txt for an introduction to NumPy arrays.

28. SciPy

See the section Python, NumPy, and SciPy above to see what it is.

I will not cover it in this tutorial. Instead I will refer you to an online tutorial on SciPy.

As a preparation for the upcoming workshop, I believe the material on NumPy would be sufficient.

29. Some examples putting all these together

This is the end of my short tutorial on Python that would be needed for the workshop.

Have fun at the workshop!