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.
"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)
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:
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
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.
Read print.py.
There are two built-in functions for getting keyboard input, and they are:
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.
These are also compound data types and we will study them, but let us study modules first before we continue with them.
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.pyThe 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.
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.
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.
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.
This is the end of my short tutorial on Python that would be needed for the workshop.
Have fun at the workshop!