Python

1

This page serves as both an introduction to python for an introduction to some basic programming concepts (without any code), check out the Programming Concepts page. There are other tutorials at the end of this page if you would like to find some more info.

Python is a very versatile programming language that I can safely say that I use everyday for tasks from scientific programming (phylogenetics, next generation analyses, etc) to basic tasks like formatting or renaming files and folders. You can use it to download things from websites and format them to your liking (from NCBI, GBIF, etc.). You can also use it for statistical and probabilistic analyses.

Despite the fact that it is versitile and flexible, python is very easy to learn and is a relatively simple programming language. In general, it is easy to read because it doesn’t have a whole lot of syntax (think things like special words or symbols).

The price you pay for some of this flexibility and readability is that it may run slower than other languages like C or C++ or Java. For some tasks, it really doesn’t matter. For other tasks, it is very common to build a proof of concept program in python to make sure everything works as you expect and then refactor it in something like C or C++ when you need speed.

There are many reasons that we might choose python over other languages, but I will leave that discussion to another page. For now, let’s get started with some of the basics.

Getting python
python 2 vs python 3

Some more about this issue is listed on the python page here.

Linux

Python is already on your linux machine. To prove it (or make sure) you can open the terminal and just type python. It should give you something that looks like


Python VERSION 
Type "help", "copyright", "credits" or "license" for more information.
>>> 
1
2
3
Python VERSION 
Type "help", "copyright", "credits" or "license" for more information.
>>> 

If for some reason you don’t see this, then you can do something like sudo apt-get install python.

Mac

Python should already be installed on the Mac as well. Some versions ago, Mac didn’t install a complete python and therefore you had to install another one. I am not much of a mac user, so I don’t know the best option, but I am using everything as installed from the scipy superpack. Here are a list of options that I have found

Some options include

Windows

Windows does not come with python installed, therefore, you should go to the website and get whatever the newest python 2 is available (download site here). Avoid the python 3 versions (which are listed first). Then follow the instructions and you should have it all installed. Open it up and see what you have. I can recommend this site from the python site for making sure that you can run python files.

Installing other libraries

There are a number of other libraries that we will make use of that you can go ahead and install. These would include numpy and scipy. For Linux, you can just probably do something like sudo apt-get install python-scipy python-numpy.

For Mac, at least 10.8.2, see the installation options for python in general above.

For Windows, you will see there is information on the scipy website here. For 32 bit windows, you can just do the standard installations from that website for both numpy and scipy. If you have 64 bit Windows, I can recommend getting numpy and scipy from here (numpy) and here (scipy).

We will talk about these more in the scientific programming page.

Basic programming concepts in python

On the basic programming tutorial webpage (here), we briefly introduced things like conditional statements, for loops, functions, datatypes, and classes. You don’t need to be an expert in those before we start class, but it will serve you well to become familiar quickly. Here we will talk about how each of those concepts is actually applied in the python programming language. To run these check out the code in the hackingtrees repository on bitbucket or just use the code presented here (it is the same code and it all works) and you can run all these by putting the code into a file and running python filename.

Datatypes

As we discussed in the basic programming tutorial webpage (here), there are basic datatypes in programming. So we distinguish between things like strings of characters (or letters and words) and things like numbers. Even with numbers, we distinguish between those that have decimals and those that are just integers. This is because all of these are store in memory differently. In python, when you use a variable, you don’t have to immediately say what type of data that variable holds. Python tries to figure it out. This would be in contrast to something like C, C++, and Java where you explicitly say “this variable is going to hold a string of characters”. Ok, you don’t say it out loud but you denote it in the code. Here, I will introduce some of the important datatypes in python that we will use. Some issues specifically related to math and datatypes can be found in the Scientific Programming page.

Let’s start a file called datatypes.py. In this file we will explore the different datatypes.

Ints and floats (aka, numbers)
First, we will look at the two basic numbers, ints and floating points. Ints are just integers with no decimal places. Floating points are numbers that have decimal places. Here is what to put in the file:


a = 5 
b = 7
c = a+b #a and b are ints, so is c
print c

a = 5.5 #a is now holding a floating point
c = a+b #b is still an int, but c is a floating point
print c
1
2
3
4
5
6
7
8
a = 5 
b = 7
c = a+b #a and b are ints, so is c
print c

a = 5.5 #a is now holding a floating point
c = a+b #b is still an int, but c is a floating point
print c

