Chat room

Create a Meebo Chat Room

Saturday, December 18, 2010

User-defined Functions:gcufbioinfo

Let's create another function. How about reverse?

>>> def reverse(s): 
...     """Return the sequence string in reverse order.""" 
...     letters = list(s) 
...     letters.reverse() 
...     return ''.join(letters) 
...      
>>> reverse('CCGGAAGAGCTTACTTAG') 
'GATTCATTCGAGAAGGCC' 
There are a few new things in this function that need explanation. First, we've used an argument name of "s" instead of "dna". You can name your arguments whatever you like in Python. It is something of a convention to use short names based on their expected value or meaning. So "s" for string is fairly common in Python code. The other reason to use "s" instead of "dna" in this example is that this function works correctly on any string, not just strings representing dna sequences. So "s" is a better reflection of the generic utility of this function than "dna".
You can see that the reverse function takes in a string, creates a list based on the string, and reverses the order of the list. Now we need to put the list back together as a string so we can return a string. Python string objects have a join() method that joins together a list into a string, separating each list element by a string value. Since we do not want any character as a separator, we use the join() method on an empty string, represented by two quotes ('' or "").

0 comments:

Post a Comment

Pages 381234 »
Twitter Delicious Facebook Digg Stumbleupon Favorites More