From Scratch: Writing Your Own Functions
From Scratch: Writing Your Own Functions
CHAPTER 4
Essentially a function is
● a block of code
def get_gc_content(dna):
length = len(dna)
g_count = dna.count('G')
c_count = dna.count('C')
gc_content = (g_count + c_count) / length
return gc_content
Note: This function will not work correctly with Python 2 unless you include the following
import at the top.
from __future__ import division
Calling a function
my_gc_content = get_gc_content("ATGCGCGATCGATCGAATCG")
print(str(my_gc_content))
print(get_gc_content("ATGCATGCAACTGTAGC"))
print(get_gc_content("aactgtagctagctagcagcgta"))
Improving our function
def get_gc_content(dna):
g_count = dna.upper().count('G')
c_count = dna.upper().count('C')
[...]
Improving our function