What output do you get when you run python datatypes.py? You should get

12
12.5

So you see, we didn’t have to tell python that the second a was going to be a floating point number or that the resulting addition with b in the second case would also produce a floating point. Things start to go wrong if we do this though:


a = 1
b = 5
c = a/b
print c
1
2
3
4
a = 1
b = 5
c = a/b
print c

If you add this and run, you will see that instead of 0.2 you get 0. This is because python sees that a is an int and b is an int, so it assumes that anything you do with these two numbers will result in an int. However, we know that the result should be a floating point. So how do we force this? Well, we can do this


a = 1
b = 5
c = float(a)/b
print c
1
2
3
4
a = 1
b = 5
c = float(a)/b
print c

That is called “casting”. We are basically forcing a particular datatype. You can also get this result by simply adding a decimal after the 1 or the 5 like this


a = 1.
b = 5
c = a/b
print c
1
2
3
4
a = 1.
b = 5
c = a/b
print c

More about this can be found in the Scientific Programming page. Let’s move on.

Strings
Another basic datatype is the string of characters. This is pretty simple. If you want to print a word like “programming”, do you notice that to distinguish that word in the sentence I put quotes around it? Well you do the same thing in python.


a = "programming"
print a
1
2
a = "programming"
print a

That is it. If you want to add strings you can do things like


prea = "i am doing a bit of "
a = "programming"
print prea+a
1
2
3
prea = "i am doing a bit of "
a = "programming"
print prea+a

What happens if you add an int and a string? Well you get an error. You can try it


prea = "i like "
two = 2
a = " program"
print prea+two+a
1
2
3
4
prea = "i like "
two = 2
a = " program"
print prea+two+a

It is going to say

Traceback (most recent call last):
  File "datatypes.py", line 35, in 
    print prea+two+a
TypeError: cannot concatenate 'str' and 'int' objects

We learned about casting where we force a variable to be a particular datatype (at least for a short time). To cast the variable two to a string for just the printing, we can surround it with str(two) in the print line. This means that two will stay an int, but for that one line, it will be a string so we can print. That would make the above lines change to this


prea = "i like "
two = 2
a = " program"
print prea+str(two)+a
1
2
3
4
prea = "i like "
two = 2
a = " program"
print prea+str(two)+a

Lists
The list is a common datatype that functions in python much as you would expect any list to function. First off, it should be clear that you can have a list of any datatype (including classes that we will discuss). Here is a basic list:


a = [1,2,3]
1
a = [1,2,3]

To get particular elements we can do things like this


a = [1,2,3]
print a
a.append(4)
print a
print a[0]
print a[0]+a[1]
print a[-1]
print a[:2]
print a[-2:]
a.remove(2)
print a
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
a = [1,2,3]
print a
a.append(4)
print a
print a[0]
print a[0]+a[1]
print a[-1]
print a[:2]
print a[-2:]
a.remove(2)
print a

This will result in

[1, 2, 3]
[1, 2, 3, 4]
1
3
4
[1, 2]
[3, 4]
[1, 3, 4]

So you can see that we can print the whole list, we can append to it, we can get specific elements and do things with those elements (like add them), we can get the front or the end of the list, and we can remove elements from the list. The order of the list always stays the same. We will loop over things in the list a little later but not yet.

Dictionaries
The last basic datatype that we will talk about is called a dictionary. In python, dictionaries can be very powerful. These are also known as hashes if you have heard that term before. The dictionary works like a “key” and “value” system. So you give a key and then that key has a value. Think of a grocery store. Grocery stores will have a system where each product (key) has a price (value). You can ask the system for the value of the product. That would be a typical use of this kind of datatype.

In python this looks like


a = {}
a['candy1'] = 10
a['candy2'] = 15
print a
a['anotherproduct'] = 20
print a
print a['anotherproduct']
print a['candy2']
1
2
3
4
5
6
7
8
a = {}
a['candy1'] = 10
a['candy2'] = 15
print a
a['anotherproduct'] = 20
print a
print a['anotherproduct']
print a['candy2']

This results in something like this

{'candy2': 15, 'candy1': 10}
{'anotherproduct': 20, 'candy2': 15, 'candy1': 10}
20
15

Those are the basic datatypes in python. Next we are going to go over classes.

Pages: 1 2 3 4

One comment on “Python

  1. Pingback: Python information available ← Hacking Trees

Leave a Reply