Difference between revisions of "Phylogenetics: Python Primer"

From EEBedia
Jump to: navigation, search
(Windows users)
Line 7: Line 7:
 
|}
 
|}
  
=== What is Python? ===
+
== What is Python? ==
 
[http://www.python.org/ Python] is one of two programming languages that we will use this semester (the other being [http://www.r-project.org/ R]). One might make the case that programs like PAUP that are associated with a unique command language also represent programming languages; however, Python and R are different in being general purpose (i.e. not written specifically for phylogenetics).
 
[http://www.python.org/ Python] is one of two programming languages that we will use this semester (the other being [http://www.r-project.org/ R]). One might make the case that programs like PAUP that are associated with a unique command language also represent programming languages; however, Python and R are different in being general purpose (i.e. not written specifically for phylogenetics).
  
 
Python is one of a number of different high-level computing languages in common use. All of the software we use is written in one of these languages. PAUP is written entirely in the C language, while SplitsTree is written in Java. Knowing a little bit of computer programming can save you immense amounts of time by allowing you to automate things. You will being realizing these savings doing homework for this class. While it is possible to do all the homework assignments by hand using a calculator, you will find that using Python will save you time and is more accurate (a mistake on a calculator early on in a calculation can be very costly in terms of time and accuracy). Python is a good language to learn first because it is relatively simple (not many words or punctuation rules to learn) and is much more forgiving than other languages. It is in a class of languages known as scripting languages because the program is interpreted as it is read - languages such as C require two additional steps (compiling and linking) before they can be run.
 
Python is one of a number of different high-level computing languages in common use. All of the software we use is written in one of these languages. PAUP is written entirely in the C language, while SplitsTree is written in Java. Knowing a little bit of computer programming can save you immense amounts of time by allowing you to automate things. You will being realizing these savings doing homework for this class. While it is possible to do all the homework assignments by hand using a calculator, you will find that using Python will save you time and is more accurate (a mistake on a calculator early on in a calculation can be very costly in terms of time and accuracy). Python is a good language to learn first because it is relatively simple (not many words or punctuation rules to learn) and is much more forgiving than other languages. It is in a class of languages known as scripting languages because the program is interpreted as it is read - languages such as C require two additional steps (compiling and linking) before they can be run.
  
=== Installing Python ===
+
== Installing Python ==
  
==== Mac users ====
+
=== Mac users ===
  
 
[[Image:Mac logo.jpg|left]] If you have a Mac, then Python is already installed on your computer because it is used in many parts of the Mac OS X operating system. To verify this, and to see what version of Python you have, start your Terminal program (in the ''Applications/Utilities'' folder), then type the following at the unix prompt (note: the prompt is represented by a dollar sign ($), so you should only type the "python -V" part):<br style="clear:both" />
 
[[Image:Mac logo.jpg|left]] If you have a Mac, then Python is already installed on your computer because it is used in many parts of the Mac OS X operating system. To verify this, and to see what version of Python you have, start your Terminal program (in the ''Applications/Utilities'' folder), then type the following at the unix prompt (note: the prompt is represented by a dollar sign ($), so you should only type the "python -V" part):<br style="clear:both" />
Line 20: Line 20:
 
  Python 2.5.1
 
  Python 2.5.1
  
==== Windows users ====
+
=== Windows users ===
  
 
[[Image:Win logo.png|left]] If you have Windows, you will need to download and install Python from the http://www.python.org/download/ web site. Note that there are two current versions of Python. You should install the older one (Python 2.6.1). (We would use Python 3.0 except that some Python software that we will use later in the semester is not yet compatible with Python 3.0, so it makes sense to stick with 2.6 for now.) There are several different types of downloads: choose '''Python 2.6.1 Windows installer (Windows binary -- does not include source)''' and follow the directions once you start it up.
 
[[Image:Win logo.png|left]] If you have Windows, you will need to download and install Python from the http://www.python.org/download/ web site. Note that there are two current versions of Python. You should install the older one (Python 2.6.1). (We would use Python 3.0 except that some Python software that we will use later in the semester is not yet compatible with Python 3.0, so it makes sense to stick with 2.6 for now.) There are several different types of downloads: choose '''Python 2.6.1 Windows installer (Windows binary -- does not include source)''' and follow the directions once you start it up.
  
Once you have Python installed, you can invoke it from a console window (in Vista, use Start, All Programs, Accessories, Command Prompt) by typing <tt>python</tt>: <br style="clear:both" />
+
Once you have Python installed, you can invoke it from a console window (in Vista, use Start, All Programs, Accessories, Command Prompt) by typing <tt>python</tt>. The <tt>-V</tt> option causes Python to print out its version number and then quit: <br style="clear:both" />
 
  C:\Users\Administrator>python -V
 
  C:\Users\Administrator>python -V
 
  Python 2.6.1
 
  Python 2.6.1
  
==== Everyone ====
+
=== Everyone ===
  
 
You should download the documentation from http://docs.python.org/download.html so that it can be accessed quickly. I find the HTML form best: I unpacked the zip file and bookmarked it in my browser as follows:
 
You should download the documentation from http://docs.python.org/download.html so that it can be accessed quickly. I find the HTML form best: I unpacked the zip file and bookmarked it in my browser as follows:
 
  file:///Users/plewis/Documents/Manuals/python-html/index.html
 
  file:///Users/plewis/Documents/Manuals/python-html/index.html
 +
 +
== Python basics ==
 +
 +
This is the briefest of introductions, designed to get you just to the point where you can do the majority of your homework assignments using Python. If you get stuck trying to write a Python program, I have found the Tutorial and Global Index to be the most useful parts of the Python documentation.
 +
 +
=== Kinds of information you can store in Python variables ===
 +
 +
Integers are whole positive or negative numbers. Floats are numbers with an implicit or explicit decimal point. Strings are series of characters (i.e. words or sentences). Lists are collections of integers, floats, strings, etc. Finally, tuples are like lists, except that you cannot change anything in a tuple. Here are some examples. The >>> represents the Python prompt (you will see this when you start Python), so don't type that! The output is shown below each Python statement that generates output (don't type that either!). Finally, everything after a # character represents a comment (while you can type these in, it would probably be a waste of your time).
 +
 +
>>> i = 9                      # assign the integer 9 to the variable i (you get to choose the variable names)
 +
>>> f = 9.5                    # assign the float 9.5 to the variable f
 +
>>> s = "Have a nice day"      # assign the string "Have a nice day" to the variable s
 +
>>> L = [i, f, s]              # a list consisting of the integer, the float and the string we just defined
 +
>>> t = (i, f, s)              # a tuple consisting of the integer, the float and the string we just defined
 +
>>> L                          # this causes Python to show you the contents of L
 +
>>> [9, 9.5, "Have a nice day"]
 +
>>> L[0] = 5                    # change the first value in the list to 5
 +
>>> L[-1] = "Ok"                # change the last value in the list to "Ok"
 +
>>> L                          # show the list L
 +
>>> [5, 9.5, "Ok"]              # note that you can modify the elements of a list
 +
>>> t[0] = 5                    # try changing a tuple
 +
>>> t                          # show the tuple t
 +
>>> (9, 9.5, "Have a nice day") # can't change the elements of a tuple
 +
 +
 +
=== ===
 +
=== ===
 +
=== ===
 +
=== ===
 +
=== ===

Revision as of 13:46, 4 February 2009

Adiantum.png EEB 349: Phylogenetics
This lab represents an introduction to the Python computing language, which will be useful for upcoming homework assignments as well as for software written as Python extensions (i.e. Phycas)

What is Python?

Python is one of two programming languages that we will use this semester (the other being R). One might make the case that programs like PAUP that are associated with a unique command language also represent programming languages; however, Python and R are different in being general purpose (i.e. not written specifically for phylogenetics).

Python is one of a number of different high-level computing languages in common use. All of the software we use is written in one of these languages. PAUP is written entirely in the C language, while SplitsTree is written in Java. Knowing a little bit of computer programming can save you immense amounts of time by allowing you to automate things. You will being realizing these savings doing homework for this class. While it is possible to do all the homework assignments by hand using a calculator, you will find that using Python will save you time and is more accurate (a mistake on a calculator early on in a calculation can be very costly in terms of time and accuracy). Python is a good language to learn first because it is relatively simple (not many words or punctuation rules to learn) and is much more forgiving than other languages. It is in a class of languages known as scripting languages because the program is interpreted as it is read - languages such as C require two additional steps (compiling and linking) before they can be run.

Installing Python

Mac users

Mac logo.jpg
If you have a Mac, then Python is already installed on your computer because it is used in many parts of the Mac OS X operating system. To verify this, and to see what version of Python you have, start your Terminal program (in the Applications/Utilities folder), then type the following at the unix prompt (note: the prompt is represented by a dollar sign ($), so you should only type the "python -V" part):
$ python -V
Python 2.5.1

Windows users

Win logo.png
If you have Windows, you will need to download and install Python from the http://www.python.org/download/ web site. Note that there are two current versions of Python. You should install the older one (Python 2.6.1). (We would use Python 3.0 except that some Python software that we will use later in the semester is not yet compatible with Python 3.0, so it makes sense to stick with 2.6 for now.) There are several different types of downloads: choose Python 2.6.1 Windows installer (Windows binary -- does not include source) and follow the directions once you start it up.

Once you have Python installed, you can invoke it from a console window (in Vista, use Start, All Programs, Accessories, Command Prompt) by typing python. The -V option causes Python to print out its version number and then quit:

C:\Users\Administrator>python -V
Python 2.6.1

Everyone

You should download the documentation from http://docs.python.org/download.html so that it can be accessed quickly. I find the HTML form best: I unpacked the zip file and bookmarked it in my browser as follows:

file:///Users/plewis/Documents/Manuals/python-html/index.html

Python basics

This is the briefest of introductions, designed to get you just to the point where you can do the majority of your homework assignments using Python. If you get stuck trying to write a Python program, I have found the Tutorial and Global Index to be the most useful parts of the Python documentation.

Kinds of information you can store in Python variables

Integers are whole positive or negative numbers. Floats are numbers with an implicit or explicit decimal point. Strings are series of characters (i.e. words or sentences). Lists are collections of integers, floats, strings, etc. Finally, tuples are like lists, except that you cannot change anything in a tuple. Here are some examples. The >>> represents the Python prompt (you will see this when you start Python), so don't type that! The output is shown below each Python statement that generates output (don't type that either!). Finally, everything after a # character represents a comment (while you can type these in, it would probably be a waste of your time).

>>> i = 9                       # assign the integer 9 to the variable i (you get to choose the variable names)
>>> f = 9.5                     # assign the float 9.5 to the variable f
>>> s = "Have a nice day"       # assign the string "Have a nice day" to the variable s
>>> L = [i, f, s]               # a list consisting of the integer, the float and the string we just defined
>>> t = (i, f, s)               # a tuple consisting of the integer, the float and the string we just defined
>>> L                           # this causes Python to show you the contents of L
>>> [9, 9.5, "Have a nice day"]
>>> L[0] = 5                    # change the first value in the list to 5
>>> L[-1] = "Ok"                # change the last value in the list to "Ok"
>>> L                           # show the list L
>>> [5, 9.5, "Ok"]              # note that you can modify the elements of a list
>>> t[0] = 5                    # try changing a tuple
>>> t                           # show the tuple t
>>> (9, 9.5, "Have a nice day") # can't change the elements of a tuple